Package dsa
Class Point2D
- java.lang.Object
-
- dsa.Point2D
-
- All Implemented Interfaces:
Comparable<Point2D>
public class Point2D extends Object implements Comparable<Point2D>
An immutable data type to represent a 2d point with real-valued coordinates.
-
-
Constructor Summary
Constructors Constructor Description Point2D(double x, double y)
Constructs a point(x, y)
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Comparator<Point2D>
atan2Order()
Returns a comparator for comparing two points by the atan2 angle (-pi to pi) they make with this point.int
compareTo(Point2D other)
Returns a comparison of this point withother
, by their x- and y-coordinates.double
distanceSquaredTo(Point2D other)
Returns the squared Euclidean distance between this point andother
.double
distanceTo(Point2D other)
Returns the Euclidean distance between this point andother
.Comparator<Point2D>
distanceToOrder()
Returns a comparator for comparing two points by their distance to this point.void
draw()
Draws this point using standard draw.void
drawTo(Point2D other)
Draws a line between this point andother
using standard draw.boolean
equals(Object other)
Returnstrue
if this point and other have the same x- and y-coordinates, andfalse
otherwise.int
hashCode()
Returns a hash code for this point.static void
main(String[] args)
Unit tests the data type.Comparator<Point2D>
polarOrder()
Returns a comparator for comparing two points by the polar angle (0 to 2pi) they make with this point.double
r()
Returns the polar radius of this point.static Comparator<Point2D>
rOrder()
Returns a comparator for comparing two points by their polar radius.double
theta()
Returns the polar angle (-pi to pi) of this point.String
toString()
Returns a string representation of this point.double
x()
Returns the x-coordinate of this point.static Comparator<Point2D>
xOrder()
Returns a comparator for comparing two points by their x-coordinate.double
y()
Returns the y-coordinate of this point.static Comparator<Point2D>
yOrder()
Returns a comparator for comparing two points by their y-coordinate.
-
-
-
Method Detail
-
x
public double x()
Returns the x-coordinate of this point.- Returns:
- the x-coordinate of this point.
-
y
public double y()
Returns the y-coordinate of this point.- Returns:
- the y-coordinate of this point.
-
r
public double r()
Returns the polar radius of this point.- Returns:
- the polar radius of this point.
-
theta
public double theta()
Returns the polar angle (-pi to pi) of this point.- Returns:
- the polar angle (-pi to pi) of this point.
-
distanceTo
public double distanceTo(Point2D other)
Returns the Euclidean distance between this point andother
.- Parameters:
other
- the other point.- Returns:
- the Euclidean distance between this point and
other
.
-
distanceSquaredTo
public double distanceSquaredTo(Point2D other)
Returns the squared Euclidean distance between this point andother
.- Parameters:
other
- the other point.- Returns:
- the squared Euclidean distance between this point and
other
.
-
equals
public boolean equals(Object other)
Returnstrue
if this point and other have the same x- and y-coordinates, andfalse
otherwise.
-
hashCode
public int hashCode()
Returns a hash code for this point.
-
toString
public String toString()
Returns a string representation of this point.
-
draw
public void draw()
Draws this point using standard draw.
-
drawTo
public void drawTo(Point2D other)
Draws a line between this point andother
using standard draw.- Parameters:
other
- the other point
-
compareTo
public int compareTo(Point2D other)
Returns a comparison of this point withother
, by their x- and y-coordinates.- Specified by:
compareTo
in interfaceComparable<Point2D>
- Parameters:
other
- the other point.- Returns:
- a comparison of this point with
other
, by their x- and y-coordinates.
-
atan2Order
public Comparator<Point2D> atan2Order()
Returns a comparator for comparing two points by the atan2 angle (-pi to pi) they make with this point.- Returns:
- a comparator for comparing two points by the atan2 angle (-pi to pi) they make with this point.
-
polarOrder
public Comparator<Point2D> polarOrder()
Returns a comparator for comparing two points by the polar angle (0 to 2pi) they make with this point.- Returns:
- a comparator for comparing two points by the polar angle (0 to 2pi) they make with this point.
-
distanceToOrder
public Comparator<Point2D> distanceToOrder()
Returns a comparator for comparing two points by their distance to this point.- Returns:
- a comparator for comparing two points by their distance to this point.
-
xOrder
public static Comparator<Point2D> xOrder()
Returns a comparator for comparing two points by their x-coordinate.- Returns:
- a comparator for comparing two points by their x-coordinate.
-
yOrder
public static Comparator<Point2D> yOrder()
Returns a comparator for comparing two points by their y-coordinate.- Returns:
- a comparator for comparing two points by their y-coordinate.
-
rOrder
public static Comparator<Point2D> rOrder()
Returns a comparator for comparing two points by their polar radius.- Returns:
- a comparator for comparing two points by their polar radius.
-
main
public static void main(String[] args)
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-
-