import world.Posn; /** * To represent a location on a map * @author Viera K. Proulx * @since 20 March 2011 * */ public class Loc{ /** the latitude for this location */ int longitude; /** the longitude of this location */ int latitude; /** * The full constructor * @param longitude the give longitude * @param latitude the given latitude */ public Loc(int longitude, int latitude){ this.longitude = longitude; this.latitude = latitude; } /* TEMPLATE: FIELDS ... this.longitude ... -- int ... this.latitude ... -- int METHODS ... this.distanceTo(Loc3) ... -- double ... this.toX() ... -- int ... this.toY() ... -- int ... this.toPosn() ... -- Posn */ /** * Compute the distance from this location to the * given location * * @param that the given Loc * @return the distance between the two locations */ public double distanceTo(Loc that){ return Math.sqrt(55 * (this.latitude - that.latitude) * 55 * (this.latitude - that.latitude) + 70 * (this.longitude - that.longitude) * 70 * (this.longitude - that.longitude)); } /** * Convert the longitude to the x coordinate in the 400 by 400 scene * @param lng the given longitude * @return the desired x coordinate */ private int toX(int lng){ return 400 - 400 * (lng - 65) / 60; } /** * Convert the latitude to the x coordinate in the 400 by 400 scene * @param lat the given latitude * @return the desired x coordinate */ private int toY(int lat){ return 400 - 400 * (lat - 20) / 30; } /** * Represent this location as a Posn in a * 400 by 400 scene * @return the desired Posn */ public Posn toPosn(){ return new Posn(this.toX(this.longitude), this.toY(this.latitude)); } /** * Represent this location as a String * @return "(longitude, latitude)" */ public String toString(){ return this.longitude + ", " + this.latitude; } }