    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 * @version IFS 1.3 (Iterative Forward Search)<br>
    010 *          Copyright (C) 2006 - 2014 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     * @return x coordinate
    049     **/
    050    public int getX() {
    051        return iX;
    052    }
    053
    054    /** Gets y coordinate
    055     * @return y coordinate
    056     **/
    057    public int getY() {
    058        return iY;
    059    }
    060
    061    /**
    062     * Compare two coordinates. It is based on comparison of the parent
    063     * rectangle and x,y coordinates
    064     */
    065    @Override
    066    public boolean equals(Object object) {
    067        if (object == null || !(object instanceof Location)) {
    068            return false;
    069        }
    070        Location location = (Location) object;
    071
    072        return (variable().equals(location.variable()) && location.getX() == getX() && location.getY() == getY());
    073    }
    074
    075    /**
    076     * String representation (for debugging and printing purposes). For example,
    077     * rect43=[12,10] where rect43 is the name of the parent rectangle and
    078     * [12,10] is the location.
    079     */
    080    @Override
    081    public String toString() {
    082        return variable().getName() + "=[" + getX() + "," + getY() + "]";
    083    }
    084
    085    /** Location's name. E.g., [12,10] where x=12 and y=10. */
    086    @Override
    087    public String getName() {
    088        return "[" + getX() + "," + getY() + "]";
    089    }
    090
    091    /** Returns true if the given location intersects with this location
    092     * @param anotherLocation given location
    093     * @return true if the given location intersects with this location
    094     **/
    095    public boolean hasIntersection(Location anotherLocation) {
    096        if (getX() + (variable()).getWidth() <= anotherLocation.getX()) {
    097            return false;
    098        }
    099        if (getY() + (variable()).getHeight() <= anotherLocation.getY()) {
    100            return false;
    101        }
    102        if (anotherLocation.getX() + (anotherLocation.variable()).getWidth() <= getX()) {
    103            return false;
    104        }
    105        if (anotherLocation.getY() + (anotherLocation.variable()).getHeight() <= getY()) {
    106            return false;
    107        }
    108        return true;
    109    }
    110}