Git Product home page Git Product logo

coord's Introduction

A simple Coordinate class for Java.

Documentation

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 Summary

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.

Method Summary

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

Constructor Detail

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-coordinate
y - The y-coordinate
See 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-coordinate
y - The y-coordinate
Throws:
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)

Method Detail

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 compared
Returns:
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 valid
See 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 to
Returns:
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 otherwise
See 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 to
Returns:
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 to
Returns:
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 larger
X, in which the Coord with the larger x-coordinate is larger
Y, in which the Coord with the larger y-coordinate is larger
DIST, in which the Coord furthest from the origin is larger
Parameteres:
newMode - the new compareMode
Throws:
NullPointerException if compareMode is null
IllegalArgumentException if compareMode is not valid
See 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-coordinate
y - 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-coordinate
y - The y-coordinate
Throws:
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 by
dy - The amount to change the y-coordinate by
Throws:
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 by
dy - The amount to change the y-coordinate by
Throws:
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 by
dy - The amount to change the y-coordinate by
Throws:
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)

coord's People

Contributors

jdabtieu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.