Package dsa
Class RectHV
- java.lang.Object
-
- dsa.RectHV
-
public class RectHV extends Object
An immutable data type to represent a 2d axis-aligned rectangle with real-valued coordinates.
-
-
Constructor Summary
Constructors Constructor Description RectHV(double xMin, double yMin, double xMax, double yMax)
Constructs a rectangle[xMin, yMin] x [xMax, yMax]
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(Point2D p)
Returnstrue
if this rectangle contains the pointp
, andfalse
otherwise.double
distanceSquaredTo(Point2D p)
Returns the squared Euclidean distance between the pointp
and the closest point on this, and 0 if the point is within.double
distanceTo(Point2D p)
Returns the Euclidean distance between the pointp
and the closest point on this rectangle, and 0 if the point is within.void
draw()
Draws this rectangle using standard draw.boolean
equals(Object other)
Returnstrue
if this rectangle and other have the same $x$- and $y$-bounds, and false otherwise.int
hashCode()
Returns a hash code for this rectangle.double
height()
Returns the height of this rectangle.boolean
intersects(RectHV other)
Returnstrue
if this rectangle intersects with other, andfalse
otherwise.static void
main(String[] args)
Unit tests the data type.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)
Constructs a rectangle[xMin, yMin] x [xMax, yMax]
.- Parameters:
xMin
- the x-coordinate of the lower-left endpoint.yMin
- the y-coordinate of the lower-left endpoint.xMax
- the x-coordinate of the upper-right endpoint.yMax
- the y-coordinate of the upper-right endpoint.
-
-
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.
-
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.
-
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.
-
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.
-
height
public double height()
Returns the height of this rectangle.- Returns:
- the height of this rectangle.
-
intersects
public boolean intersects(RectHV other)
Returnstrue
if this rectangle intersects with other, andfalse
otherwise.- Parameters:
other
- the other rectangle.- Returns:
true
if this rectangle intersects with other, andfalse
otherwise.
-
contains
public boolean contains(Point2D p)
Returnstrue
if this rectangle contains the pointp
, andfalse
otherwise.- Parameters:
p
- the point.- Returns:
true
if this rectangle contains the pointp
, andfalse
otherwise.
-
distanceTo
public double distanceTo(Point2D p)
Returns the Euclidean distance between the pointp
and the closest point on this rectangle, and 0 if the point is within.- Parameters:
p
- the point.- Returns:
- the Euclidean distance between the point
p
and the closest point on this rectangle, and 0 if the point is within.
-
distanceSquaredTo
public double distanceSquaredTo(Point2D p)
Returns the squared Euclidean distance between the pointp
and the closest point on this, and 0 if the point is within.- Parameters:
p
- the point.- Returns:
- the squared Euclidean distance between the point
p
and the closest point on this rectangle, and 0 if the point is within.
-
equals
public boolean equals(Object other)
Returnstrue
if this rectangle and other have the same $x$- and $y$-bounds, and false otherwise.
-
hashCode
public int hashCode()
Returns a hash code for this rectangle.
-
toString
public String toString()
Returns a string representation of this rectangle.
-
draw
public void draw()
Draws this rectangle using standard draw.
-
main
public static void main(String[] args)
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-
-