public class Interval1D extends Object
Interval1D
class represents a one-dimensional interval.
The interval is closed—it contains both endpoints.
Intervals are immutable: their values cannot be changed after they are created.
The class 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 Comparator<Interval1D> |
LENGTH_ORDER
Compares two intervals by length.
|
static Comparator<Interval1D> |
MAX_ENDPOINT_ORDER
Compares two intervals by max endpoint.
|
static Comparator<Interval1D> |
MIN_ENDPOINT_ORDER
Compares two intervals by min endpoint.
|
Constructor and Description |
---|
Interval1D(double min,
double max)
Initializes a closed interval [min, max].
|
Modifier and Type | Method and Description |
---|---|
boolean |
contains(double x)
Returns true if this interval contains the specified value.
|
boolean |
equals(Object other)
Compares this transaction to the specified object.
|
int |
hashCode()
Returns an integer hash code for this interval.
|
boolean |
intersects(Interval1D that)
Returns true if this interval intersects the specified interval.
|
double |
left()
Deprecated.
Replaced by
min() . |
double |
length()
Returns the length of this interval.
|
static void |
main(String[] args)
Unit tests the
Interval1D data type. |
double |
max()
Returns the max endpoint of this interval.
|
double |
min()
Returns the min endpoint of this interval.
|
double |
right()
Deprecated.
Replaced by
max() . |
String |
toString()
Returns a string representation of this interval.
|
public static final Comparator<Interval1D> MIN_ENDPOINT_ORDER
public static final Comparator<Interval1D> MAX_ENDPOINT_ORDER
public static final Comparator<Interval1D> LENGTH_ORDER
public Interval1D(double min, double max)
min
- the smaller endpointmax
- the larger endpointIllegalArgumentException
- if the min endpoint is greater than the max endpointIllegalArgumentException
- if either min
or max
is Double.NaN
, Double.POSITIVE_INFINITY
or
Double.NEGATIVE_INFINITY
@Deprecated public double left()
min()
.@Deprecated public double right()
max()
.public double min()
public double max()
public boolean intersects(Interval1D that)
that
- the other intervaltrue
if this interval intersects the argument interval;
false
otherwisepublic boolean contains(double x)
x
- the valuetrue
if this interval contains the value x
;
false
otherwisepublic double length()
public String toString()
public boolean equals(Object other)
public int hashCode()
public static void main(String[] args)
Interval1D
data type.args
- the command-line arguments