Class RectHV


  • public final class RectHV
    extends Object
    The 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.

    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 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.
    • 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 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
        Throws:
        IllegalArgumentException - if any of xmin, ymin, xmax, or ymax is Double.NaN.
        IllegalArgumentException - if xmax < xmin or ymax < 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 point p, possibly at the boundary; false otherwise
      • distanceTo

        public double distanceTo​(Point2D p)
        Returns the Euclidean distance between this rectangle and the point p.
        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 point p.
        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.
        Overrides:
        equals in class Object
        Parameters:
        other - the other rectangle
        Returns:
        true if this rectangle equals other; false otherwise
      • hashCode

        public int hashCode()
        Returns an integer hash code for this rectangle.
        Overrides:
        hashCode in class Object
        Returns:
        an integer hash code for this rectangle
      • toString

        public String toString()
        Returns a string representation of this rectangle.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this rectangle, using the format [xmin, xmax] x [ymin, ymax]
      • draw

        public void draw()
        Draws this rectangle to standard draw.