Package edu.princeton.cs.algs4
Class RectHV
- Object
-
- edu.princeton.cs.algs4.RectHV
-
public final class RectHV extends Object
TheRectHV
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.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description RectHV(double xmin, double ymin, double xmax, double ymax)
Initializes a new rectangle [xmin, xmax] x [ymin, ymax].
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 pointp
.double
distanceTo(Point2D p)
Returns the Euclidean distance between this rectangle and the pointp
.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.
-
-
-
Constructor Detail
-
RectHV
public RectHV(double xmin, double ymin, double xmax, double ymax)
Initializes a new rectangle [xmin, xmax] x [ymin, ymax].- Parameters:
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 endpoint- Throws:
IllegalArgumentException
- if any ofxmin
,ymin
,xmax
, orymax
isDouble.NaN
.IllegalArgumentException
- ifxmax < xmin
orymax < ymin
.
-
-
Method Detail
-
xmin
public double xmin()
Returns the minimum x-coordinate of any point in this rectangle.- Returns:
- the minimum x-coordinate of any point in this rectangle
-
xmax
public double xmax()
Returns the maximum x-coordinate of any point in this rectangle.- Returns:
- the maximum x-coordinate of any point in this rectangle
-
ymin
public double ymin()
Returns the minimum y-coordinate of any point in this rectangle.- Returns:
- the minimum y-coordinate of any point in this rectangle
-
ymax
public double ymax()
Returns the maximum y-coordinate of any point in this rectangle.- Returns:
- the maximum y-coordinate of any point in this rectangle
-
width
public double width()
Returns the width of this rectangle.- Returns:
- the width of this rectangle
xmax - xmin
-
height
public double height()
Returns the height of this rectangle.- Returns:
- the height of this rectangle
ymax - ymin
-
intersects
public boolean intersects(RectHV that)
Returns true if the two rectangles intersect, and false otherwise. This includes improper intersections (at points on the boundary of each rectangle) and nested intersections (when one rectangle is contained inside the other).- Parameters:
that
- the other rectangle- Returns:
true
if this rectangle intersects the argument rectangle at one or more points; false otherwise
-
contains
public boolean contains(Point2D p)
Returns true if this rectangle contains the point, and false otherwise. This includes point on the boundary of the rectangle.- Parameters:
p
- the point- Returns:
true
if this rectangle contains the pointp
, possibly at the boundary;false
otherwise
-
distanceTo
public double distanceTo(Point2D p)
Returns the Euclidean distance between this rectangle and the pointp
.- Parameters:
p
- the point- Returns:
- the Euclidean distance between the point
p
and the closest point on this rectangle; 0 if the point is contained in this rectangle
-
distanceSquaredTo
public double distanceSquaredTo(Point2D p)
Returns the square of the Euclidean distance between this rectangle and the pointp
.- Parameters:
p
- the point- Returns:
- the square of the Euclidean distance between the point
p
and the closest point on this rectangle; 0 if the point is contained in this rectangle
-
equals
public boolean equals(Object other)
Compares this rectangle to the specified rectangle.
-
hashCode
public int hashCode()
Returns an integer hash code for this rectangle.
-
toString
public String toString()
Returns a string representation of this rectangle.
-
draw
public void draw()
Draws this rectangle to standard draw.
-
-