public final class Point2D extends Object implements Comparable<Point2D>
Point class is an immutable data type to encapsulate a
two-dimensional point with real-value coordinates.
Note: in order to deal with the difference behavior of double and Double with respect to -0.0 and +0.0, the Point2D constructor converts any coordinates that are -0.0 to +0.0.
For additional documentation, see Section 1.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
| Modifier and Type | Field and Description |
|---|---|
static Comparator<Point2D> |
R_ORDER
Compares two points by polar radius.
|
static Comparator<Point2D> |
X_ORDER
Compares two points by x-coordinate.
|
static Comparator<Point2D> |
Y_ORDER
Compares two points by y-coordinate.
|
| Constructor and Description |
|---|
Point2D(double x,
double y)
Initializes a new point (x, y).
|
| Modifier and Type | Method and Description |
|---|---|
static double |
area2(Point2D a,
Point2D b,
Point2D c)
Returns twice the signed area of the triangle a-b-c.
|
Comparator<Point2D> |
atan2Order()
Compares two points by atan2() angle (between –π and π) with respect to this point.
|
static int |
ccw(Point2D a,
Point2D b,
Point2D c)
Returns true if a→b→c is a counterclockwise turn.
|
int |
compareTo(Point2D that)
Compares two points by y-coordinate, breaking ties by x-coordinate.
|
double |
distanceSquaredTo(Point2D that)
Returns the square of the Euclidean distance between this point and that point.
|
double |
distanceTo(Point2D that)
Returns the Euclidean distance between this point and that point.
|
Comparator<Point2D> |
distanceToOrder()
Compares two points by distance to this point.
|
void |
draw()
Plot this point using standard draw.
|
void |
drawTo(Point2D that)
Plot a line from this point to that point using standard draw.
|
boolean |
equals(Object other)
Compares this point to the specified point.
|
int |
hashCode()
Returns an integer hash code for this point.
|
static void |
main(String[] args)
Unit tests the point data type.
|
Comparator<Point2D> |
polarOrder()
Compares two points by polar angle (between 0 and 2π) with respect to this point.
|
double |
r()
Returns the polar radius of this point.
|
double |
theta()
Returns the angle of this point in polar coordinates.
|
String |
toString()
Return a string representation of this point.
|
double |
x()
Returns the x-coordinate.
|
double |
y()
Returns the y-coordinate.
|
public static final Comparator<Point2D> X_ORDER
public static final Comparator<Point2D> Y_ORDER
public static final Comparator<Point2D> R_ORDER
public Point2D(double x,
double y)
x - the x-coordinatey - the y-coordinateIllegalArgumentException - if either x or y
is Double.NaN, Double.POSITIVE_INFINITY or
Double.NEGATIVE_INFINITYpublic double x()
public double y()
public double r()
public double theta()
public static int ccw(Point2D a, Point2D b, Point2D c)
a - first pointb - second pointc - third pointpublic static double area2(Point2D a, Point2D b, Point2D c)
a - first pointb - second pointc - third pointpublic double distanceTo(Point2D that)
that - the other pointpublic double distanceSquaredTo(Point2D that)
that - the other pointpublic int compareTo(Point2D that)
y0 < y1 or if y0 == y1 and x0 < x1.compareTo in interface Comparable<Point2D>that - the other point0 if this string is equal to the argument
string (precisely when equals() returns true);
a negative integer if this point is less than the argument
point; and a positive integer if this point is greater than the
argument pointpublic Comparator<Point2D> polarOrder()
public Comparator<Point2D> atan2Order()
public Comparator<Point2D> distanceToOrder()
public boolean equals(Object other)
public String toString()
public int hashCode()
public void draw()
public void drawTo(Point2D that)
that - the other pointpublic static void main(String[] args)
args - the command-line arguments