package simulator; import java.awt.geom.Point2D; /* * Location.java * Created on 2005/1/3 * */ /** * Location - This class represents a location in the amusement park. Currently * the location is simply a point in euclidean space. This is similar to * java.awt.point, but we don't need that much functionality. In the future we * might switch to a different location system such as graphs. * * @author Ping * */ public class Location implements Cloneable { // We currently just use integers to store the location private double x; private double y; public Location(double xCoord, double yCoord) { x = xCoord; y = yCoord; } /** * This function returns the x coordinate of the location * @return double representing the x coordinate */ public double getX() { return x; } /** * This function returns the y coordinate of the location * @return int representing the y coordinate */ public double getY() { return y; } /** * This function creates a new location/coordinate 1 unit * towards the location passed in * @param loc The location that this point is moving towards. * @return the new location that is 1 unit closer to the destination */ public Location moveTowards(Location loc) { double deltaX = loc.x - x; double deltaY = loc.y - y; double distance = Point2D.distance(x, y, loc.x, loc.y); double scale = 1 / distance; // now scale the differences appropriately deltaX *= scale; deltaY *= scale; // now move the point based on the scaled differences // This corresponds to a move of 1 unit return new Location(x + deltaX, y + deltaY); } /** * This function computes the distance from one location to another * location. The distance is simply the straight line distance * between two points rounded up to the nearest integer. * * @param loc The location that we are trying to find the distance * @return int representing the distance between the location passed in and * this location. */ public int distance(Location loc) { // we round up so use the ceiling function return (int)Math.ceil(Point2D.distance(x, y, loc.x, loc.y)); } public boolean equals(Object o) { if(o instanceof Location) { Location l = (Location)o; return l.x == this.x && l.y == this.y; } return false; } public Object clone() throws CloneNotSupportedException { return super.clone(); } }