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