Class MathUtils

java.lang.Object
org.pepsoft.util.MathUtils

public final class MathUtils extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    clamp(double min, double value, double max)
     
    static float
    clamp(float min, float value, float max)
     
    static int
    clamp(int min, int value, int max)
     
    static long
    clamp(long min, long value, long max)
     
    static double
    distanceToLineSegment(int px, int py, int vx, int vy, int wx, int wy)
    Calculates the shortest distance of a point to a line segment.
    static double
    getDistance(double dx, double dy, double dz)
    Calculates the distance between two points in 3D space.
    static float
    getDistance(float dx, float dy)
    Calculates the distance between two points in 2D space.
    static float
    getDistance(float dx, float dy, float dz)
    Calculates the distance between two points in 3D space.
    static float
    getDistance(int dx, int dy)
    Calculates the distance between two points.
    static float
    getDistance(int dx, int dy, int dz)
     
    static float
    getDistance(int x1, int y1, int x2, int y2)
    Calculates the distance between two points in two dimensional space.
    static float
    getDistance(int x1, int y1, int z1, int x2, int y2, int z2)
    Calculates the distance between two points in three dimensional space.
    static double
    mod(double a, double b)
    Calculates x modulo y.
    static float
    mod(float a, float b)
    Calculates x modulo y.
    static int
    mod(int a, int b)
    Calculates x modulo y.
    static int
    pow(int x, int y)
    Positive integer powers.
    static javax.vecmath.Vector3d
    rotateVectorCC(javax.vecmath.Vector3d vector, javax.vecmath.Vector3d axis, double angle)
    Rotate a vector counterclockwise around an axis.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • pow

      public static int pow(int x, int y)
      Positive integer powers.
    • mod

      public static float mod(float a, float b)
      Calculates x modulo y. This is different than the Java remainder operator (%) in that it always returns a positive value.
      Parameters:
      a - The operand.
      b - The modulus.
      Returns:
      x modulo y
    • mod

      public static double mod(double a, double b)
      Calculates x modulo y. This is different than the Java remainder operator (%) in that it always returns a positive value.
      Parameters:
      a - The operand.
      b - The modulus.
      Returns:
      x modulo y
    • mod

      public static int mod(int a, int b)
      Calculates x modulo y. This is different than the Java remainder operator (%) in that it always returns a positive value.
      Parameters:
      a - The operand.
      b - The modulus.
      Returns:
      x modulo y
    • getDistance

      public static float getDistance(int x1, int y1, int x2, int y2)
      Calculates the distance between two points in two dimensional space. Uses a lookup table for distances below 300 for speed.
      Parameters:
      x1 - The X coordinate of the first point.
      y1 - The Y coordinate of the first point.
      x2 - The X coordinate of the second point.
      y2 - The Y coordinate of the second point.
      Returns:
      The distance between the two points.
    • getDistance

      public static float getDistance(int x1, int y1, int z1, int x2, int y2, int z2)
      Calculates the distance between two points in three dimensional space. Uses a lookup table for distances below 50 for speed.
      Parameters:
      x1 - The X coordinate of the first point.
      y1 - The Y coordinate of the first point.
      z1 - The Z coordinate of the first point.
      x2 - The X coordinate of the second point.
      y2 - The Y coordinate of the second point.
      z2 - The Z coordinate of the second point.
      Returns:
      The distance between the two points.
    • getDistance

      public static float getDistance(int dx, int dy)
      Calculates the distance between two points. Uses a lookup table for distances below 300 for speed.
      Parameters:
      dx - The difference along the x axis between the two points.
      dy - The difference along the y axis between the two points.
      Returns:
      The distance between the two points.
    • getDistance

      public static float getDistance(float dx, float dy)
      Calculates the distance between two points in 2D space.
      Parameters:
      dx - The difference along the x axis between the two points.
      dy - The difference along the y axis between the two points.
      Returns:
      The distance between the two points.
    • getDistance

      public static float getDistance(float dx, float dy, float dz)
      Calculates the distance between two points in 3D space.
      Parameters:
      dx - The difference along the x axis between the two points.
      dy - The difference along the y axis between the two points.
      dz - The difference along the z axis between the two points.
      Returns:
      The distance between the two points.
    • getDistance

      public static double getDistance(double dx, double dy, double dz)
      Calculates the distance between two points in 3D space.
      Parameters:
      dx - The difference along the x axis between the two points.
      dy - The difference along the y axis between the two points.
      dz - The difference along the z axis between the two points.
      Returns:
      The distance between the two points.
    • getDistance

      public static float getDistance(int dx, int dy, int dz)
    • clamp

      public static int clamp(int min, int value, int max)
    • clamp

      public static long clamp(long min, long value, long max)
    • clamp

      public static float clamp(float min, float value, float max)
    • clamp

      public static double clamp(double min, double value, double max)
    • rotateVectorCC

      public static javax.vecmath.Vector3d rotateVectorCC(javax.vecmath.Vector3d vector, javax.vecmath.Vector3d axis, double angle)
      Rotate a vector counterclockwise around an axis.
      Parameters:
      vector - The vector to rotate.
      axis - The axis around which to rotate it.
      angle - How many radians to rotate it counterclockwise.
      Returns:
      The rotated vector.
    • distanceToLineSegment

      public static double distanceToLineSegment(int px, int py, int vx, int vy, int wx, int wy)
      Calculates the shortest distance of a point to a line segment.
      Parameters:
      px - The X coordinate of the point.
      py - The Y coordinate of the point.
      vx - The X coordinate of the start of the line segment.
      vy - The Y coordinate of the start of the line segment.
      wx - The X coordinate of the end of the line segment.
      wy - The Y coordinate of the end of the line segment.
      Returns:
      The shortest distance of the specified point to the specified line segment.