001    package net.sf.cpsolver.ifs.example.rpp;
002    
003    import net.sf.cpsolver.ifs.model.*;
004    
005    /**
006     * Location (value, i.e., a single placement of the rectangle). Location encodes X and Y 
007     * coordinate.
008     * 
009     * @version
010     * IFS 1.1 (Iterative Forward Search)<br>
011     * Copyright (C) 2006 Tomáš Müller<br>
012     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
013     * Lazenska 391, 76314 Zlin, Czech Republic<br>
014     * <br>
015     * This library is free software; you can redistribute it and/or
016     * modify it under the terms of the GNU Lesser General Public
017     * License as published by the Free Software Foundation; either
018     * version 2.1 of the License, or (at your option) any later version.
019     * <br><br>
020     * This library is distributed in the hope that it will be useful,
021     * but WITHOUT ANY WARRANTY; without even the implied warranty of
022     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
023     * Lesser General Public License for more details.
024     * <br><br>
025     * You should have received a copy of the GNU Lesser General Public
026     * License along with this library; if not, write to the Free Software
027     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
028     */
029    public class Location extends Value {
030        private int iX, iY;
031        /**
032         * Constructor
033         * @param rectangle parent variable
034         * @param x x coordinate
035         * @param y y coordinate
036         */
037        public Location(Rectangle rectangle, int x, int y) {
038            super(rectangle);
039            iX = x;
040            iY = y;
041        }
042        
043        /** Gets x coordinate */
044        public int getX() {
045            return  iX;
046        }
047        
048        /** Gets y coordinate */
049        public int getY() {
050            return iY;
051        }
052        
053        /** Compare two coordinates. It is based on comparison of the parent rectangle and x,y coordinates */
054        public boolean equals(Object object) {
055            if (object == null || !(object instanceof Location)) {
056                return false;
057            }
058            Location location = (Location) object;
059            
060            return (variable().equals(location.variable()) && location.getX() == getX() && location.getY() == getY());
061        }
062        
063        /** String representation (for debugging and printing purposes). 
064         * For example, rect43=[12,10] where rect43 is the name of the parent rectangle and [12,10] is the location.
065         */
066        public String toString() {
067            return variable().getName() + "=[" + getX() + "," + getY() + "]";
068        }
069        
070        /** Location's name. E.g., [12,10] where x=12 and y=10.*/
071        public String getName() {
072            return "[" + getX() + "," + getY() + "]";
073        }
074        
075        /** Returns true if the given location intersects with this location */
076        public boolean hasIntersection(Location anotherLocation) {
077            if (getX() + ((Rectangle) variable()).getWidth() <= anotherLocation.getX()) {
078                return false;
079            }
080            if (getY() + ((Rectangle) variable()).getHeight() <= anotherLocation.getY()) {
081                return false;
082            }
083            if (anotherLocation.getX() + ((Rectangle) anotherLocation.variable()).getWidth() <= getX()) {
084                return false;
085            }
086            if (anotherLocation.getY() + ((Rectangle) anotherLocation.variable()).getHeight() <= getY()) {
087                return false;
088            }
089            return true;
090        }
091    }