Class MathUtil


  • public final class MathUtil
    extends Object
    Mathematic and geometric utilities.
    Version:
    17.0 2020-01-04 14:41:41
    Author:
    Stéphane GALLAND, Hamza JAFFALI
    Maven Group Id:
    org.arakhne.afc.core
    Maven Artifact Id:
    mathgen
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double clamp​(double v, double min, double max)
      Clamp the given value to the given range.
      static int clamp​(int v, int min, int max)
      Clamp the given value to the given range.
      static double clampCyclic​(double value, double min, double max)
      Clamp the given value to fit between the min and max values according to a cyclic heuristic.
      static double clampToNearestBounds​(double value, double minBounds, double maxBounds)
      Replies the value clamped to the nearest bounds.
      static int compareEpsilon​(double v1, double v2)
      Compares its two arguments for order.
      static int compareEpsilon​(double v1, double v2, double epsilon)
      Compares its two arguments for order.
      static double cot​(double angle)
      Replies the cotangent of the specified angle.
      static double crd​(double angle)
      Replies the chord of the specified angle.
      static double csc​(double angle)
      Replies the cosecant of the specified angle.
      static double exsec​(double angle)
      Replies the exsecant of the specified angle.
      static int getCohenSutherlandCode​(double px, double py, double rxmin, double rymin, double rxmax, double rymax)
      Compute the zone where the point is against the given rectangle according to the Cohen-Sutherland algorithm.
      static int getCohenSutherlandCode​(int px, int py, int rxmin, int rymin, int rxmax, int rymax)
      Compute the zone where the point is against the given rectangle according to the Cohen-Sutherland algorithm.
      static int getCohenSutherlandCode3D​(double px, double py, double pz, double rxmin, double rymin, double rzmin, double rxmax, double rymax, double rzmax)
      Compute the zone where the point is against the given rectangular prism according to the Cohen-Sutherland algorithm.
      static int getCohenSutherlandCode3D​(int px, int py, int pz, int rxmin, int rymin, int rzmin, int rxmax, int rymax, int rzmax)
      Compute the zone where the point is against the given rectangular prism according to the Cohen-Sutherland algorithm.
      static DoubleRange getMinMax​(double value1, double value2, double value3)
      Determine the min and max values from a set of three values.
      static double haversine​(double angle)
      Replies the half of the versine (aka, haversine) of the specified angle.
      static boolean isEpsilonEqual​(double v1, double v2)
      Replies if the given values are near.
      static boolean isEpsilonEqual​(double v1, double v2, double epsilon)
      Replies if the given values are near.
      static boolean isEpsilonZero​(double value)
      Replies if the given value is near zero.
      static boolean isEpsilonZero​(double value, double epsilon)
      Replies if the given value is near zero.
      static double max​(double... values)
      Replies the max value.
      static float max​(float... values)
      Replies the max value.
      static int max​(int... values)
      Replies the max value.
      static long max​(long... values)
      Replies the max value.
      static short max​(short... values)
      Replies the max value.
      static double min​(double... values)
      Replies the min value.
      static float min​(float... values)
      Replies the min value.
      static int min​(int... values)
      Replies the min value.
      static long min​(long... values)
      Replies the min value.
      static short min​(short... values)
      Replies the min value.
      static double sec​(double angle)
      Replies the secant of the specified angle.
      static int sign​(double value)
      Returns the sign of the argument; zero if the argument is zero, 1 if the argument is greater than zero, -1 if the argument is less than zero.
      static int signNoZero​(double value)
      Returns the sign of the argument; 1 if the argument is greater than or equal to zero, -1 if the argument is less than zero.
      static double versin​(double angle)
      Replies the versine of the specified angle.
    • Method Detail

      • sign

        @Pure
        public static int sign​(double value)
        Returns the sign of the argument; zero if the argument is zero, 1 if the argument is greater than zero, -1 if the argument is less than zero.

        This function differs from Math.signum(double) because it is returning a integer value.

        If you would like to treat the zero value as a positive number, see signNoZero(double).

        Parameters:
        value - the floating-point value whose sign is to be returned
        Returns:
        the sign of the argument, -1, 0 or 1.
        See Also:
        signNoZero(double), Math.signum(double)
      • signNoZero

        @Pure
        public static int signNoZero​(double value)
        Returns the sign of the argument; 1 if the argument is greater than or equal to zero, -1 if the argument is less than zero.

        This function differs from sign(double) because it assumes the zero value has a positive sign.

        Parameters:
        value - the floating-point value whose sign is to be returned
        Returns:
        the sign of the argument, -1 or 1.
        Since:
        16.0
        See Also:
        sign(double), Math.signum(double)
      • clamp

        @Pure
        public static double clamp​(double v,
                                   double min,
                                   double max)
        Clamp the given value to the given range.

        If the value is outside the [min;max] range, it is clamp to the nearest bounding value min or max.

        Parameters:
        v - is the value to clamp.
        min - is the min value of the range.
        max - is the max value of the range.
        Returns:
        the value in [min;max] range.
      • clamp

        @Pure
        public static int clamp​(int v,
                                int min,
                                int max)
        Clamp the given value to the given range.

        If the value is outside the [min;max] range, it is clamp to the nearest bounding value min or max.

        Parameters:
        v - is the value to clamp.
        min - is the min value of the range.
        max - is the max value of the range.
        Returns:
        the value in [min;max] range.
      • isEpsilonZero

        @Pure
        public static boolean isEpsilonZero​(double value)
        Replies if the given value is near zero.
        Parameters:
        value - is the value to test.
        Returns:
        true if the given value is near zero, otherwise false.
        See Also:
        Math.ulp(double)
      • isEpsilonZero

        @Pure
        public static boolean isEpsilonZero​(double value,
                                            double epsilon)
        Replies if the given value is near zero.
        Parameters:
        value - is the value to test.
        epsilon - the approximation epsilon. If Double.NaN, the function Math.ulp(double) is used for evaluating the epsilon.
        Returns:
        true if the given value is near zero, otherwise false.
      • isEpsilonEqual

        @Pure
        public static boolean isEpsilonEqual​(double v1,
                                             double v2)
        Replies if the given values are near.
        Parameters:
        v1 - first value.
        v2 - second value.
        Returns:
        true if the given v1 is near v2, otherwise false.
        See Also:
        Math.ulp(double)
      • isEpsilonEqual

        @Pure
        public static boolean isEpsilonEqual​(double v1,
                                             double v2,
                                             double epsilon)
        Replies if the given values are near.
        Parameters:
        v1 - first value.
        v2 - second value.
        epsilon - the approximation epsilon. If Double.NaN, the function Math.ulp(double) is used for evaluating the epsilon.
        Returns:
        true if the given v1 is near v2, otherwise false.
      • compareEpsilon

        @Pure
        public static int compareEpsilon​(double v1,
                                         double v2)
        Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
        Parameters:
        v1 - first value.
        v2 - second value.
        Returns:
        a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
      • compareEpsilon

        @Pure
        public static int compareEpsilon​(double v1,
                                         double v2,
                                         double epsilon)
        Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
        Parameters:
        v1 - first value.
        v2 - second value.
        epsilon - approximation epsilon. If Double.NaN, the function Math.ulp(double) is used for evaluating the epsilon.
        Returns:
        a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
      • max

        @Pure
        public static double max​(double... values)
        Replies the max value.
        Parameters:
        values - are the values to scan.
        Returns:
        the max value.
      • max

        @Pure
        public static float max​(float... values)
        Replies the max value.
        Parameters:
        values - are the values to scan.
        Returns:
        the max value.
      • max

        @Pure
        public static int max​(int... values)
        Replies the max value.
        Parameters:
        values - are the values to scan.
        Returns:
        the max value.
      • max

        @Pure
        public static long max​(long... values)
        Replies the max value.
        Parameters:
        values - are the values to scan.
        Returns:
        the max value.
      • max

        @Pure
        public static short max​(short... values)
        Replies the max value.
        Parameters:
        values - are the values to scan.
        Returns:
        the max value.
      • min

        @Pure
        public static double min​(double... values)
        Replies the min value.
        Parameters:
        values - are the values to scan.
        Returns:
        the min value.
      • min

        @Pure
        public static float min​(float... values)
        Replies the min value.
        Parameters:
        values - are the values to scan.
        Returns:
        the min value.
      • min

        @Pure
        public static int min​(int... values)
        Replies the min value.
        Parameters:
        values - are the values to scan.
        Returns:
        the min value.
      • min

        @Pure
        public static long min​(long... values)
        Replies the min value.
        Parameters:
        values - are the values to scan.
        Returns:
        the min value.
      • min

        @Pure
        public static short min​(short... values)
        Replies the min value.
        Parameters:
        values - are the values to scan.
        Returns:
        the min value.
      • clampCyclic

        @Pure
        public static double clampCyclic​(double value,
                                         double min,
                                         double max)
        Clamp the given value to fit between the min and max values according to a cyclic heuristic. If the given value is not between the minimum and maximum values, the replied value is modulo the min-max range.
        Parameters:
        value - the value to clamp.
        min - the minimum value inclusive.
        max - the maximum value exclusive.
        Returns:
        the clamped value
      • clampToNearestBounds

        @Pure
        public static double clampToNearestBounds​(double value,
                                                  double minBounds,
                                                  double maxBounds)
        Replies the value clamped to the nearest bounds. If |value-minBounds| > |value-maxBounds| then the returned value is maxBounds; otherwise it is minBounds.
        Parameters:
        value - is the value to clamp.
        minBounds - is the minimal allowed value.
        maxBounds - is the maximal allowed value.
        Returns:
        minBounds or maxBounds.
      • getCohenSutherlandCode3D

        @Pure
        public static int getCohenSutherlandCode3D​(int px,
                                                   int py,
                                                   int pz,
                                                   int rxmin,
                                                   int rymin,
                                                   int rzmin,
                                                   int rxmax,
                                                   int rymax,
                                                   int rzmax)
        Compute the zone where the point is against the given rectangular prism according to the Cohen-Sutherland algorithm.
        Parameters:
        px - is the coordinates of the point.
        py - is the coordinates of the point.
        pz - is the coordinates of the point.
        rxmin - is the min of the coordinates of the rectangular prism.
        rymin - is the min of the coordinates of the rectangular prism.
        rzmin - is the min of the coordinates of the rectangular prism.
        rxmax - is the max of the coordinates of the rectangular prism.
        rymax - is the max of the coordinates of the rectangular prism.
        rzmax - is the max of the coordinates of the rectangular prism.
        Returns:
        the zone
      • getCohenSutherlandCode3D

        @Pure
        public static int getCohenSutherlandCode3D​(double px,
                                                   double py,
                                                   double pz,
                                                   double rxmin,
                                                   double rymin,
                                                   double rzmin,
                                                   double rxmax,
                                                   double rymax,
                                                   double rzmax)
        Compute the zone where the point is against the given rectangular prism according to the Cohen-Sutherland algorithm.

        This function considers that if a point coordinate is equal to the a border coordinate of the rectangle, it is inside the rectangle.

        Parameters:
        px - is the coordinates of the point.
        py - is the coordinates of the point.
        pz - is the coordinates of the point.
        rxmin - is the min of the coordinates of the rectangular prism.
        rymin - is the min of the coordinates of the rectangular prism.
        rzmin - is the min of the coordinates of the rectangular prism.
        rxmax - is the max of the coordinates of the rectangular prism.
        rymax - is the max of the coordinates of the rectangular prism.
        rzmax - is the max of the coordinates of the rectangular prism.
        Returns:
        the zone
      • getMinMax

        public static DoubleRange getMinMax​(double value1,
                                            double value2,
                                            double value3)
        Determine the min and max values from a set of three values.

        This function has an algorithm that is efficient for 3 values.

        If one of the value is Double.NaN, it is ignored. If all the values are Double.NaN, the function replies null.

        Parameters:
        value1 - the first value.
        value2 - the second value.
        value3 - the third value.
        Returns:
        the min max range; or null.
        Since:
        13.0
      • csc

        @Pure
        public static double csc​(double angle)
        Replies the cosecant of the specified angle.

        csc(a) = 1/sin(a)

        [Cosecant function] [Cosecant function]

        Parameters:
        angle - the angle.
        Returns:
        the cosecant of the angle.
      • sec

        @Pure
        public static double sec​(double angle)
        Replies the secant of the specified angle.

        csc(a) = 1/cos(a)

        [Secant function] [Secant function]

        Parameters:
        angle - the angle.
        Returns:
        the secant of the angle.
      • cot

        @Pure
        public static double cot​(double angle)
        Replies the cotangent of the specified angle.

        csc(a) = 1/tan(a)

        [Cotangent function] [Cotangent function]

        Parameters:
        angle - the angle.
        Returns:
        the cotangent of the angle.
      • versin

        @Pure
        public static double versin​(double angle)
        Replies the versine of the specified angle.

        versin(a) = 1 - cos(a)

        [Versine function]

        Parameters:
        angle - the angle.
        Returns:
        the versine of the angle.
      • exsec

        @Pure
        public static double exsec​(double angle)
        Replies the exsecant of the specified angle.

        exsec(a) = sec(a) - 1

        [Exsecant function]

        Parameters:
        angle - the angle.
        Returns:
        the exsecant of the angle..
      • crd

        @Pure
        public static double crd​(double angle)
        Replies the chord of the specified angle.

        crd(a) = 2 sin(a/2)

        [Chord function]

        Parameters:
        angle - the angle.
        Returns:
        the chord of the angle.
      • haversine

        @Pure
        public static double haversine​(double angle)
        Replies the half of the versine (aka, haversine) of the specified angle.

        haversine(a) = sin2(a/2) = (1-cos(a)) / 2

        [Excosecant function]

        Parameters:
        angle - the angle.
        Returns:
        the half of the versine of the angle.