A simple Coordinate class for Java.
com.jdabtieu
Class Coord
java.lang.Object
com.jdabtieu.Coord
All Implemented Interfaces:
Cloneable, Comparable<Coord>
public class Coord
extends Object
implements Comparable<Coord>, Cloneable
The Coord class represents coordinates on a Cartesian plane.
It stores coordinates in the form (x,y) where x and y are signed 32-bit integers.
This class extends the functionality of the already existing java.awt.point, including but not limited to implementing the Comparable interface.
Constructor and Description |
---|
Coord(int x, int y) Constructs a new Coord which stores the coordinate (x, y) in the Cartesian plane. |
Coord(String x, String y) Constructs a new Coord which stores the coordinate (x, y) in the Cartesian plane. |
Modifier and Type | Method and Description |
---|---|
Coord | clone() Returns a new Coord with the same coordinates as this. |
int | compareTo(Coord arg0) Compares two Coords using the compareMode. |
double | distanceTo(Coord arg0) Deprecated - use eucDist instead Gets the straight-line distance from this to arg0. |
boolean | equals(Object obj) Compares this Coord to the specified object. |
double | eucDist(Coord arg0) Gets the straight-line (Euclidean) distance from this to arg0. |
int | getX() Gets the x-coordinate of this Coord. |
int | getY() Gets the y-coordinate of this Coord. |
int | hashCode() Returns a hash code for this Coord. |
long | manDist(Coord arg0) Gets the Manhattan distance from this to arg0. |
static void | setCompareMode(String newMode) Sets a new compareMode. |
void | setLocation(int x, int y) Sets the location of this Coord to (x,y). |
void | setLocation(String x, String y) Sets the location of this Coord to (x,y). |
void | setX(int x) Sets the x-coordinate of this Coord. |
void | setY(int y) Sets the y-coordinate of this Coord. |
String | toString() Converts this Coord into a String in the form (x,y). |
void | translate(int dx, int dy) Move this Coord to (x+dx,y+dy). |
void | translate(long dx, long dy) Move this Coord to (x+dx,y+dy). |
void | translate(String dx, String dy) Move this Coord to (x+dx,y+dy). |
Methods inherited from java.lang.Object |
---|
finalize , getClass , notify , notifyAll , wait , wait , wait |
Coord |
---|
public Coord(int x, int y) Initializes a newly created Coord object so that it represents the point (x,y) on the Cartesian plane.Parameters: x - The x-coordinatey - The y-coordinateSee also: setLocation(int, int) |
Coord |
---|
public Coord(String x, String y) Initializes a newly created Coord object so that it represents the point (x,y) on the Cartesian plane. Strings x and y must be integers, once trimmed of any leading or trailing whitespace.Parameters: x - The x-coordinatey - The y-coordinateThrows: NullPointerException - if one or both arguments are null NumberFormatException - if one or both arguments are not integers, are larger than Integer.MAX_VALUE , or less than Integer.MIN_VALUE See Also: setLocation(String, String) |
clone |
---|
public Coord clone() Returns a copy of this Coord. The copy has the same x- and y-coordinates as this Coord. Overrides: clone in class Object Returns: copy of this Coord |
compareTo |
---|
public int compareTo(Coord arg0) Compares the location of this Coord to another Coord arg0 based on compareMode. By default, a Coord is larger if its x-coordinate is larger than another Coord's x-coordinate, or if both are equal and the y-coordinate is greater. compareTo should never throw an exception unless the value of compareMode has been modified using a debugger.Specified by: compareTo in interface Comparable<String> Parameters: arg0 - the Coord to be comparedReturns: the value 0 if both Coords are equal; the value -1 if arg0 is greater than this ; the value 1 if this is greater than arg0 Throws: NullPointerException if compareMode is null IllegalStateException if compareMode is not validSee Also: setCompareMode(String) |
distanceTo |
---|
public double distanceTo(Coord arg0) Deprecated - use eucDist instead Calculates the straight-line distance to another Coord arg0 . The returned distance will always be zero or positive.Parameters: arg0 the Coord to calculate distance toReturns: the straight-line distance to arg0 |
equals |
---|
public boolean equals (Object obj) Compares this Coord to the specified object. The result is true if the argument is not null and is a Coord representing the same point on the Cartesian plane as this Coord.Overrides: equals in class Object Parameters: true if the given object is equivalent to this Coord, false otherwiseSee Also: compareTo(Coord) |
eucDist |
---|
public double eucDist(Coord arg0) Calculates the straight-line (Euclidean) distance to another Coord arg0 . The returned distance will always be zero or positive.Parameters: arg0 the Coord to calculate distance toReturns: the straight-line (Euclidean) distance to arg0 |
getX |
---|
public int getX() Returns the x-coordinate of this Coord. Returns: x-coordinate of this Coord |
getY |
---|
public int getY() Returns the y-coordinate of this Coord. Returns: y-coordinate of this Coord |
hashCode |
---|
public int hashCode() Returns a hash code for this Coord. The hash code for a Coord object is computed as (1 * 31 + x) * 31 + y . The hash code for the Coord representing the origin is 961.Overrides: hashCode in class Object Returns: a hash code for this Coord |
manDist |
---|
public long manDist(Coord arg0) Calculates the Manhattan distance to another Coord arg0 . The returned distance will always be zero or positive.Parameters: arg0 the Coord to calculate distance toReturns: the Manhattan distance to arg0 |
setCompareMode |
---|
public static void setCompareMode(String newMode) Sets a new compareMode for use in compareTo(Coord arg0 . The default is XY , in which the Coord with the larger x-coordinate, or if both are equal, the larger y-coordinate, is larger. Also available are:YX , in which the Coord with the larger y-coordinate, or if both are equal, the larger x-coordinate, is largerX , in which the Coord with the larger x-coordinate is largerY , in which the Coord with the larger y-coordinate is largerDIST , in which the Coord furthest from the origin is largerParameteres: newMode - the new compareModeThrows: NullPointerException if compareMode is null IllegalArgumentException if compareMode is not validSee Also: compareTo(Coord) |
setLocation |
---|
public void setLocation(int x, int y) Sets the location of this Coord to (x,y). This method is used by the constructor Coord(int x, int y) Parameters: x - The x-coordinatey - The y-coordinate |
setLocation |
---|
public void setLocation(String x, String y) Sets the location of this Coord to (x,y). Strings x and y must be integers, once trimmed of any leading or trailing whitespace. This method is used by the constructor Coord(String x, String y) Parameters: x - The x-coordinatey - The y-coordinateThrows: NullPointerException - if one or both arguments are null NumberFormatException - if one or both arguments are not integers, are larger than Integer.MAX_VALUE , or less than Integer.MIN_VALUE |
setX |
---|
public void setX(int x) Sets the x-coordinate of this Coord to x. Parameters: x - the new x-coordinate for this Coord |
setY |
---|
public void setY(int y) Sets the y-coordinate of this Coord to x. Parameters: y - the new y-coordinate for this Coord |
toString |
---|
public String toString() Returns a String representation for this Coord. The String returned will be in the form of ( x ,y ). For example, the Coord representing the origin would be (0,0) .Overrides: toString in class Object Returns: a String representation of this Coord |
translate |
---|
public void translate(int dx, int dy) Move this Coord to (x+dx,y+dy), if x+dx and y+dy are within the bounds of the 32-bit int. Parameters: dx - The amount to change the x-coordinate bydy - The amount to change the y-coordinate byThrows: ArithmeticException - if one or both arguments cause x or y to become larger than Integer.MAX_VALUE , or less than Integer.MIN_VALUE See Also: translate(long, long) |
translate |
---|
public void translate(long dx, long dy) Move this Coord to (x+dx,y+dy), if x+dx and y+dy are within the bounds of the 32-bit int. Parameters: dx - The amount to change the x-coordinate bydy - The amount to change the y-coordinate byThrows: ArithmeticException - if one or both arguments cause x or y to become larger than Integer.MAX_VALUE , or less than Integer.MIN_VALUE |
translate |
---|
public void translate(String dx, String dy) Move this Coord to (x+dx,y+dy), if dx and dy are integers once they are trimmed of leading and trailing whitespace, and if x+dx and y+dy are within the bounds of the 32-bit int. Parameters: dx - The amount to change the x-coordinate bydy - The amount to change the y-coordinate byThrows: NullPointerException - if one or both arguments are null NumberFormatException - if one or both arguments are not an integer of if they are greater than Long.MAX_VALUE or less than Long.MIN_VALUE ArithmeticException - if one or both arguments cause x or y to become larger than Integer.MAX_VALUE , or less than Integer.MIN_VALUE See Also: translate(long, long) |