public final class RectHV extends Object
RectHV class is an immutable data type to encapsulate a
two-dimensional axis-aligned rectagle with real-value coordinates.
The rectangle is closed—it includes the points on the boundary.
For additional documentation, see Section 1.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
| Constructor and Description |
|---|
RectHV(double xmin,
double ymin,
double xmax,
double ymax)
Initializes a new rectangle [xmin, xmax]
x [ymin, ymax].
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
contains(Point2D p)
Returns true if this rectangle contains the point, and false otherwise.
|
double |
distanceSquaredTo(Point2D p)
Returns the square of the Euclidean distance between this rectangle and the point
p. |
double |
distanceTo(Point2D p)
Returns the Euclidean distance between this rectangle and the point
p. |
void |
draw()
Draws this rectangle to standard draw.
|
boolean |
equals(Object other)
Compares this rectangle to the specified rectangle.
|
int |
hashCode()
Returns an integer hash code for this rectangle.
|
double |
height()
Returns the height of this rectangle.
|
boolean |
intersects(RectHV that)
Returns true if the two rectangles intersect, and false otherwise.
|
String |
toString()
Returns a string representation of this rectangle.
|
double |
width()
Returns the width of this rectangle.
|
double |
xmax()
Returns the maximum x-coordinate of any point in this rectangle.
|
double |
xmin()
Returns the minimum x-coordinate of any point in this rectangle.
|
double |
ymax()
Returns the maximum y-coordinate of any point in this rectangle.
|
double |
ymin()
Returns the minimum y-coordinate of any point in this rectangle.
|
public RectHV(double xmin,
double ymin,
double xmax,
double ymax)
xmin - the x-coordinate of the lower-left endpointymin - the y-coordinate of the lower-left endpointxmax - the x-coordinate of the upper-right endpointymax - the y-coordinate of the upper-right endpointIllegalArgumentException - if any of xmin,
ymin, xmax, or ymax
is Double.NaN.IllegalArgumentException - if xmax < xmin or ymax < ymin.public double xmin()
public double xmax()
public double ymin()
public double ymax()
public double width()
xmax - xminpublic double height()
ymax - yminpublic boolean intersects(RectHV that)
that - the other rectangletrue if this rectangle intersects the argument
rectangle at one or more points; false otherwisepublic boolean contains(Point2D p)
p - the pointtrue if this rectangle contains the point p,
possibly at the boundary; false otherwisepublic double distanceTo(Point2D p)
p.p - the pointp and the closest point
on this rectangle; 0 if the point is contained in this rectanglepublic double distanceSquaredTo(Point2D p)
p.p - the pointp and
the closest point on this rectangle; 0 if the point is contained
in this rectanglepublic boolean equals(Object other)
public int hashCode()
public String toString()
public void draw()