Class MathUtils

java.lang.Object
com.blackrook.gloop.openal.struct.MathUtils

public final class MathUtils extends Object
A class with static methods that perform "other" types of mathematics.

A bunch of the collision/intersection algorithms found in this class are adapted from Real-Time Collision Detection by Christer Ericson (ISBN-13: 978-1-55860-732-3).

Author:
Matthew Tropiano
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    PI over Two.
    static final double
    Three PI over Two.
    static final double
    Two PI.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean[]
    andBooleanArrays(boolean[] b1, boolean[] b2)
    Logically "and"-s two boolean arrays together.
    static double
    angleInterpolateDegrees(double factor, double degA, double degB)
    Special angle interpolation.
    static double
    angleInterpolateRadians(double factor, double radA, double radB)
    Special angle interpolation.
    static double
    clampValue(double val, double lo, double hi)
    Coerces a double to the range bounded by lo and hi.
    static float
    clampValue(float val, float lo, float hi)
    Coerces a float to the range bounded by lo and hi.
    static int
    clampValue(int val, int lo, int hi)
    Coerces an integer to the range bounded by lo and hi.
    static short
    clampValue(short val, short lo, short hi)
    Coerces a short to the range bounded by lo and hi.
    static int
    closer(double value, int v1, int v2)
    Returns the value that "value" is closest to.
    static double
    closerComponent(double val, double min, double max)
    Returns (val - min) if val is closer to min than max, (max - val) otherwise.
    static int
    Finds the closest power of two to an integer value, larger than the initial value.
    static void
    correctClippingAspect(double targetAspect, double left, double right, double bottom, double top, double[] outBounds)
    Corrects a set of view clipping bounds to fit into a different aspect.
    static double
    cosineInterpolate(double factor, double x, double y)
    Gives a value that is the result of a cosine interpolation between two values.
    static double
    cubicInterpolate(double factor, double w, double x, double y, double z)
    Gives a value that is the result of a cublic interpolation between two values.
    static double
    degToRad(double degrees)
    Converts degrees to radians.
    static int
    farther(double value, int v1, int v2)
    Returns the value that "value" is farthest from.
    static int
    gcd(int a, int b)
    Returns the greatest common divisor of two integers.
    static double
    getAngleDotProductDegrees(double degA, double degB)
    Returns a simulated dot product between two degree angles as though they were unit vectors, rotated.
    static double
    getAngleDotProductRadians(double radA, double radB)
    Returns a simulated dot product between two radian angles as though they were unit vectors, rotated.
    static double
    getAngularDistanceDegrees(double angle1, double angle2)
    Returns the difference in degrees between two angles.
    static double
    getAngularDistanceRadians(double angle1, double angle2)
    Returns the difference in radians between two angles.
    static double
    getInterpolationFactor(double value, double lo, double hi)
    Gets a scalar factor that equals how "far along" a value is along an interval.
    static boolean
    getIntersectionBox(double spx, double spy, double shw, double shh, double tpx, double tpy, double thw, double thh)
    Tests if two boxes intersect.
    static boolean
    getIntersectionBox2DPlane(double ax, double ay, double bx, double by, double bcx, double bcy, double bhw, double bhh)
    Checks if an axis-aligned box breaks a 2D plane, represented by a line.
    static boolean
    getIntersectionCircle(double spx, double spy, double srad, double tpx, double tpy, double trad)
    Returns if two described circles intersect.
    static boolean
    getIntersectionCircle2DPlane(double ax, double ay, double bx, double by, double ccx, double ccy, double crad)
    Checks if a circle breaks a 2D plane, represented by a line.
    static boolean
    getIntersectionCircleBox(double ccx, double ccy, double crad, double bcx, double bcy, double bhw, double bhh)
    Returns if a circle and box intersect.
    static boolean
    getIntersectionCylinder(float sx, float sy, float sz, float srad, float sh, float tx, float ty, float tz, float trad, float th)
    Checks the collision of two cylinders.
    static double
    getIntersectionLine(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy)
    Tests if an intersection occurs between two line segments.
    static double
    getIntersectionLineBox(double ax, double ay, double bx, double by, double bcx, double bcy, double bhw, double bhh)
    Tests if a line segment intersects with a box.
    static double
    getIntersectionLineCircle(double ax, double ay, double bx, double by, double ccx, double ccy, double crad)
    Tests if a line segment intersects with a circle.
    static double
    getIntersectionLinePlane(double ax, double ay, double az, double bx, double by, double bz, double pnx, double pny, double pnz, double pdist)
    Tests if a line intersects with a plane.
    static boolean
    getIntersectionPoint2DPlane(double ax, double ay, double bx, double by, double px, double py)
    Checks if a point lies behind a 2D plane, represented by a line.
    static double
    getLineLength(double x0, double y0, double x1, double y1)
    Returns the length of a line by the coordinates of the two points that comprise it.
    static double
    getLineLength(double x0, double y0, double z0, double x1, double y1, double z1)
    Returns the length of a line by the coordinates of the two points that comprise it.
    static double
    getLineLengthSquared(double x0, double y0, double x1, double y1)
    Returns the squared length of a line by the coordinates of the two points that comprise it.
    static double
    getLineLengthSquared(double x0, double y0, double z0, double x1, double y1, double z1)
    Returns the squared length of a line by the coordinates of the two points that comprise it.
    static double
    getLinePointSide(double ax, double ay, double bx, double by, double px, double py)
    Checks what "side" a point is on a line.
    static double
    getObservedAngleDegrees(double viewX, double viewY, double targetX, double targetY, double targetDegrees)
    Returns the observed angle of an object in space facing a direction.
    static double
    getObservedAngleRadians(double viewX, double viewY, double targetX, double targetY, double targetRadians)
    Returns the observed angle of an object in space facing a direction.
    static int
    getPercent(int x, float percentage)
    Returns the percentage of an integer to the nearest complete integer.
    static double
    getRelativeAngleDegrees(double angle1, double angle2)
    Returns the relative angle between two angles.
    static double
    getRelativeAngleRadians(double angle1, double angle2)
    Returns the relative angle between two angles.
    static double
    getTriangleArea(double ax, double ay, double bx, double by, double cx, double cy)
    Returns the signed area of a triangular area made up of 3 points.
    static double
    getTriangleAreaDoubleSigned(double ax, double ay, double bx, double by, double cx, double cy)
    Returns the doubled signed area of a triangular area made up of 3 points.
    static double
    getTriangleAreaSigned(double ax, double ay, double bx, double by, double cx, double cy)
    Returns the signed area of a triangular area made up of 3 points.
    static double
    getVectorAngleDegrees(double x, double y)
    Returns the angular rotation of a vector described in two dimensions.
    static double
    getVectorAngleRadians(double x, double y)
    Returns the angular rotation of a vector described in two dimensions.
    static double
    getVectorDotProduct(double v1x, double v1y, double v2x, double v2y)
    Returns the dot product of two vectors.
    static double
    getVectorDotProduct(double v1x, double v1y, double v1z, double v2x, double v2y, double v2z)
    Returns the dot product of two vectors.
    static double
    getVectorLength(double x, double y)
    Returns the length of a vector by its components.
    static double
    getVectorLength(double x, double y, double z)
    Returns the length of a vector by its components.
    static double
    getVectorLengthSquared(double x, double y)
    Returns the squared length of a vector by its components.
    static double
    getVectorLengthSquared(double x, double y, double z)
    Returns the squared length of a vector by its components.
    static double
    getVectorUnitDotProduct(double v1x, double v1y, double v2x, double v2y)
    Returns the dot product of two vectors, converted to unit vectors first.
    static double
    getVectorUnitDotProduct(double v1x, double v1y, double v1z, double v2x, double v2y, double v2z)
    Returns the dot product of two vectors, converted to unit vectors first.
    static boolean
    isPowerOfTwo(int x)
    Checks if an integer is a valid power of two.
    static boolean
    isPowerOfTwo(long x)
    Checks if an integer is a valid power of two.
    static double
    linearInterpolate(double factor, double x, double y)
    Gives a value that is the result of a linear interpolation between two values.
    static boolean[]
    orBooleanArrays(boolean[] b1, boolean[] b2)
    Logically "or"-s two boolean arrays together.
    static double
    radToDeg(double radians)
    Converts radians to degrees.
    static byte
    rotateLeft(byte n, int x)
    Rotates the bits of a number to the left.
    static int
    rotateLeft(int n, int x)
    Rotates the bits of a number to the left.
    static long
    rotateLeft(long n, int x)
    Rotates the bits of a number to the left.
    static short
    rotateLeft(short n, int x)
    Rotates the bits of a number to the left.
    static byte
    rotateRight(byte n, int x)
    Rotates the bits of a number to the right.
    static int
    rotateRight(int n, int x)
    Rotates the bits of a number to the right.
    static long
    rotateRight(long n, int x)
    Rotates the bits of a number to the right.
    static short
    rotateRight(short n, int x)
    Rotates the bits of a number to the right.
    static double
    roundToNearest(double value, double increment)
    Rounds to the nearest increment.
    static double
    sanitizeAngleDegrees(double angle)
    Takes an angle in degrees and corrects it to the [0, 360) interval.
    static double
    sanitizeAngleRadians(double angle)
    Takes an angle in radians and corrects it to the [0, 2PI) interval.
    static double
    wrapValue(double val, double lo, double hi)
    Coerces a double to the range bounded by lo and hi, by "wrapping" the value.
    static float
    wrapValue(float val, float lo, float hi)
    Coerces a float to the range bounded by lo and hi, by "wrapping" the value.
    static int
    wrapValue(int val, int lo, int hi)
    Coerces an integer to the range bounded by lo and hi, by "wrapping" the value.
    static short
    wrapValue(short val, short lo, short hi)
    Coerces a short to the range bounded by lo and hi, by "wrapping" the value.
    static boolean[]
    xorBooleanArrays(boolean[] b1, boolean[] b2)
    Logically "xor"-s two boolean arrays together.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • rotateLeft

      public static int rotateLeft(int n, int x)
      Rotates the bits of a number to the left.
      Parameters:
      n - the input number.
      x - the number of positions.
      Returns:
      the resultant number.
    • rotateRight

      public static int rotateRight(int n, int x)
      Rotates the bits of a number to the right.
      Parameters:
      n - the input number.
      x - the number of positions.
      Returns:
      the resultant number.
    • rotateLeft

      public static short rotateLeft(short n, int x)
      Rotates the bits of a number to the left.
      Parameters:
      n - the input number.
      x - the number of positions.
      Returns:
      the resultant number.
    • rotateRight

      public static short rotateRight(short n, int x)
      Rotates the bits of a number to the right.
      Parameters:
      n - the input number.
      x - the number of positions.
      Returns:
      the resultant number.
    • rotateLeft

      public static byte rotateLeft(byte n, int x)
      Rotates the bits of a number to the left.
      Parameters:
      n - the input number.
      x - the number of positions.
      Returns:
      the resultant number.
    • rotateRight

      public static byte rotateRight(byte n, int x)
      Rotates the bits of a number to the right.
      Parameters:
      n - the input number.
      x - the number of positions.
      Returns:
      the resultant number.
    • rotateLeft

      public static long rotateLeft(long n, int x)
      Rotates the bits of a number to the left.
      Parameters:
      n - the input number.
      x - the number of positions.
      Returns:
      the resultant number.
    • rotateRight

      public static long rotateRight(long n, int x)
      Rotates the bits of a number to the right.
      Parameters:
      n - the input number.
      x - the number of positions.
      Returns:
      the resultant number.
    • closestPowerOfTwo

      public static int closestPowerOfTwo(int x)
      Finds the closest power of two to an integer value, larger than the initial value.

      Examples:

      • If x is 19, this returns 32.
      • If x is 4, this returns 4.
      • If x is 99, this returns 128.
      • If x is 129, this returns 256.
      Parameters:
      x - the input value.
      Returns:
      the closest power of two.
    • gcd

      public static int gcd(int a, int b)
      Returns the greatest common divisor of two integers.
      Parameters:
      a - the first integer.
      b - the second integer.
      Returns:
      the GCD.
    • isPowerOfTwo

      public static boolean isPowerOfTwo(int x)
      Checks if an integer is a valid power of two.
      Parameters:
      x - the input value.
      Returns:
      true if it is, false if not.
    • isPowerOfTwo

      public static boolean isPowerOfTwo(long x)
      Checks if an integer is a valid power of two.
      Parameters:
      x - the input value.
      Returns:
      true if it is, false if not.
    • getPercent

      public static int getPercent(int x, float percentage)
      Returns the percentage of an integer to the nearest complete integer.
      Example: getPercent(20,50) returns 10.
      Example 2: getPercent(10,25) returns 2.
      Parameters:
      x - the integer.
      percentage - the percentage.
      Returns:
      the result equal to floor of x*(percentage/100), mathematically.
    • radToDeg

      public static double radToDeg(double radians)
      Converts radians to degrees.
      Parameters:
      radians - the input angle in radians.
      Returns:
      the resultant angle in degrees.
    • degToRad

      public static double degToRad(double degrees)
      Converts degrees to radians.
      Parameters:
      degrees - the input angle in degrees.
      Returns:
      the resultant angle in radians.
    • sanitizeAngleDegrees

      public static double sanitizeAngleDegrees(double angle)
      Takes an angle in degrees and corrects it to the [0, 360) interval.
      Parameters:
      angle - the input angle.
      Returns:
      the equivalent angle in degrees.
    • sanitizeAngleRadians

      public static double sanitizeAngleRadians(double angle)
      Takes an angle in radians and corrects it to the [0, 2PI) interval.
      Parameters:
      angle - the input angle.
      Returns:
      the equivalent angle in radians.
    • roundToNearest

      public static double roundToNearest(double value, double increment)
      Rounds to the nearest increment.
      Example: roundToNearest(3.4, 1.0) returns 3.0.
      Example: roundToNearest(3.4, 5.0) returns 5.0.
      Example: roundToNearest(3.4, 0.3) returns 3.3.
      Parameters:
      value - the value to round.
      increment - the incremental value.
      Returns:
      the nearest increment using the input value.
    • clampValue

      public static int clampValue(int val, int lo, int hi)
      Coerces an integer to the range bounded by lo and hi.
      Example: clampValue(32,-16,16) returns 16.
      Example: clampValue(4,-16,16) returns 4.
      Example: clampValue(-1000,-16,16) returns -16.
      Parameters:
      val - the integer.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "forced" into the range.
    • clampValue

      public static short clampValue(short val, short lo, short hi)
      Coerces a short to the range bounded by lo and hi.
      Example: clampValue(32,-16,16) returns 16.
      Example: clampValue(4,-16,16) returns 4.
      Example: clampValue(-1000,-16,16) returns -16.
      Parameters:
      val - the short.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "forced" into the range.
    • clampValue

      public static float clampValue(float val, float lo, float hi)
      Coerces a float to the range bounded by lo and hi.
      Example: clampValue(32,-16,16) returns 16.
      Example: clampValue(4,-16,16) returns 4.
      Example: clampValue(-1000,-16,16) returns -16.
      Parameters:
      val - the float.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "forced" into the range.
    • clampValue

      public static double clampValue(double val, double lo, double hi)
      Coerces a double to the range bounded by lo and hi.
      Example: clampValue(32,-16,16) returns 16.
      Example: clampValue(4,-16,16) returns 4.
      Example: clampValue(-1000,-16,16) returns -16.
      Parameters:
      val - the double.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "forced" into the range.
    • wrapValue

      public static int wrapValue(int val, int lo, int hi)
      Coerces an integer to the range bounded by lo and hi, by "wrapping" the value.
      Example: wrapValue(32,-16,16) returns 0.
      Example: wrapValue(4,-16,16) returns 4.
      Example: wrapValue(-1000,-16,16) returns 8.
      Parameters:
      val - the integer.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "wrapped" into the range.
    • wrapValue

      public static short wrapValue(short val, short lo, short hi)
      Coerces a short to the range bounded by lo and hi, by "wrapping" the value.
      Example: wrapValue(32,-16,16) returns 0.
      Example: wrapValue(4,-16,16) returns 4.
      Example: wrapValue(-1000,-16,16) returns 8.
      Parameters:
      val - the short.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "wrapped" into the range.
    • wrapValue

      public static float wrapValue(float val, float lo, float hi)
      Coerces a float to the range bounded by lo and hi, by "wrapping" the value.
      Example: wrapValue(32,-16,16) returns 0.
      Example: wrapValue(4,-16,16) returns 4.
      Example: wrapValue(-1000,-16,16) returns 8.
      Parameters:
      val - the float.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "wrapped" into the range.
    • wrapValue

      public static double wrapValue(double val, double lo, double hi)
      Coerces a double to the range bounded by lo and hi, by "wrapping" the value.
      Example: wrapValue(32,-16,16) returns 0.
      Example: wrapValue(4,-16,16) returns 4.
      Example: wrapValue(-1000,-16,16) returns 8.
      Parameters:
      val - the double.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "wrapped" into the range.
    • andBooleanArrays

      public static boolean[] andBooleanArrays(boolean[] b1, boolean[] b2)
      Logically "and"-s two boolean arrays together. If both arrays are not the same size, an array of length max(b1.length, b2.length) is returned and the longer array is "and"-ed against falses past min(b1.length, b2.length).
      Parameters:
      b1 - the first array.
      b2 - the second array.
      Returns:
      A new boolean array that is the logical "and" of both arrays.
    • orBooleanArrays

      public static boolean[] orBooleanArrays(boolean[] b1, boolean[] b2)
      Logically "or"-s two boolean arrays together. If both arrays are not the same size, an array of length max(b1.length, b2.length) is returned and the longer array is "or"-ed against falses past min(b1.length, b2.length).
      Parameters:
      b1 - the first array.
      b2 - the second array.
      Returns:
      A new boolean array that is the logical "or" of both arrays.
    • xorBooleanArrays

      public static boolean[] xorBooleanArrays(boolean[] b1, boolean[] b2)
      Logically "xor"-s two boolean arrays together. If both arrays are not the same size, an array of length max(b1.length, b2.length) is returned and the longer array is "xor"-ed against falses past min(b1.length, b2.length).
      Parameters:
      b1 - the first array.
      b2 - the second array.
      Returns:
      A new boolean array that is the logical "xor" of both arrays.
    • closer

      public static int closer(double value, int v1, int v2)
      Returns the value that "value" is closest to.
      Parameters:
      value - the input value
      v1 - first evaluating value
      v2 - second evaluating value
      Returns:
      either v1 or v2, whichever's closer.
    • closerComponent

      public static double closerComponent(double val, double min, double max)
      Returns (val - min) if val is closer to min than max, (max - val) otherwise. Result is always positive.
      Parameters:
      val - the value to test.
      min - the minimum bound.
      max - the maximum bound.
      Returns:
      the closer component value.
    • farther

      public static int farther(double value, int v1, int v2)
      Returns the value that "value" is farthest from.
      Parameters:
      value - the input value
      v1 - first evaluating value
      v2 - second evaluating value
      Returns:
      either v1 or v2, whichever's farther.
    • linearInterpolate

      public static double linearInterpolate(double factor, double x, double y)
      Gives a value that is the result of a linear interpolation between two values.
      Parameters:
      factor - the interpolation factor.
      x - the first value.
      y - the second value.
      Returns:
      the interpolated value.
    • cosineInterpolate

      public static double cosineInterpolate(double factor, double x, double y)
      Gives a value that is the result of a cosine interpolation between two values.
      Parameters:
      factor - the interpolation factor.
      x - the first value.
      y - the second value.
      Returns:
      the interpolated value.
    • cubicInterpolate

      public static double cubicInterpolate(double factor, double w, double x, double y, double z)
      Gives a value that is the result of a cublic interpolation between two values. Requires two outside values to predict a curve more accurately.
      Parameters:
      factor - the interpolation factor between x and y.
      w - the value before the first.
      x - the first value.
      y - the second value.
      z - the value after the second.
      Returns:
      the interpolated value.
    • angleInterpolateDegrees

      public static double angleInterpolateDegrees(double factor, double degA, double degB)
      Special angle interpolation. Best used for presentation, not for calculation.
      Parameters:
      factor - interpolation factor.
      degA - the first angle in degrees.
      degB - the second angle in degrees.
      Returns:
      the resultant angle.
    • angleInterpolateRadians

      public static double angleInterpolateRadians(double factor, double radA, double radB)
      Special angle interpolation. Best used for presentation, not for calculation.
      Parameters:
      factor - interpolation factor.
      radA - the first angle in degrees.
      radB - the second angle in degrees.
      Returns:
      the resultant angle.
    • correctClippingAspect

      public static void correctClippingAspect(double targetAspect, double left, double right, double bottom, double top, double[] outBounds)
      Corrects a set of view clipping bounds to fit into a different aspect.
      Parameters:
      targetAspect - the target aspect.
      left - the leftmost clipping bound.
      right - the rightmost clipping bound.
      bottom - the bottom clipping bound.
      top - the top clipping bound.
      outBounds - the output array to put the calculated planes.
    • getInterpolationFactor

      public static double getInterpolationFactor(double value, double lo, double hi)
      Gets a scalar factor that equals how "far along" a value is along an interval.
      Parameters:
      value - the value to test.
      lo - the lower value of the interval.
      hi - the higher value of the interval.
      Returns:
      a value between 0 and 1 describing this distance (0 = beginning or less, 1 = end or greater), or 0 if lo and hi are equal.
    • getVectorAngleDegrees

      public static double getVectorAngleDegrees(double x, double y)
      Returns the angular rotation of a vector described in two dimensions. Result is in degrees.
      Parameters:
      x - the x-component.
      y - the y-component.
      Returns:
      a number in the range [0, 360). 0 is considered to be EAST.
    • getVectorAngleRadians

      public static double getVectorAngleRadians(double x, double y)
      Returns the angular rotation of a vector described in two dimensions. Result is in radians.
      Parameters:
      x - the x-component.
      y - the y-component.
      Returns:
      a number in the range [0, 2*PI). 0 is considered to be EAST.
    • getVectorLength

      public static double getVectorLength(double x, double y)
      Returns the length of a vector by its components.
      Parameters:
      x - the x-component.
      y - the y-component.
      Returns:
      the length of the vector.
    • getVectorLengthSquared

      public static double getVectorLengthSquared(double x, double y)
      Returns the squared length of a vector by its components.
      Parameters:
      x - the x-component.
      y - the y-component.
      Returns:
      the length of the vector.
    • getVectorLength

      public static double getVectorLength(double x, double y, double z)
      Returns the length of a vector by its components.
      Parameters:
      x - the x-component.
      y - the y-component.
      z - the z-component.
      Returns:
      the length of the vector.
    • getVectorLengthSquared

      public static double getVectorLengthSquared(double x, double y, double z)
      Returns the squared length of a vector by its components.
      Parameters:
      x - the x-component.
      y - the y-component.
      z - the z-component.
      Returns:
      the length of the vector.
    • getVectorDotProduct

      public static double getVectorDotProduct(double v1x, double v1y, double v2x, double v2y)
      Returns the dot product of two vectors.
      Parameters:
      v1x - the first vector's x-component.
      v1y - the first vector's y-component.
      v2x - the second vector's x-component.
      v2y - the second vector's y-component.
      Returns:
      the dot product of both vectors.
    • getVectorDotProduct

      public static double getVectorDotProduct(double v1x, double v1y, double v1z, double v2x, double v2y, double v2z)
      Returns the dot product of two vectors.
      Parameters:
      v1x - the first vector's x-component.
      v1y - the first vector's y-component.
      v1z - the first vector's z-component.
      v2x - the second vector's x-component.
      v2y - the second vector's y-component.
      v2z - the second vector's z-component.
      Returns:
      the dot product of both vectors.
    • getVectorUnitDotProduct

      public static double getVectorUnitDotProduct(double v1x, double v1y, double v2x, double v2y)
      Returns the dot product of two vectors, converted to unit vectors first. NOTE: Zero vectors will cause a divide by zero!
      Parameters:
      v1x - the first vector's x-component.
      v1y - the first vector's y-component.
      v2x - the second vector's x-component.
      v2y - the second vector's y-component.
      Returns:
      the dot product of both vectors.
    • getVectorUnitDotProduct

      public static double getVectorUnitDotProduct(double v1x, double v1y, double v1z, double v2x, double v2y, double v2z)
      Returns the dot product of two vectors, converted to unit vectors first. NOTE: Zero vectors will cause a divide by zero!
      Parameters:
      v1x - the first vector's x-component.
      v1y - the first vector's y-component.
      v1z - the first vector's z-component.
      v2x - the second vector's x-component.
      v2y - the second vector's y-component.
      v2z - the second vector's z-component.
      Returns:
      the dot product of both vectors.
    • getAngleDotProductRadians

      public static double getAngleDotProductRadians(double radA, double radB)
      Returns a simulated dot product between two radian angles as though they were unit vectors, rotated.
      Parameters:
      radA - the first angle in radians.
      radB - the second angle in radians.
      Returns:
      the resultant dot product.
    • getAngleDotProductDegrees

      public static double getAngleDotProductDegrees(double degA, double degB)
      Returns a simulated dot product between two degree angles as though they were unit vectors, rotated.
      Parameters:
      degA - the first angle in degrees.
      degB - the second angle in degrees.
      Returns:
      the resultant dot product.
    • getLineLength

      public static double getLineLength(double x0, double y0, double x1, double y1)
      Returns the length of a line by the coordinates of the two points that comprise it.
      Parameters:
      x0 - the first point's x-component.
      y0 - the first point's y-component.
      x1 - the second point's x-component.
      y1 - the second point's y-component.
      Returns:
      the length of the line.
    • getLineLengthSquared

      public static double getLineLengthSquared(double x0, double y0, double x1, double y1)
      Returns the squared length of a line by the coordinates of the two points that comprise it.
      Parameters:
      x0 - the first point's x-component.
      y0 - the first point's y-component.
      x1 - the second point's x-component.
      y1 - the second point's y-component.
      Returns:
      the length of the line.
    • getLineLength

      public static double getLineLength(double x0, double y0, double z0, double x1, double y1, double z1)
      Returns the length of a line by the coordinates of the two points that comprise it.
      Parameters:
      x0 - the first point's x-component.
      y0 - the first point's y-component.
      z0 - the first point's z-component.
      x1 - the second point's x-component.
      y1 - the second point's y-component.
      z1 - the second point's z-component.
      Returns:
      the length of the line.
    • getLineLengthSquared

      public static double getLineLengthSquared(double x0, double y0, double z0, double x1, double y1, double z1)
      Returns the squared length of a line by the coordinates of the two points that comprise it.
      Parameters:
      x0 - the first point's x-component.
      y0 - the first point's y-component.
      z0 - the first point's z-component.
      x1 - the second point's x-component.
      y1 - the second point's y-component.
      z1 - the second point's z-component.
      Returns:
      the length of the line.
    • getAngularDistanceDegrees

      public static double getAngularDistanceDegrees(double angle1, double angle2)
      Returns the difference in degrees between two angles.
      Parameters:
      angle1 - the first angle in DEGREES.
      angle2 - the second angle in DEGREES.
      Returns:
      a number in the range [0, 180]. 0 is an EXACT match.
    • getAngularDistanceRadians

      public static double getAngularDistanceRadians(double angle1, double angle2)
      Returns the difference in radians between two angles.
      Parameters:
      angle1 - the first angle in RADIANS.
      angle2 - the second angle in RADIANS.
      Returns:
      a number in the range [0, Math.PI]. 0 is an EXACT match.
    • getRelativeAngleDegrees

      public static double getRelativeAngleDegrees(double angle1, double angle2)
      Returns the relative angle between two angles.
      Parameters:
      angle1 - the first angle in DEGREES.
      angle2 - the second angle in DEGREES.
      Returns:
      a number in the range [-180, 180]. 0 is an EXACT match.
    • getRelativeAngleRadians

      public static double getRelativeAngleRadians(double angle1, double angle2)
      Returns the relative angle between two angles.
      Parameters:
      angle1 - the first angle in RADIANS.
      angle2 - the second angle in RADIANS.
      Returns:
      a number in the range [-Math.PI, Math.PI]. 0 is an EXACT match.
    • getObservedAngleDegrees

      public static double getObservedAngleDegrees(double viewX, double viewY, double targetX, double targetY, double targetDegrees)
      Returns the observed angle of an object in space facing a direction.
      Parameters:
      viewX - the view origin X-coordinate.
      viewY - the view origin Y-coordinate.
      targetX - the target origin X-coordinate.
      targetY - the target origin Y-coordinate.
      targetDegrees - the target facing degrees. 0 is EAST, 90 is NORTH...
      Returns:
      the observed absolute angle (degrees) from the view.
    • getObservedAngleRadians

      public static double getObservedAngleRadians(double viewX, double viewY, double targetX, double targetY, double targetRadians)
      Returns the observed angle of an object in space facing a direction.
      Parameters:
      viewX - the view origin X-coordinate.
      viewY - the view origin Y-coordinate.
      targetX - the target origin X-coordinate.
      targetY - the target origin Y-coordinate.
      targetRadians - the target facing radians. 0 is EAST, PI/2 is NORTH...
      Returns:
      the observed absolute angle (radians) from the view.
    • getTriangleArea

      public static double getTriangleArea(double ax, double ay, double bx, double by, double cx, double cy)
      Returns the signed area of a triangular area made up of 3 points.
      Parameters:
      ax - the first point, x-coordinate.
      ay - the first point, y-coordinate.
      bx - the second point, x-coordinate.
      by - the second point, y-coordinate.
      cx - the third point, x-coordinate.
      cy - the third point, y-coordinate.
      Returns:
      the calculated area.
    • getTriangleAreaSigned

      public static double getTriangleAreaSigned(double ax, double ay, double bx, double by, double cx, double cy)
      Returns the signed area of a triangular area made up of 3 points.
      Parameters:
      ax - the first point, x-coordinate.
      ay - the first point, y-coordinate.
      bx - the second point, x-coordinate.
      by - the second point, y-coordinate.
      cx - the third point, x-coordinate.
      cy - the third point, y-coordinate.
      Returns:
      the calculated area.
    • getTriangleAreaDoubleSigned

      public static double getTriangleAreaDoubleSigned(double ax, double ay, double bx, double by, double cx, double cy)
      Returns the doubled signed area of a triangular area made up of 3 points.
      Parameters:
      ax - the first point, x-coordinate.
      ay - the first point, y-coordinate.
      bx - the second point, x-coordinate.
      by - the second point, y-coordinate.
      cx - the third point, x-coordinate.
      cy - the third point, y-coordinate.
      Returns:
      the calculated area.
    • getLinePointSide

      public static double getLinePointSide(double ax, double ay, double bx, double by, double px, double py)
      Checks what "side" a point is on a line. Line orientation is "point A looking at point B"
      Parameters:
      ax - the line segment, first point, x-coordinate.
      ay - the line segment, first point, y-coordinate.
      bx - the line segment, second point, x-coordinate.
      by - the line segment, second point, y-coordinate.
      px - the point, x-coordinate.
      py - the point, y-coordinate.
      Returns:
      a scalar value representing the line side (0 is on the line, < 0 is left, > 0 is right).
    • getIntersectionLine

      public static double getIntersectionLine(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy)
      Tests if an intersection occurs between two line segments.
      Parameters:
      ax - the first line segment, first point, x-coordinate.
      ay - the first line segment, first point, y-coordinate.
      bx - the first line segment, second point, x-coordinate.
      by - the first line segment, second point, y-coordinate.
      cx - the second line segment, first point, x-coordinate.
      cy - the second line segment, first point, y-coordinate.
      dx - the second line segment, second point, x-coordinate.
      dy - the second line segment, second point, y-coordinate.
      Returns:
      a scalar value representing how far along the first line segment the intersection occurred, or Double.NaN if no intersection.
    • getIntersectionLinePlane

      public static double getIntersectionLinePlane(double ax, double ay, double az, double bx, double by, double bz, double pnx, double pny, double pnz, double pdist)
      Tests if a line intersects with a plane.
      Parameters:
      ax - the line's first point, x-coordinate.
      ay - the line's first point, y-coordinate.
      az - the line's first point, z-coordinate.
      bx - the line's second point, x-coordinate.
      by - the line's second point, y-coordinate.
      bz - the line's second point, z-coordinate.
      pnx - the unit-vector plane normal, x-component.
      pny - the unit-vector plane normal, y-component.
      pnz - the unit-vector plane normal, z-component.
      pdist - the distance of the plane from the origin (dot product of normal and a point on the plane).
      Returns:
      a scalar value representing how far along the line segment the intersection occurred, or Double.NaN if no intersection.
    • getIntersectionLineCircle

      public static double getIntersectionLineCircle(double ax, double ay, double bx, double by, double ccx, double ccy, double crad)
      Tests if a line segment intersects with a circle.
      Parameters:
      ax - the line segment, first point, x-coordinate.
      ay - the line segment, first point, y-coordinate.
      bx - the line segment, second point, x-coordinate.
      by - the line segment, second point, y-coordinate.
      ccx - the circle center, x-coordinate.
      ccy - the circle center, y-coordinate.
      crad - the circle radius.
      Returns:
      a scalar value representing how far along the line segment the intersection occurred, or Double.NaN if no intersection.
    • getIntersectionLineBox

      public static double getIntersectionLineBox(double ax, double ay, double bx, double by, double bcx, double bcy, double bhw, double bhh)
      Tests if a line segment intersects with a box.
      Parameters:
      ax - the line segment, first point, x-coordinate.
      ay - the line segment, first point, y-coordinate.
      bx - the line segment, second point, x-coordinate.
      by - the line segment, second point, y-coordinate.
      bcx - the box center, x-coordinate.
      bcy - the box center, y-coordinate.
      bhw - the box half width.
      bhh - the box half height.
      Returns:
      a scalar value representing how far along the line segment the intersection occurred, or Double.NaN if no intersection.
    • getIntersectionCircle

      public static boolean getIntersectionCircle(double spx, double spy, double srad, double tpx, double tpy, double trad)
      Returns if two described circles intersect.
      Parameters:
      spx - the first circle center, x-coordinate.
      spy - the first circle center, y-coordinate.
      srad - the first circle radius.
      tpx - the second circle center, x-coordinate.
      tpy - the second circle center, y-coordinate.
      trad - the second circle radius.
      Returns:
      true if so, false if not.
    • getIntersectionCircleBox

      public static boolean getIntersectionCircleBox(double ccx, double ccy, double crad, double bcx, double bcy, double bhw, double bhh)
      Returns if a circle and box intersect.
      Parameters:
      ccx - the circle center, x-coordinate.
      ccy - the circle center, y-coordinate.
      crad - the circle radius.
      bcx - the box center, x-coordinate.
      bcy - the box center, y-coordinate.
      bhw - the box half width.
      bhh - the box half height.
      Returns:
      if an intersection occurred.
    • getIntersectionBox

      public static boolean getIntersectionBox(double spx, double spy, double shw, double shh, double tpx, double tpy, double thw, double thh)
      Tests if two boxes intersect.
      Parameters:
      spx - the first box center, x-coordinate.
      spy - the first box center, y-coordinate.
      shw - the first box half width.
      shh - the first box half height.
      tpx - the second box center, x-coordinate.
      tpy - the second box center, y-coordinate.
      thw - the second box half width.
      thh - the second box half height.
      Returns:
      true if an intersection occurred, false if not.
    • getIntersectionCylinder

      public static boolean getIntersectionCylinder(float sx, float sy, float sz, float srad, float sh, float tx, float ty, float tz, float trad, float th)
      Checks the collision of two cylinders.
      Parameters:
      sx - the first cylinder, origin x-coordinate.
      sy - the first cylinder, origin y-coordinate.
      sz - the first cylinder, origin z-coordinate (base/floor of cylinder).
      srad - the first cylinder radius.
      sh - the first cylinder height.
      tx - the second cylinder, origin x-coordinate.
      ty - the second cylinder, origin y-coordinate.
      tz - the second cylinder, origin z-coordinate (base/floor of cylinder).
      trad - the second cylinder radius.
      th - the second cylinder height.
      Returns:
      true if an intersection occurred, false if not.
    • getIntersectionPoint2DPlane

      public static boolean getIntersectionPoint2DPlane(double ax, double ay, double bx, double by, double px, double py)
      Checks if a point lies behind a 2D plane, represented by a line. Directionality of the line affects the plane - plane normal goes away from the "left" side of the line.
      Parameters:
      ax - the line segment, first point, x-coordinate.
      ay - the line segment, first point, y-coordinate.
      bx - the line segment, second point, x-coordinate.
      by - the line segment, second point, y-coordinate.
      px - the point, x-coordinate.
      py - the point, y-coordinate.
      Returns:
      true if the point is past the plane, false if not.
      See Also:
    • getIntersectionCircle2DPlane

      public static boolean getIntersectionCircle2DPlane(double ax, double ay, double bx, double by, double ccx, double ccy, double crad)
      Checks if a circle breaks a 2D plane, represented by a line. Directionality of the line affects the plane - plane normal goes away from the "left" side of the line.
      Parameters:
      ax - the line segment, first point, x-coordinate.
      ay - the line segment, first point, y-coordinate.
      bx - the line segment, second point, x-coordinate.
      by - the line segment, second point, y-coordinate.
      ccx - the circle centerpoint, x-coordinate.
      ccy - the circle centerpoint, y-coordinate.
      crad - the circle centerpoint, radius.
      Returns:
      true if any part of the circle is past the plane, false if not.
      See Also:
    • getIntersectionBox2DPlane

      public static boolean getIntersectionBox2DPlane(double ax, double ay, double bx, double by, double bcx, double bcy, double bhw, double bhh)
      Checks if an axis-aligned box breaks a 2D plane, represented by a line. Directionality of the line affects the plane - plane normal goes away from the "right" side of the line.
      Parameters:
      ax - the line segment, first point, x-coordinate.
      ay - the line segment, first point, y-coordinate.
      bx - the line segment, second point, x-coordinate.
      by - the line segment, second point, y-coordinate.
      bcx - the box center, x-coordinate.
      bcy - the box center, y-coordinate.
      bhw - the box half width.
      bhh - the box half height.
      Returns:
      true if any part of the box is past the plane, false if not.
      See Also: