Package edu.princeton.cs.algs4
Class Interval2D
- Object
-
- edu.princeton.cs.algs4.Interval2D
-
public class Interval2D extends Object
TheInterval2D
class represents a closed two-dimensional interval, which represents all points (x, y) with bothxmin <= x <= xmax
andymin <= y <= ymax
. Two-dimensional intervals are immutable: their values cannot be changed after they are created. The classInterval2D
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 Summary
Constructors Constructor Description Interval2D(Interval1D x, Interval1D y)
Initializes a two-dimensional interval.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description double
area()
Returns the area of this two-dimensional interval.boolean
contains(Point2D p)
Does this two-dimensional interval contain the point p?void
draw()
Draws this two-dimensional interval to standard draw.boolean
equals(Object other)
Does this interval equal the other interval?int
hashCode()
Returns an integer hash code for this interval.boolean
intersects(Interval2D that)
Does this two-dimensional interval intersect that two-dimensional interval?static void
main(String[] args)
Unit tests theInterval2D
data type.String
toString()
Returns a string representation of this two-dimensional interval.
-
-
-
Constructor Detail
-
Interval2D
public Interval2D(Interval1D x, Interval1D y)
Initializes a two-dimensional interval.- Parameters:
x
- the one-dimensional interval of x-coordinatesy
- 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.
-
equals
public boolean equals(Object other)
Does this interval equal the other interval?
-
hashCode
public int hashCode()
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 theInterval2D
data type.- Parameters:
args
- the command-line arguments
-
-