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