Module org.dyn4j

Class Interval


  • public class Interval
    extends java.lang.Object
    Represents a one dimensional numeric Interval.
    Since:
    1.0.0
    Version:
    3.1.9
    Author:
    William Bittle
    • Field Detail

      • min

        protected double min
        The minimum value
      • max

        protected double max
        The maximum value
    • Constructor Detail

      • Interval

        public Interval​(double min,
                        double max)
        Full constructor.
        Parameters:
        min - the minimum value
        max - the maximum value
        Throws:
        java.lang.IllegalArgumentException - if min > max
      • Interval

        public Interval​(Interval interval)
        Copy constructor.
        Parameters:
        interval - the Interval to copy
        Since:
        3.1.1
    • Method Detail

      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getMin

        public double getMin()
        Returns the minimum value for this Interval.
        Returns:
        double
      • getMax

        public double getMax()
        Returns the maximum value for this Interval.
        Returns:
        double
      • setMin

        public void setMin​(double min)
        Sets the minimum for this interval.
        Parameters:
        min - the minimum value
        Throws:
        java.lang.IllegalArgumentException - if min > max
      • setMax

        public void setMax​(double max)
        Sets the maximum for this interval.
        Parameters:
        max - the maximum value
        Throws:
        java.lang.IllegalArgumentException - if max < min
      • includesInclusive

        public boolean includesInclusive​(double value)
        Returns true if the given value is within this Interval including the maximum and minimum.
        Parameters:
        value - the test value
        Returns:
        boolean
      • includesExclusive

        public boolean includesExclusive​(double value)
        Returns true if the given value is within this Interval exlcuding the maximum and minimum.
        Parameters:
        value - the test value
        Returns:
        boolean
      • includesInclusiveMin

        public boolean includesInclusiveMin​(double value)
        Returns true if the given value is within this Interval including the minimum and excluding the maximum.
        Parameters:
        value - the test value
        Returns:
        boolean
      • includesInclusiveMax

        public boolean includesInclusiveMax​(double value)
        Returns true if the given value is within this Interval including the maximum and excluding the minimum.
        Parameters:
        value - the test value
        Returns:
        boolean
      • overlaps

        public boolean overlaps​(Interval interval)
        Returns true if the two Intervals overlap.
        Parameters:
        interval - the Interval
        Returns:
        boolean
      • getOverlap

        public double getOverlap​(Interval interval)
        Returns the amount of overlap between this Interval and the given Interval.

        This method tests to if the Intervals overlap first. If they do then the overlap is returned, if they do not then 0 is returned.

        Parameters:
        interval - the Interval
        Returns:
        double
      • clamp

        public double clamp​(double value)
        If the value is within this Interval, inclusive, then return the value, else return either the max or minimum value.
        Parameters:
        value - the value to clamp
        Returns:
        double
      • clamp

        public static double clamp​(double value,
                                   double min,
                                   double max)
        Returns a number clamped between two other numbers.

        This method assumes that min ≤ max.

        Parameters:
        value - the value to clamp
        min - the min value
        max - the max value
        Returns:
        double
      • isDegenerate

        public boolean isDegenerate()
        Returns true if this Interval is degenerate.

        An Interval is degenerate if it equals [a, a].

        Returns:
        boolean
      • isDegenerate

        public boolean isDegenerate​(double error)
        Returns true if this Interval is degenerate given the allowed error.

        An Interval is degenerate given some error if max - min <= error.

        Parameters:
        error - the allowed error
        Returns:
        boolean
      • contains

        public boolean contains​(Interval interval)
        Returns true if the given Interval is contained in this Interval.
        Parameters:
        interval - the Interval
        Returns:
        boolean
      • distance

        public double distance​(Interval interval)
        Returns the distance between the two Intervals.

        If the given interval overlaps this interval, zero is returned.

        Parameters:
        interval - the Interval
        Returns:
        double
      • expand

        public void expand​(double value)
        Expands this Interval by half the given amount in both directions.

        The value can be negative to shrink the interval. However, if the value is greater than the current length of the interval, the interval can become invalid. In this case, the interval will become a degenerate interval at the mid point of the min and max.

        Parameters:
        value - the value
      • getExpanded

        public Interval getExpanded​(double value)
        Returns a new Interval of this interval expanded by half the given amount in both directions.

        The value can be negative to shrink the interval. However, if the value is greater than the current length of the interval, the interval will be invalid. In this case, the interval returned will be a degenerate interval at the mid point of the min and max.

        Parameters:
        value - the value
        Returns:
        Interval
        Since:
        3.1.1
      • getLength

        public double getLength()
        Returns the length of this interval from its min to its max.
        Returns:
        double
        Since:
        3.1.1