Package jmri.util

Class MathUtil

java.lang.Object
jmri.util.MathUtil

public final class MathUtil extends Object
Useful math methods.
  • Field Details

  • Method Details

    • zeroPoint2D

      Returns:
      the point {0, 0}
    • infinityPoint2D

      Returns:
      the point {POSITIVE_INFINITY, POSITIVE_INFINITY}
    • gcd

      public static int gcd(int a, int b)
      Parameters:
      a - the first number
      b - the second number
      Returns:
      the greatest common divisor of a and b
    • pointToPoint2D

      Convert Point to Point2D.
      Parameters:
      p - the Point
      Returns:
      the Point2D
    • point2DToPoint

      Convert Point2D to Point.
      Parameters:
      p - the Point
      Returns:
      the Point2D
    • equals

      public static boolean equals(float a, float b)
      Parameters:
      a - the first float
      b - the second float
      Returns:
      true if a is equal to b
    • equals

      public static boolean equals(double a, double b)
      Parameters:
      a - the first double
      b - the second double
      Returns:
      true if a is equal to b
    • equals

      public static boolean equals(Rectangle2D a, Rectangle2D b)
      Parameters:
      a - the first Rectangle2D
      b - the second Rectangle2D
      Returns:
      true if a is equal to b
    • equals

      public static boolean equals(Point2D a, Point2D b)
      Parameters:
      a - the first Point2D
      b - the second Point2D
      Returns:
      true if a is equal to b
    • isEqualToZeroPoint2D

      public static boolean isEqualToZeroPoint2D(@Nonnull Point2D p)
      Parameters:
      p - the point
      Returns:
      true if p1 is equal to zeroPoint2D
    • min

      Get the minimum coordinates of two points.
      Parameters:
      pA - the first point
      pB - the second point
      Returns:
      the minimum coordinates
    • max

      Get the maximum coordinates of two points.
      Parameters:
      pA - the first point
      pB - the second point
      Returns:
      the maximum coordinates
    • pin

      Get the coordinates of a point pinned between two other points.
      Parameters:
      pA - the first point
      pB - the second point
      pC - the third point
      Returns:
      the coordinates of pA pined between pB and pC
    • pin

      Get the coordinates of a point pinned in a rectangle
      Parameters:
      pA - the point
      pR - the rectangle
      Returns:
      the coordinates of point pA pined in rectangle pR
    • add

      Add two points.
      Parameters:
      pA - the first point
      pB - the second point
      Returns:
      the sum of the two points
    • subtract

      Subtract two points.
      Parameters:
      pA - the first point
      pB - the second point
      Returns:
      the difference of the two points
    • multiply

      @CheckReturnValue public static Point2D multiply(@Nonnull Point2D p, double s)
      Multiply a point times a scalar.
      Parameters:
      p - the point
      s - the scalar
      Returns:
      the point multiplied by the scalar
    • multiply

      @CheckReturnValue public static Point2D multiply(@Nonnull Point2D p, double x, double y)
      Multiply a point times two scalar.
      Parameters:
      p - the point
      x - the X scalar
      y - the Y scalar
      Returns:
      the point multiplied by the two scalars
    • multiply

      public static Point2D multiply(double s, @Nonnull Point2D p)
      Multiply a scalar times a point.
      Parameters:
      s - the scalar
      p - the point
      Returns:
      the point multiplied by the scalar
    • multiply

      Multiply a point times a point.
      Parameters:
      p1 - the first point
      p2 - the second point
      Returns:
      the first point multiplied by the second
    • divide

      @CheckReturnValue public static Point2D divide(@Nonnull Point2D p, double s)
      Divide a point by a scalar.
      Parameters:
      p - the point
      s - the scalar
      Returns:
      the point divided by the scalar
    • divide

      @CheckReturnValue public static Point2D divide(@Nonnull Point2D p, double x, double y)
      Divide a point by two scalars.
      Parameters:
      p - the point
      x - the X scalar
      y - the Y scalar
      Returns:
      the point divided by the scalar
    • offset

      @CheckReturnValue public static Point2D offset(@Nonnull Point2D p, double x, double y)
      Offset a point by two scalars.
      Parameters:
      p - the point
      x - the x scalar
      y - the y scalar
      Returns:
      the point offset by the scalars
    • rotateRAD

      @CheckReturnValue public static Point2D rotateRAD(double x, double y, double a)
      Rotate x and y coordinates (by radians).
      Parameters:
      x - the x coordinate
      y - the y coordinate
      a - the angle (in radians)
      Returns:
      the point rotated by the angle
    • rotateDEG

      @CheckReturnValue public static Point2D rotateDEG(double x, double y, double a)
      Rotate x and y coordinates (by degrees).
      Parameters:
      x - the x coordinate
      y - the y coordinate
      a - the angle (in radians)
      Returns:
      the point rotated by the angle
    • rotateRAD

      @CheckReturnValue public static Point2D rotateRAD(@Nonnull Point2D p, double a)
      Rotate a point (by radians).
      Parameters:
      p - the point
      a - the angle (in radians)
      Returns:
      the point rotated by the angle
    • rotateDEG

      @CheckReturnValue public static Point2D rotateDEG(@Nonnull Point2D p, double a)
      Rotate a point (by degrees).
      Parameters:
      p - the point
      a - the angle (in radians)
      Returns:
      the point rotated by the angle
    • rotateRAD

      @CheckReturnValue public static Point2D rotateRAD(@Nonnull Point2D p, @Nonnull Point2D c, double aRAD)
      Rotate a point around another point (by radians).
      Parameters:
      p - the point being rotated
      c - the point its being rotated around
      aRAD - the angle (in radians)
      Returns:
      the point rotated by the angle
    • rotateDEG

      @CheckReturnValue public static Point2D rotateDEG(@Nonnull Point2D p, @Nonnull Point2D c, double aDEG)
      Rotate a point around another point (by degrees).
      Parameters:
      p - the point being rotated
      c - the point its being rotated around
      aDEG - the angle (in radians)
      Returns:
      the point rotated by the angle
    • orthogonal

      public static Point2D orthogonal(@Nonnull Point2D p)
      Parameters:
      p - the point
      Returns:
      the point orthogonal to this one (relative to {0, 0})
    • vectorDEG

      @CheckReturnValue public static Point2D vectorDEG(double dirDEG, double magnitude)
      Create a vector given a direction and a magnitude.
      Parameters:
      dirDEG - the direction (in degrees)
      magnitude - the magnitude
      Returns:
      the vector with the specified direction and magnitude
    • vectorRAD

      @CheckReturnValue public static Point2D vectorRAD(double dirRAD, double magnitude)
      Create a vector given a direction and a magnitude.
      Parameters:
      dirRAD - the direction (in radians)
      magnitude - the magnitude
      Returns:
      the vector with the specified direction and magnitude
    • dot

      @CheckReturnValue public static double dot(@Nonnull Point2D pA, @Nonnull Point2D pB)
      Dot product of two points (vectors).
      Parameters:
      pA - the first point
      pB - the second point
      Returns:
      the dot product of the two points note: Arccos(x) (inverse cosine) of dot product is the angle between the vectors
    • lengthSquared

      @CheckReturnValue public static double lengthSquared(@Nonnull Point2D p)
      Calculate the length squared of a point (vector).
      Parameters:
      p - the point (vector)
      Returns:
      the length squared of the point (vector)
    • length

      @CheckReturnValue public static double length(@Nonnull Point2D p)
      Calculate the length of a point (vector).
      Parameters:
      p - the point (vector)
      Returns:
      the length of the point (vector)
    • distance

      @CheckReturnValue public static double distance(@Nonnull Point2D pA, @Nonnull Point2D pB)
      Calculate the distance between two points.
      Parameters:
      pA - the first point
      pB - the second point
      Returns:
      the distance between the two points
    • normalize

      @CheckReturnValue public static Point2D normalize(@Nonnull Point2D p, double length)
      Normalize a point (vector) to a length.
      Parameters:
      p - the point (vector)
      length - the length to normalize to
      Returns:
      the normalized point (vector)
    • normalize

      Normalize a point (vector).
      Parameters:
      p - the point (vector)
      Returns:
      the normalized point (vector)
    • computeAngleRAD

      Compute the angle (direction in radians) for a vector.
      Parameters:
      p - the vector (point relative to zeroPoint2D)
      Returns:
      the angle in radians
    • computeAngleDEG

      Compute the angle (direction in degrees) for a vector.
      Parameters:
      p - the vector (point relative to zeroPoint2D)
      Returns:
      the angle in degrees
    • computeAngleRAD

      Compute the angle (direction in radians) from point 1 to point 2.

      Note: Goes CCW from south to east to north to west, etc. For JMRI subtract from PI/2 to get east, south, west, north

      Parameters:
      p1 - the first Point2D
      p2 - the second Point2D
      Returns:
      the angle in radians
    • computeAngleDEG

      Compute the angle (direction in degrees) from point 1 to point 2.

      Note: Goes CCW from south to east to north to west, etc. For JMRI subtract from 90.0 to get east, south, west, north

      Parameters:
      p1 - the first Point2D
      p2 - the second Point2D
      Returns:
      the angle in degrees
    • lerp

      public static int lerp(int a, int b, double t)
      Calculate the linear interpolation between two integers.
      Parameters:
      a - the first number
      b - the second number
      t - the fraction (between 0 and 1)
      Returns:
      the linear interpolation between a and b for t
    • lerp

      @CheckReturnValue public static double lerp(double a, double b, double t)
      Calculate the linear interpolation between two doubles.
      Parameters:
      a - the first number
      b - the second number
      t - the fraction (between 0 and 1)
      Returns:
      the linear interpolation between a and b for t
    • lerp

      Calculate the linear interpolation between two Doubles.
      Parameters:
      a - the first number
      b - the second number
      t - the fraction (between 0 and 1)
      Returns:
      the linear interpolation between a and b for t
    • lerp

      @CheckReturnValue public static Point2D lerp(@Nonnull Point2D pA, @Nonnull Point2D pB, double t)
      Calculate the linear interpolation between two points.
      Parameters:
      pA - the first point
      pB - the second point
      t - the fraction (between 0 and 1)
      Returns:
      the linear interpolation between a and b for t
    • granulize

      @CheckReturnValue public static double granulize(double v, double g)
      Round value to granular increment.
      Parameters:
      v - the value to granulize
      g - the granularity
      Returns:
      the value granulized to the granularity
    • granulize

      @CheckReturnValue public static Point2D granulize(@Nonnull Point2D p, double gH, double gV)
      Round point to horizontal and vertical granular increments.
      Parameters:
      p - the point to granulize
      gH - the horizontal granularity
      gV - the vertical granularity
      Returns:
      the point granulized to the granularity
    • granulize

      @CheckReturnValue public static Point2D granulize(@Nonnull Point2D p, double g)
      Round point to granular increment.
      Parameters:
      p - the point to granulize
      g - the granularity
      Returns:
      the point granulized to the granularity
    • granulize

      Round Rectangle2D to granular increment.
      Parameters:
      r - the rectangle to granulize
      g - the granularity
      Returns:
      the rectangle granulized to the granularity
    • midPoint

      Calculate the midpoint between two points.
      Parameters:
      pA - the first point
      pB - the second point
      Returns:
      the midpoint between the two points
    • oneThirdPoint

      Calculate the point 1/3 of the way between two points.
      Parameters:
      pA - the first point
      pB - the second point
      Returns:
      the point one third of the way from pA to pB
    • twoThirdsPoint

      Calculate the point 2/3 of the way between two points.
      Parameters:
      pA - the first point
      pB - the second point
      Returns:
      the point two thirds of the way from pA to pB
    • oneFourthPoint

      Calculate the point 1/4 of the way between two points.
      Parameters:
      pA - the first point
      pB - the second point
      Returns:
      the point one fourth of the way from pA to pB
    • threeFourthsPoint

      Calculate the point 3/4 of the way between two points.
      Parameters:
      pA - the first point
      pB - the second point
      Returns:
      the point three fourths of the way from pA to pB
    • wrap

      public static int wrap(int inValue, int inMin, int inMax)
      Wrap an int between two values (for example +/- 180 or 0-360 degrees).
      Parameters:
      inValue - the value
      inMin - the lowest value
      inMax - the highest value
      Returns:
      the value wrapped between the lowest and highest values Note: THIS IS NOT A PIN OR TRUNCATE; VALUES WRAP AROUND BETWEEN MIN AND MAX (And yes, this works correctly with negative numbers)
    • wrap

      @CheckReturnValue public static double wrap(double inValue, double inMin, double inMax)
      Wrap a double between two values (for example +/- 180 or 0-360 degrees).
      Parameters:
      inValue - the value
      inMin - the lowest value
      inMax - the highest value
      Returns:
      the value wrapped between the lowest and highest values Note: THIS IS NOT A PIN OR TRUNCATE; VALUES WRAP AROUND BETWEEN MIN AND MAX (And yes, this works correctly with negative numbers)
    • wrapPM180

      @CheckReturnValue public static double wrapPM180(double inValue)
      Wrap a value between +/-180.
      Parameters:
      inValue - the value
      Returns:
      the value wrapped between -180 and +180
    • wrapPM360

      @CheckReturnValue public static double wrapPM360(double inValue)
      Wrap a value between +/-360.
      Parameters:
      inValue - the value
      Returns:
      the value wrapped between -360 and +360
    • wrap360

      @CheckReturnValue public static double wrap360(double inValue)
      Wrap a value between 0 and 360.
      Parameters:
      inValue - the value
      Returns:
      the value wrapped between -360 and +360
    • normalizeAngleDEG

      @CheckReturnValue public static double normalizeAngleDEG(double a)
      Wrap an angle between 0 and 360.
      Parameters:
      a - the angle
      Returns:
      the angle wrapped between 0 and 360
    • diffAngleDEG

      @CheckReturnValue public static double diffAngleDEG(double a, double b)
      Calculate the relative difference (+/-180) between two angles.
      Parameters:
      a - the first angle
      b - the second angle
      Returns:
      the relative difference between the two angles (in degrees)
    • absDiffAngleDEG

      @CheckReturnValue public static double absDiffAngleDEG(double a, double b)
      Calculate the absolute difference (0-180) between two angles.
      Parameters:
      a - the first angle
      b - the second angle
      Returns:
      the absolute difference between the two angles (in degrees)
    • diffAngleRAD

      @CheckReturnValue public static double diffAngleRAD(double a, double b)
      Calculate the relative difference (+/-PI) between two angles.
      Parameters:
      a - the first angle
      b - the second angle
      Returns:
      the relative difference between the two angles (in radians)
    • absDiffAngleRAD

      @CheckReturnValue public static double absDiffAngleRAD(double a, double b)
      Calculate the absolute difference (0-PI) between two angles.
      Parameters:
      a - the first angle
      b - the second angle
      Returns:
      the absolute difference between the two angles (in radians)
    • pin

      public static int pin(int inValue, int inMin, int inMax)
      Pin a value between min and max.
      Parameters:
      inValue - the value
      inMin - the min
      inMax - the max
      Returns:
      the value pinned between the min and max values
    • pin

      @CheckReturnValue public static double pin(double inValue, double inMin, double inMax)
      Pin a value between min and max.
      Parameters:
      inValue - the value
      inMin - the min
      inMax - the max
      Returns:
      the value pinned between the min and max values
    • zeroRectangle2D

      Returns:
      a new rectangle {0.0, 0.0, 0.0, 0.0}
    • zeroToInfinityRectangle2D

      Returns:
      a new rectangle {0.0, 0.0, POSITIVE_INFINITY, POSITIVE_INFINITY}
    • infinityRectangle2D

      Returns:
      a new rectangle {NEGATIVE_INFINITY, NEGATIVE_INFINITY, POSITIVE_INFINITY, POSITIVE_INFINITY}
    • rectangle2DToString

      Return an I18N string to represent a rectangle.
      Parameters:
      r - the rectangle2D
      Returns:
      the string
    • rectangleToRectangle2D

      Convert Rectangle to Rectangle2D.
      Parameters:
      r - the Rectangle
      Returns:
      the Rectangle2D
    • rectangle2DToRectangle

      Convert Rectangle2D to Rectangle.
      Parameters:
      r - the Rectangle
      Returns:
      the Rectangle2D
    • getOrigin

      Get the origin (top left) of the rectangle.
      Parameters:
      r - the rectangle
      Returns:
      the origin of the rectangle
    • setOrigin

      Set the origin (top left) of the rectangle.
      Parameters:
      r - the rectangle
      origin - the origin
      Returns:
      a new rectangle with the new origin
    • dimensionToString

      An I18N string to represent a Dimension.
      Parameters:
      d - the Dimension
      Returns:
      the string
    • getSize

      Get the size of a rectangle.
      Parameters:
      r - the rectangle
      Returns:
      the size of the rectangle
    • setSize

      Set the size of a rectangle
      Parameters:
      r - the rectangle
      d - the new size (as dimension)
      Returns:
      a new rectangle with the new size
    • setSize

      Set the size of a rectangle
      Parameters:
      r - the rectangle
      s - the new size (as Point2D)
      Returns:
      a new rectangle with the new size
    • center

      Calculate the center of the rectangle.
      Parameters:
      r - the rectangle
      Returns:
      the center of the rectangle
    • midPoint

      Calculate the midpoint of the rectangle.
      Parameters:
      r - the rectangle
      Returns:
      the midpoint of the rectangle
    • offset

      @CheckReturnValue public static Rectangle2D offset(@Nonnull Rectangle2D r, double x, double y)
      Offset a rectangle by distinct x,y values.
      Parameters:
      r - the rectangle
      x - the horizontal offset
      y - the vertical offset
      Returns:
      the offset rectangle
    • offset

      Offset a rectangle by a single value.
      Parameters:
      r - the rectangle
      o - the offset
      Returns:
      the offset rectangle
    • inset

      @CheckReturnValue public static Rectangle2D inset(@Nonnull Rectangle2D r, double i)
      Inset a rectangle by a single value.
      Parameters:
      r - the rectangle
      i - the inset (positive make it smaller, negative, bigger)
      Returns:
      the inset rectangle
    • inset

      @CheckReturnValue public static Rectangle2D inset(@Nonnull Rectangle2D r, double h, double v)
      Inset a rectangle by distinct x,y values.
      Parameters:
      r - the rectangle
      h - the horzontial inset (positive make it smaller, negative, bigger)
      v - the vertical inset (positive make it smaller, negative, bigger)
      Returns:
      the inset rectangle
    • scale

      @CheckReturnValue public static Rectangle2D scale(@Nonnull Rectangle2D r, double s)
      Scale a rectangle.
      Parameters:
      r - the rectangle
      s - the scale
      Returns:
      the scaled rectangle
    • centerRectangleOnPoint

      Center rectangle on point.
      Parameters:
      r - the rectangle
      p - the point
      Returns:
      the Point2D
    • centerRectangleOnRectangle

      Center rectangle on rectangle.
      Parameters:
      r1 - the first rectangle
      r2 - the second rectangle
      Returns:
      the first rectangle centered on the second
    • rectangleAtPoint

      Get rectangle at point.
      Parameters:
      p - the point
      width - the width
      height - the height
      Returns:
      the rectangle
    • drawBezier

      Draw a cubic Bezier curve.
      Parameters:
      g2 - the Graphics2D context to draw to (null if just want length)
      p0 - origin control point
      p1 - first control point
      p2 - second control point
      p3 - terminating control point
      Returns:
      the length of the Bezier curve
    • getBezierPath

      public static GeneralPath getBezierPath(@Nonnull Point2D[] p, double displacement)
      Get the path for a Bezier curve.
      Parameters:
      p - control points
      displacement - right/left to draw a line parallel to the Bezier
      Returns:
      the length of the Bezier curve
    • getBezierPath

      Get the path for a Bezier curve.
      Parameters:
      p - control points
      Returns:
      the length of the Bezier curve
    • drawBezier

      public static double drawBezier(@CheckForNull Graphics2D g2, @Nonnull Point2D[] p, double displacement)
      Draw a Bezier curve
      Parameters:
      g2 - the Graphics2D context to draw to (null to just return length)
      p - the control points
      displacement - right/left to draw a line parallel to the Bezier
      Returns:
      the length of the Bezier curve
    • fillBezier

      public static double fillBezier(@CheckForNull Graphics2D g2, @Nonnull Point2D[] p, double displacement)
      Fill a Bezier curve.
      Parameters:
      g2 - the Graphics2D context to draw to
      p - the control points
      displacement - right/left to draw a line parallel to the Bezier
      Returns:
      the length of the Bezier curve
    • drawBezier

      public static double drawBezier(@CheckForNull Graphics2D g2, @Nonnull Point2D[] p)
      Draw a Bezier curve.
      Parameters:
      g2 - the Graphics2D context to draw to (null to just return length)
      p - the control points
      Returns:
      the length of the Bezier curve
    • fillBezier

      public static double fillBezier(@CheckForNull Graphics2D g2, @Nonnull Point2D[] p)
      Fill a Bezier curve.
      Parameters:
      g2 - the Graphics2D context to draw to (null if just want length)
      p - the control points
      Returns:
      the length of the Bezier curve
    • getBezierBounds

      computer the bounds of a Bezier curve.
      Parameters:
      p - the control points
      Returns:
      the bounds of the Bezier curve
    • intersect

      Find intersection of two lines.
      Parameters:
      p1 - the first point on the first line
      p2 - the second point on the first line
      p3 - the first point on the second line
      p4 - the second point on the second line
      Returns:
      the intersection point of the two lines or null if one doesn't exist
    • distance

      public static double distance(@Nonnull Point2D p1, @Nonnull Point2D p2, @Nonnull Point2D p3)
      get (signed) distance p3 is from line segment defined by p1 and p2
      Parameters:
      p1 - the first point on the line segment
      p2 - the second point on the line segment
      p3 - the point whose distance from the line segment you wish to calculate
      Returns:
      the distance (note: plus/minus determines the (left/right) side of the line)
    • midPoint

      public static Point2D midPoint(List<Point2D> points)
      return average point in points
      Parameters:
      points - to average
      Returns:
      the average point
    • isPointInPolygon

      public static boolean isPointInPolygon(Point2D pointT, List<Point2D> points)
      Parameters:
      pointT - the point
      points - the polygon
      Returns:
      true if pointT is in the polygon made up of the points
    • convexHull

      public static List<Point2D> convexHull(List<Point2D> points)
      compute convex hull (outline of polygon)
      Parameters:
      points - of the polygon
      Returns:
      points of the convex hull
    • isCounterClockWise

      public static boolean isCounterClockWise(Point2D a, Point2D b, Point2D c)
      isCounterClockWise
      Parameters:
      a - the first point
      b - the second point
      c - the third point
      Returns:
      true if the three points make a counter-clockwise turn