public class Interval1D extends java.lang.Object
Interval1D
includes methods for checking whether
an interval contains a point and determining whether two intervals intersect.
For additional documentation, see Section 1.2 of Algorithms, 4th Edition, by Robert Sedgewick and Kevin Wayne.
Modifier and Type | Field and Description |
---|---|
static java.util.Comparator<Interval1D> |
LEFT_ENDPOINT_ORDER
Compares two intervals by left endpoint.
|
static java.util.Comparator<Interval1D> |
LENGTH_ORDER
Compares two intervals by length.
|
static java.util.Comparator<Interval1D> |
RIGHT_ENDPOINT_ORDER
Compares two intervals by right endpoint.
|
Constructor and Description |
---|
Interval1D(double left,
double right)
Initializes an interval [left, right].
|
Modifier and Type | Method and Description |
---|---|
boolean |
contains(double x)
Does this interval contain the value x?
|
boolean |
intersects(Interval1D that)
Does this interval intersect that interval?
|
double |
left()
Returns the left endpoint.
|
double |
length()
Returns the length of this interval.
|
static void |
main(java.lang.String[] args)
Unit tests the Interval1D data type.
|
double |
right()
Returns the right endpoint.
|
java.lang.String |
toString()
Returns a string representation of this interval.
|
public static final java.util.Comparator<Interval1D> LEFT_ENDPOINT_ORDER
public static final java.util.Comparator<Interval1D> RIGHT_ENDPOINT_ORDER
public static final java.util.Comparator<Interval1D> LENGTH_ORDER
public Interval1D(double left, double right)
left
- the left endpointright
- the right endpointjava.lang.IllegalArgumentException
- if the left endpoint is greater than the right endpointjava.lang.IllegalArgumentException
- if either left or right
is Double.NaN, Double.POSITIVE_INFINITY or
Double.NEGATIVE_INFINITYpublic double left()
public double right()
public boolean intersects(Interval1D that)
that
- the other intervalpublic boolean contains(double x)
x
- the valuepublic double length()
public java.lang.String toString()
toString
in class java.lang.Object
public static void main(java.lang.String[] args)