Class Interval2D


  • public class Interval2D
    extends Object
    The Interval2D class represents a closed two-dimensional interval, which represents all points (x, y) with both xmin <= x <= xmax and ymin <= y <= ymax. Two-dimensional intervals are immutable: their values cannot be changed after they are created. The class Interval2D includes methods for checking whether a two-dimensional interval contains a point and determining whether two two-dimensional intervals intersect.

    For additional documentation, see Section 1.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

    Author:
    Robert Sedgewick, Kevin Wayne
    • Constructor Detail

      • Interval2D

        public Interval2D​(Interval1D x,
                          Interval1D y)
        Initializes a two-dimensional interval.
        Parameters:
        x - the one-dimensional interval of x-coordinates
        y - the one-dimensional interval of y-coordinates
    • Method Detail

      • intersects

        public boolean intersects​(Interval2D that)
        Does this two-dimensional interval intersect that two-dimensional interval?
        Parameters:
        that - the other two-dimensional interval
        Returns:
        true if this two-dimensional interval intersects that two-dimensional interval; false otherwise
      • contains

        public boolean contains​(Point2D p)
        Does this two-dimensional interval contain the point p?
        Parameters:
        p - the two-dimensional point
        Returns:
        true if this two-dimensional interval contains the point p; false otherwise
      • area

        public double area()
        Returns the area of this two-dimensional interval.
        Returns:
        the area of this two-dimensional interval
      • toString

        public String toString()
        Returns a string representation of this two-dimensional interval.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this two-dimensional interval in the form [xmin, xmax] x [ymin, ymax]
      • equals

        public boolean equals​(Object other)
        Does this interval equal the other interval?
        Overrides:
        equals in class Object
        Parameters:
        other - the other interval
        Returns:
        true if this interval equals the other interval; false otherwise
      • hashCode

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

        public void draw()
        Draws this two-dimensional interval to standard draw.
      • main

        public static void main​(String[] args)
        Unit tests the Interval2D data type.
        Parameters:
        args - the command-line arguments