Package jmri.util
Class MathUtil
- java.lang.Object
-
- jmri.util.MathUtil
-
public final class MathUtil extends java.lang.Object
Useful math methods.
-
-
Field Summary
Fields Modifier and Type Field Description static java.awt.geom.Point2D
infinityPoint2D
static java.awt.geom.Rectangle2D
infinityRectangle2D
static java.awt.geom.Point2D
zeroPoint2D
static java.awt.geom.Rectangle2D
zeroRectangle2D
static java.awt.geom.Rectangle2D
zeroToInfinityRectangle2D
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static double
absDiffAngleDEG(double a, double b)
Calculate the absolute difference (0-180) between two angles.static double
absDiffAngleRAD(double a, double b)
Calculate the absolute difference (0-PI) between two angles.static java.awt.geom.Point2D
add(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB)
Add two points.static java.awt.geom.Point2D
center(java.awt.geom.Rectangle2D r)
Calculate the center of the rectangle.static java.awt.geom.Rectangle2D
centerRectangleOnPoint(java.awt.geom.Rectangle2D r, java.awt.geom.Point2D p)
Center rectangle on point.static java.awt.geom.Rectangle2D
centerRectangleOnRectangle(java.awt.geom.Rectangle2D r1, java.awt.geom.Rectangle2D r2)
Center rectangle on rectangle.static double
computeAngleDEG(java.awt.geom.Point2D p)
Compute the angle (direction in degrees) for a vector.static double
computeAngleDEG(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)
Compute the angle (direction in degrees) from point 1 to point 2.static double
computeAngleRAD(java.awt.geom.Point2D p)
Compute the angle (direction in radians) for a vector.static double
computeAngleRAD(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)
Compute the angle (direction in radians) from point 1 to point 2.static java.util.List<java.awt.geom.Point2D>
convexHull(java.util.List<java.awt.geom.Point2D> points)
compute convex hull (outline of polygon)static double
diffAngleDEG(double a, double b)
Calculate the relative difference (+/-180) between two angles.static double
diffAngleRAD(double a, double b)
Calculate the relative difference (+/-PI) between two angles.static java.lang.String
dimensionToString(java.awt.Dimension d)
dimensionToString return a string to represent a Dimensionstatic double
distance(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB)
Calculate the distance between two points.static double
distance(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2, java.awt.geom.Point2D p3)
get (signed) distance p3 is from line segment defined by p1 and p2static java.awt.geom.Point2D
divide(java.awt.geom.Point2D p, double s)
Divide a point by a scalar.static java.awt.geom.Point2D
divide(java.awt.geom.Point2D p, double x, double y)
Divide a point by two scalars.static double
dot(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB)
Dot product of two points (vectors).static double
drawBezier(java.awt.Graphics2D g2, java.awt.geom.Point2D[] p)
Draw a Bezier curve.static double
drawBezier(java.awt.Graphics2D g2, java.awt.geom.Point2D[] p, double displacement)
Draw a Bezier curvestatic double
drawBezier(java.awt.Graphics2D g2, java.awt.geom.Point2D p0, java.awt.geom.Point2D p1, java.awt.geom.Point2D p2, java.awt.geom.Point2D p3)
Draw a cubic Bezier curve.static boolean
equals(double a, double b)
static boolean
equals(float a, float b)
static boolean
equals(java.awt.geom.Point2D a, java.awt.geom.Point2D b)
static boolean
equals(java.awt.geom.Rectangle2D a, java.awt.geom.Rectangle2D b)
static double
fillBezier(java.awt.Graphics2D g2, java.awt.geom.Point2D[] p)
Fill a Bezier curve.static double
fillBezier(java.awt.Graphics2D g2, java.awt.geom.Point2D[] p, double displacement)
Fill a Bezier curve.static int
gcd(int a, int b)
static java.awt.geom.Rectangle2D
getBezierBounds(java.awt.geom.Point2D[] p)
computer the bounds of a Bezier curve.static java.awt.geom.GeneralPath
getBezierPath(java.awt.geom.Point2D[] p)
Get the path for a Bezier curve.static java.awt.geom.GeneralPath
getBezierPath(java.awt.geom.Point2D[] p, double displacement)
Get the path for a Bezier curve.static java.awt.geom.Point2D
getOrigin(java.awt.geom.Rectangle2D r)
Get the origin (top left) of the rectangle.static java.awt.Dimension
getSize(java.awt.geom.Rectangle2D r)
Get the size of a rectangle.static double
granulize(double v, double g)
Round value to granular increment.static java.awt.geom.Point2D
granulize(java.awt.geom.Point2D p, double g)
Round point to granular increment.static java.awt.geom.Point2D
granulize(java.awt.geom.Point2D p, double gH, double gV)
Round point to horizontal and vertical granular increments.static java.awt.geom.Rectangle2D
granulize(java.awt.geom.Rectangle2D r, double g)
Round Rectangle2D to granular increment.static java.awt.geom.Point2D
infinityPoint2D()
static java.awt.geom.Rectangle2D
infinityRectangle2D()
static java.awt.geom.Rectangle2D
inset(java.awt.geom.Rectangle2D r, double i)
Inset a rectangle by a single value.static java.awt.geom.Rectangle2D
inset(java.awt.geom.Rectangle2D r, double h, double v)
Inset a rectangle by distinct x,y values.static java.awt.geom.Point2D
intersect(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2, java.awt.geom.Point2D p3, java.awt.geom.Point2D p4)
Find intersection of two lines.static boolean
isCounterClockWise(java.awt.geom.Point2D a, java.awt.geom.Point2D b, java.awt.geom.Point2D c)
isCounterClockWisestatic boolean
isEqualToZeroPoint2D(java.awt.geom.Point2D p)
static boolean
isPointInPolygon(java.awt.geom.Point2D pointT, java.util.List<java.awt.geom.Point2D> points)
static double
length(java.awt.geom.Point2D p)
Calculate the length of a point (vector).static double
lengthSquared(java.awt.geom.Point2D p)
Calculate the length squared of a point (vector).static double
lerp(double a, double b, double t)
Calculate the linear interpolation between two doubles.static int
lerp(int a, int b, double t)
Calculate the linear interpolation between two integers.static java.awt.geom.Point2D
lerp(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB, double t)
Calculate the linear interpolation between two points.static java.lang.Double
lerp(java.lang.Double a, java.lang.Double b, java.lang.Double t)
Calculate the linear interpolation between two Doubles.static java.awt.geom.Point2D
max(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB)
Get the maximum coordinates of two points.static java.awt.geom.Point2D
midPoint(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB)
Calculate the midpoint between two points.static java.awt.geom.Point2D
midPoint(java.awt.geom.Rectangle2D r)
Calculate the midpoint of the rectangle.static java.awt.geom.Point2D
midPoint(java.util.List<java.awt.geom.Point2D> points)
return average point in pointsstatic java.awt.geom.Point2D
min(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB)
Get the minimum coordinates of two points.static java.awt.geom.Point2D
multiply(double s, java.awt.geom.Point2D p)
Multiply a scalar times a point.static java.awt.geom.Point2D
multiply(java.awt.geom.Point2D p, double s)
Multiply a point times a scalar.static java.awt.geom.Point2D
multiply(java.awt.geom.Point2D p, double x, double y)
Multiply a point times two scalar.static java.awt.geom.Point2D
multiply(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)
Multiply a point times a point.static java.awt.geom.Point2D
normalize(java.awt.geom.Point2D p)
Normalize a point (vector).static java.awt.geom.Point2D
normalize(java.awt.geom.Point2D p, double length)
Normalize a point (vector) to a length.static double
normalizeAngleDEG(double a)
Wrap an angle between 0 and 360.static java.awt.geom.Point2D
offset(java.awt.geom.Point2D p, double x, double y)
Offset a point by two scalars.static java.awt.geom.Rectangle2D
offset(java.awt.geom.Rectangle2D r, double x, double y)
Offset a rectangle by distinct x,y values.static java.awt.geom.Rectangle2D
offset(java.awt.geom.Rectangle2D r, java.awt.geom.Point2D o)
Offset a rectangle by a single value.static java.awt.geom.Point2D
oneFourthPoint(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB)
Calculate the point 1/4 of the way between two points.static java.awt.geom.Point2D
oneThirdPoint(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB)
Calculate the point 1/3 of the way between two points.static java.awt.geom.Point2D
orthogonal(java.awt.geom.Point2D p)
static double
pin(double inValue, double inMin, double inMax)
Pin a value between min and max.static int
pin(int inValue, int inMin, int inMax)
Pin a value between min and max.static java.awt.geom.Point2D
pin(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB, java.awt.geom.Point2D pC)
Get the coordinates of a point pinned between two other points.static java.awt.geom.Point2D
pin(java.awt.geom.Point2D pA, java.awt.geom.Rectangle2D pR)
Get the coordinates of a point pinned in a rectanglestatic java.awt.Point
point2DToPoint(java.awt.geom.Point2D p)
Convert Point2D to Point.static java.awt.geom.Point2D
pointToPoint2D(java.awt.Point p)
Convert Point to Point2D.static java.awt.Rectangle
rectangle2DToRectangle(java.awt.geom.Rectangle2D r)
Convert Rectangle2D to Rectangle.static java.lang.String
rectangle2DToString(java.awt.geom.Rectangle2D r)
rectangle2DToString return a string to represent a rectanglestatic java.awt.geom.Rectangle2D
rectangleAtPoint(java.awt.geom.Point2D p, java.lang.Double width, java.lang.Double height)
Get rectangle at point.static java.awt.geom.Rectangle2D
rectangleToRectangle2D(java.awt.Rectangle r)
Convert Rectangle to Rectangle2D.static java.awt.geom.Point2D
rotateDEG(double x, double y, double a)
Rotate x and y coordinates (by degrees).static java.awt.geom.Point2D
rotateDEG(java.awt.geom.Point2D p, double a)
Rotate a point (by degrees).static java.awt.geom.Point2D
rotateDEG(java.awt.geom.Point2D p, java.awt.geom.Point2D c, double aDEG)
Rotate a point around another point (by degrees).static java.awt.geom.Point2D
rotateRAD(double x, double y, double a)
Rotate x and y coordinates (by radians).static java.awt.geom.Point2D
rotateRAD(java.awt.geom.Point2D p, double a)
Rotate a point (by radians).static java.awt.geom.Point2D
rotateRAD(java.awt.geom.Point2D p, java.awt.geom.Point2D c, double aRAD)
Rotate a point around another point (by radians).static java.awt.geom.Rectangle2D
scale(java.awt.geom.Rectangle2D r, double s)
Scale a rectangle.static java.awt.geom.Rectangle2D
setOrigin(java.awt.geom.Rectangle2D r, java.awt.geom.Point2D origin)
Set the origin (top left) of the rectangle.static java.awt.geom.Rectangle2D
setSize(java.awt.geom.Rectangle2D r, java.awt.Dimension d)
Set the size of a rectanglestatic java.awt.geom.Rectangle2D
setSize(java.awt.geom.Rectangle2D r, java.awt.geom.Point2D s)
Set the size of a rectanglestatic java.awt.geom.Point2D
subtract(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB)
Subtract two points.static java.awt.geom.Point2D
threeFourthsPoint(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB)
Calculate the point 3/4 of the way between two points.static java.awt.geom.Point2D
twoThirdsPoint(java.awt.geom.Point2D pA, java.awt.geom.Point2D pB)
Calculate the point 2/3 of the way between two points.static java.awt.geom.Point2D
vectorDEG(double dirDEG, double magnitude)
Create a vector given a direction and a magnitude.static java.awt.geom.Point2D
vectorRAD(double dirRAD, double magnitude)
Create a vector given a direction and a magnitude.static double
wrap(double inValue, double inMin, double inMax)
Wrap a double between two values (for example +/- 180 or 0-360 degrees).static int
wrap(int inValue, int inMin, int inMax)
Wrap an int between two values (for example +/- 180 or 0-360 degrees).static double
wrap360(double inValue)
Wrap a value between 0 and 360.static double
wrapPM180(double inValue)
Wrap a value between +/-180.static double
wrapPM360(double inValue)
Wrap a value between +/-360.static java.awt.geom.Point2D
zeroPoint2D()
static java.awt.geom.Rectangle2D
zeroRectangle2D()
static java.awt.geom.Rectangle2D
zeroToInfinityRectangle2D()
-
-
-
Field Detail
-
zeroPoint2D
public static final java.awt.geom.Point2D zeroPoint2D
-
infinityPoint2D
public static final java.awt.geom.Point2D infinityPoint2D
-
zeroRectangle2D
public static final java.awt.geom.Rectangle2D zeroRectangle2D
-
zeroToInfinityRectangle2D
public static final java.awt.geom.Rectangle2D zeroToInfinityRectangle2D
-
infinityRectangle2D
public static final java.awt.geom.Rectangle2D infinityRectangle2D
-
-
Method Detail
-
zeroPoint2D
@CheckReturnValue public static java.awt.geom.Point2D zeroPoint2D()
- Returns:
- the point {0, 0}
-
infinityPoint2D
@CheckReturnValue public static java.awt.geom.Point2D infinityPoint2D()
- Returns:
- the point {POSITIVE_INFINITY, POSITIVE_INFINITY}
-
gcd
public static int gcd(int a, int b)
- Parameters:
a
- the first numberb
- the second number- Returns:
- the greatest common divisor of a and b
-
pointToPoint2D
@CheckReturnValue public static java.awt.geom.Point2D pointToPoint2D(@Nonnull java.awt.Point p)
Convert Point to Point2D.- Parameters:
p
- the Point- Returns:
- the Point2D
-
point2DToPoint
@CheckReturnValue public static java.awt.Point point2DToPoint(@Nonnull java.awt.geom.Point2D p)
Convert Point2D to Point.- Parameters:
p
- the Point- Returns:
- the Point2D
-
equals
public static boolean equals(float a, float b)
- Parameters:
a
- the first floatb
- the second float- Returns:
- true if a is equal to b
-
equals
public static boolean equals(double a, double b)
- Parameters:
a
- the first doubleb
- the second double- Returns:
- true if a is equal to b
-
equals
public static boolean equals(java.awt.geom.Rectangle2D a, java.awt.geom.Rectangle2D b)
- Parameters:
a
- the first Rectangle2Db
- the second Rectangle2D- Returns:
- true if a is equal to b
-
equals
public static boolean equals(java.awt.geom.Point2D a, java.awt.geom.Point2D b)
- Parameters:
a
- the first Point2Db
- the second Point2D- Returns:
- true if a is equal to b
-
isEqualToZeroPoint2D
public static boolean isEqualToZeroPoint2D(@Nonnull java.awt.geom.Point2D p)
- Parameters:
p
- the point- Returns:
- true if p1 is equal to zeroPoint2D
-
min
@CheckReturnValue public static java.awt.geom.Point2D min(@Nonnull java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB)
Get the minimum coordinates of two points.- Parameters:
pA
- the first pointpB
- the second point- Returns:
- the minimum coordinates
-
max
@CheckReturnValue public static java.awt.geom.Point2D max(@Nonnull java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB)
Get the maximum coordinates of two points.- Parameters:
pA
- the first pointpB
- the second point- Returns:
- the maximum coordinates
-
pin
@CheckReturnValue public static java.awt.geom.Point2D pin(@Nonnull java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB, @Nonnull java.awt.geom.Point2D pC)
Get the coordinates of a point pinned between two other points.- Parameters:
pA
- the first pointpB
- the second pointpC
- the third point- Returns:
- the coordinates of pA pined between pB and pC
-
pin
@CheckReturnValue public static java.awt.geom.Point2D pin(@Nonnull java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Rectangle2D pR)
Get the coordinates of a point pinned in a rectangle- Parameters:
pA
- the pointpR
- the rectangle- Returns:
- the coordinates of point pA pined in rectangle pR
-
add
@CheckReturnValue public static java.awt.geom.Point2D add(@Nonnull java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB)
Add two points.- Parameters:
pA
- the first pointpB
- the second point- Returns:
- the sum of the two points
-
subtract
@CheckReturnValue public static java.awt.geom.Point2D subtract(@Nonnull java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB)
Subtract two points.- Parameters:
pA
- the first pointpB
- the second point- Returns:
- the difference of the two points
-
multiply
@CheckReturnValue public static java.awt.geom.Point2D multiply(@Nonnull java.awt.geom.Point2D p, double s)
Multiply a point times a scalar.- Parameters:
p
- the points
- the scalar- Returns:
- the point multiplied by the scalar
-
multiply
@CheckReturnValue public static java.awt.geom.Point2D multiply(@Nonnull java.awt.geom.Point2D p, double x, double y)
Multiply a point times two scalar.- Parameters:
p
- the pointx
- the X scalary
- the Y scalar- Returns:
- the point multiplied by the two scalars
-
multiply
public static java.awt.geom.Point2D multiply(double s, @Nonnull java.awt.geom.Point2D p)
Multiply a scalar times a point.- Parameters:
s
- the scalarp
- the point- Returns:
- the point multiplied by the scalar
-
multiply
@CheckReturnValue public static java.awt.geom.Point2D multiply(@Nonnull java.awt.geom.Point2D p1, @Nonnull java.awt.geom.Point2D p2)
Multiply a point times a point.- Parameters:
p1
- the first pointp2
- the second point- Returns:
- the first point multiplied by the second
-
divide
@CheckReturnValue public static java.awt.geom.Point2D divide(@Nonnull java.awt.geom.Point2D p, double s)
Divide a point by a scalar.- Parameters:
p
- the points
- the scalar- Returns:
- the point divided by the scalar
-
divide
@CheckReturnValue public static java.awt.geom.Point2D divide(@Nonnull java.awt.geom.Point2D p, double x, double y)
Divide a point by two scalars.- Parameters:
p
- the pointx
- the X scalary
- the Y scalar- Returns:
- the point divided by the scalar
-
offset
@CheckReturnValue public static java.awt.geom.Point2D offset(@Nonnull java.awt.geom.Point2D p, double x, double y)
Offset a point by two scalars.- Parameters:
p
- the pointx
- the x scalary
- the y scalar- Returns:
- the point offset by the scalars
-
rotateRAD
@CheckReturnValue public static java.awt.geom.Point2D rotateRAD(double x, double y, double a)
Rotate x and y coordinates (by radians).- Parameters:
x
- the x coordinatey
- the y coordinatea
- the angle (in radians)- Returns:
- the point rotated by the angle
-
rotateDEG
@CheckReturnValue public static java.awt.geom.Point2D rotateDEG(double x, double y, double a)
Rotate x and y coordinates (by degrees).- Parameters:
x
- the x coordinatey
- the y coordinatea
- the angle (in radians)- Returns:
- the point rotated by the angle
-
rotateRAD
@CheckReturnValue public static java.awt.geom.Point2D rotateRAD(@Nonnull java.awt.geom.Point2D p, double a)
Rotate a point (by radians).- Parameters:
p
- the pointa
- the angle (in radians)- Returns:
- the point rotated by the angle
-
rotateDEG
@CheckReturnValue public static java.awt.geom.Point2D rotateDEG(@Nonnull java.awt.geom.Point2D p, double a)
Rotate a point (by degrees).- Parameters:
p
- the pointa
- the angle (in radians)- Returns:
- the point rotated by the angle
-
rotateRAD
@CheckReturnValue public static java.awt.geom.Point2D rotateRAD(@Nonnull java.awt.geom.Point2D p, @Nonnull java.awt.geom.Point2D c, double aRAD)
Rotate a point around another point (by radians).- Parameters:
p
- the point being rotatedc
- the point its being rotated aroundaRAD
- the angle (in radians)- Returns:
- the point rotated by the angle
-
rotateDEG
@CheckReturnValue public static java.awt.geom.Point2D rotateDEG(@Nonnull java.awt.geom.Point2D p, @Nonnull java.awt.geom.Point2D c, double aDEG)
Rotate a point around another point (by degrees).- Parameters:
p
- the point being rotatedc
- the point its being rotated aroundaDEG
- the angle (in radians)- Returns:
- the point rotated by the angle
-
orthogonal
public static java.awt.geom.Point2D orthogonal(@Nonnull java.awt.geom.Point2D p)
- Parameters:
p
- the point- Returns:
- the point orthogonal to this one (relative to {0, 0})
-
vectorDEG
@CheckReturnValue public static java.awt.geom.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 java.awt.geom.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 java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB)
Dot product of two points (vectors).- Parameters:
pA
- the first pointpB
- 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 java.awt.geom.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 java.awt.geom.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 java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB)
Calculate the distance between two points.- Parameters:
pA
- the first pointpB
- the second point- Returns:
- the distance between the two points
-
normalize
@CheckReturnValue public static java.awt.geom.Point2D normalize(@Nonnull java.awt.geom.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
@CheckReturnValue public static java.awt.geom.Point2D normalize(@Nonnull java.awt.geom.Point2D p)
Normalize a point (vector).- Parameters:
p
- the point (vector)- Returns:
- the normalized point (vector)
-
computeAngleRAD
@CheckReturnValue public static double computeAngleRAD(@Nonnull java.awt.geom.Point2D p)
Compute the angle (direction in radians) for a vector.- Parameters:
p
- the vector (point relative to zeroPoint2D)- Returns:
- the angle in radians
-
computeAngleDEG
@CheckReturnValue public static double computeAngleDEG(@Nonnull java.awt.geom.Point2D p)
Compute the angle (direction in degrees) for a vector.- Parameters:
p
- the vector (point relative to zeroPoint2D)- Returns:
- the angle in degrees
-
computeAngleRAD
@CheckReturnValue public static double computeAngleRAD(@Nonnull java.awt.geom.Point2D p1, @Nonnull java.awt.geom.Point2D p2)
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 Point2Dp2
- the second Point2D- Returns:
- the angle in radians
-
computeAngleDEG
@CheckReturnValue public static double computeAngleDEG(@Nonnull java.awt.geom.Point2D p1, @Nonnull java.awt.geom.Point2D p2)
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 Point2Dp2
- 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 numberb
- the second numbert
- 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 numberb
- the second numbert
- the fraction (between 0 and 1)- Returns:
- the linear interpolation between a and b for t
-
lerp
@CheckReturnValue public static java.lang.Double lerp(@Nonnull java.lang.Double a, @Nonnull java.lang.Double b, @Nonnull java.lang.Double t)
Calculate the linear interpolation between two Doubles.- Parameters:
a
- the first numberb
- the second numbert
- the fraction (between 0 and 1)- Returns:
- the linear interpolation between a and b for t
-
lerp
@CheckReturnValue public static java.awt.geom.Point2D lerp(@Nonnull java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB, double t)
Calculate the linear interpolation between two points.- Parameters:
pA
- the first pointpB
- the second pointt
- 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 granulizeg
- the granularity- Returns:
- the value granulized to the granularity
-
granulize
@CheckReturnValue public static java.awt.geom.Point2D granulize(@Nonnull java.awt.geom.Point2D p, double gH, double gV)
Round point to horizontal and vertical granular increments.- Parameters:
p
- the point to granulizegH
- the horizontal granularitygV
- the vertical granularity- Returns:
- the point granulized to the granularity
-
granulize
@CheckReturnValue public static java.awt.geom.Point2D granulize(@Nonnull java.awt.geom.Point2D p, double g)
Round point to granular increment.- Parameters:
p
- the point to granulizeg
- the granularity- Returns:
- the point granulized to the granularity
-
granulize
@CheckReturnValue public static java.awt.geom.Rectangle2D granulize(@Nonnull java.awt.geom.Rectangle2D r, double g)
Round Rectangle2D to granular increment.- Parameters:
r
- the rectangle to granulizeg
- the granularity- Returns:
- the rectangle granulized to the granularity
-
midPoint
@CheckReturnValue public static java.awt.geom.Point2D midPoint(@Nonnull java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB)
Calculate the midpoint between two points.- Parameters:
pA
- the first pointpB
- the second point- Returns:
- the midpoint between the two points
-
oneThirdPoint
@CheckReturnValue public static java.awt.geom.Point2D oneThirdPoint(@Nonnull java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB)
Calculate the point 1/3 of the way between two points.- Parameters:
pA
- the first pointpB
- the second point- Returns:
- the point one third of the way from pA to pB
-
twoThirdsPoint
@CheckReturnValue public static java.awt.geom.Point2D twoThirdsPoint(@Nonnull java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB)
Calculate the point 2/3 of the way between two points.- Parameters:
pA
- the first pointpB
- the second point- Returns:
- the point two thirds of the way from pA to pB
-
oneFourthPoint
@CheckReturnValue public static java.awt.geom.Point2D oneFourthPoint(@Nonnull java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB)
Calculate the point 1/4 of the way between two points.- Parameters:
pA
- the first pointpB
- the second point- Returns:
- the point one fourth of the way from pA to pB
-
threeFourthsPoint
@CheckReturnValue public static java.awt.geom.Point2D threeFourthsPoint(@Nonnull java.awt.geom.Point2D pA, @Nonnull java.awt.geom.Point2D pB)
Calculate the point 3/4 of the way between two points.- Parameters:
pA
- the first pointpB
- 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 valueinMin
- the lowest valueinMax
- 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 valueinMin
- the lowest valueinMax
- 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 angleb
- 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 angleb
- 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 angleb
- 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 angleb
- 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 valueinMin
- the mininMax
- 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 valueinMin
- the mininMax
- the max- Returns:
- the value pinned between the min and max values
-
zeroRectangle2D
@CheckReturnValue public static java.awt.geom.Rectangle2D zeroRectangle2D()
- Returns:
- a new rectangle {0.0, 0.0, 0.0, 0.0}
-
zeroToInfinityRectangle2D
@CheckReturnValue public static java.awt.geom.Rectangle2D zeroToInfinityRectangle2D()
- Returns:
- a new rectangle {0.0, 0.0, POSITIVE_INFINITY, POSITIVE_INFINITY}
-
infinityRectangle2D
@CheckReturnValue public static java.awt.geom.Rectangle2D infinityRectangle2D()
- Returns:
- a new rectangle {NEGATIVE_INFINITY, NEGATIVE_INFINITY, POSITIVE_INFINITY, POSITIVE_INFINITY}
-
rectangle2DToString
@Nonnull public static java.lang.String rectangle2DToString(@Nonnull java.awt.geom.Rectangle2D r)
rectangle2DToString return a string to represent a rectangle- Parameters:
r
- the rectangle2D- Returns:
- the string
-
rectangleToRectangle2D
@CheckReturnValue public static java.awt.geom.Rectangle2D rectangleToRectangle2D(@Nonnull java.awt.Rectangle r)
Convert Rectangle to Rectangle2D.- Parameters:
r
- the Rectangle- Returns:
- the Rectangle2D
-
rectangle2DToRectangle
@CheckReturnValue public static java.awt.Rectangle rectangle2DToRectangle(@Nonnull java.awt.geom.Rectangle2D r)
Convert Rectangle2D to Rectangle.- Parameters:
r
- the Rectangle- Returns:
- the Rectangle2D
-
getOrigin
@CheckReturnValue public static java.awt.geom.Point2D getOrigin(@Nonnull java.awt.geom.Rectangle2D r)
Get the origin (top left) of the rectangle.- Parameters:
r
- the rectangle- Returns:
- the origin of the rectangle
-
setOrigin
@CheckReturnValue public static java.awt.geom.Rectangle2D setOrigin(@Nonnull java.awt.geom.Rectangle2D r, @Nonnull java.awt.geom.Point2D origin)
Set the origin (top left) of the rectangle.- Parameters:
r
- the rectangleorigin
- the origin- Returns:
- a new rectangle with the new origin
-
dimensionToString
@Nonnull public static java.lang.String dimensionToString(@Nonnull java.awt.Dimension d)
dimensionToString return a string to represent a Dimension- Parameters:
d
- the Dimension- Returns:
- the string
-
getSize
@CheckReturnValue public static java.awt.Dimension getSize(@Nonnull java.awt.geom.Rectangle2D r)
Get the size of a rectangle.- Parameters:
r
- the rectangle- Returns:
- the size of the rectangle
-
setSize
@CheckReturnValue public static java.awt.geom.Rectangle2D setSize(@Nonnull java.awt.geom.Rectangle2D r, @Nonnull java.awt.Dimension d)
Set the size of a rectangle- Parameters:
r
- the rectangled
- the new size (as dimension)- Returns:
- a new rectangle with the new size
-
setSize
@CheckReturnValue public static java.awt.geom.Rectangle2D setSize(@Nonnull java.awt.geom.Rectangle2D r, @Nonnull java.awt.geom.Point2D s)
Set the size of a rectangle- Parameters:
r
- the rectangles
- the new size (as Point2D)- Returns:
- a new rectangle with the new size
-
center
@CheckReturnValue public static java.awt.geom.Point2D center(@Nonnull java.awt.geom.Rectangle2D r)
Calculate the center of the rectangle.- Parameters:
r
- the rectangle- Returns:
- the center of the rectangle
-
midPoint
@CheckReturnValue public static java.awt.geom.Point2D midPoint(@Nonnull java.awt.geom.Rectangle2D r)
Calculate the midpoint of the rectangle.- Parameters:
r
- the rectangle- Returns:
- the midpoint of the rectangle
-
offset
@CheckReturnValue public static java.awt.geom.Rectangle2D offset(@Nonnull java.awt.geom.Rectangle2D r, double x, double y)
Offset a rectangle by distinct x,y values.- Parameters:
r
- the rectanglex
- the horizontal offsety
- the vertical offset- Returns:
- the offset rectangle
-
offset
@CheckReturnValue public static java.awt.geom.Rectangle2D offset(@Nonnull java.awt.geom.Rectangle2D r, @Nonnull java.awt.geom.Point2D o)
Offset a rectangle by a single value.- Parameters:
r
- the rectangleo
- the offset- Returns:
- the offset rectangle
-
inset
@CheckReturnValue public static java.awt.geom.Rectangle2D inset(@Nonnull java.awt.geom.Rectangle2D r, double i)
Inset a rectangle by a single value.- Parameters:
r
- the rectanglei
- the inset (positive make it smaller, negative, bigger)- Returns:
- the inset rectangle
-
inset
@CheckReturnValue public static java.awt.geom.Rectangle2D inset(@Nonnull java.awt.geom.Rectangle2D r, double h, double v)
Inset a rectangle by distinct x,y values.- Parameters:
r
- the rectangleh
- 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 java.awt.geom.Rectangle2D scale(@Nonnull java.awt.geom.Rectangle2D r, double s)
Scale a rectangle.- Parameters:
r
- the rectangles
- the scale- Returns:
- the scaled rectangle
-
centerRectangleOnPoint
@CheckReturnValue public static java.awt.geom.Rectangle2D centerRectangleOnPoint(@Nonnull java.awt.geom.Rectangle2D r, @Nonnull java.awt.geom.Point2D p)
Center rectangle on point.- Parameters:
r
- the rectanglep
- the point- Returns:
- the Point2D
-
centerRectangleOnRectangle
@CheckReturnValue public static java.awt.geom.Rectangle2D centerRectangleOnRectangle(@Nonnull java.awt.geom.Rectangle2D r1, @Nonnull java.awt.geom.Rectangle2D r2)
Center rectangle on rectangle.- Parameters:
r1
- the first rectangler2
- the second rectangle- Returns:
- the first rectangle centered on the second
-
rectangleAtPoint
@CheckReturnValue public static java.awt.geom.Rectangle2D rectangleAtPoint(@Nonnull java.awt.geom.Point2D p, java.lang.Double width, java.lang.Double height)
Get rectangle at point.- Parameters:
p
- the pointwidth
- the widthheight
- the height- Returns:
- the rectangle
-
drawBezier
public static double drawBezier(@CheckForNull java.awt.Graphics2D g2, @Nonnull java.awt.geom.Point2D p0, @Nonnull java.awt.geom.Point2D p1, @Nonnull java.awt.geom.Point2D p2, @Nonnull java.awt.geom.Point2D p3)
Draw a cubic Bezier curve.- Parameters:
g2
- the Graphics2D context to draw to (null if just want length)p0
- origin control pointp1
- first control pointp2
- second control pointp3
- terminating control point- Returns:
- the length of the Bezier curve
-
getBezierPath
public static java.awt.geom.GeneralPath getBezierPath(@Nonnull java.awt.geom.Point2D[] p, double displacement)
Get the path for a Bezier curve.- Parameters:
p
- control pointsdisplacement
- right/left to draw a line parallel to the Bezier- Returns:
- the length of the Bezier curve
-
getBezierPath
public static java.awt.geom.GeneralPath getBezierPath(@Nonnull java.awt.geom.Point2D[] p)
Get the path for a Bezier curve.- Parameters:
p
- control points- Returns:
- the length of the Bezier curve
-
drawBezier
public static double drawBezier(@CheckForNull java.awt.Graphics2D g2, @Nonnull java.awt.geom.Point2D[] p, double displacement)
Draw a Bezier curve- Parameters:
g2
- the Graphics2D context to draw to (null to just return length)p
- the control pointsdisplacement
- right/left to draw a line parallel to the Bezier- Returns:
- the length of the Bezier curve
-
fillBezier
public static double fillBezier(@CheckForNull java.awt.Graphics2D g2, @Nonnull java.awt.geom.Point2D[] p, double displacement)
Fill a Bezier curve.- Parameters:
g2
- the Graphics2D context to draw top
- the control pointsdisplacement
- right/left to draw a line parallel to the Bezier- Returns:
- the length of the Bezier curve
-
drawBezier
public static double drawBezier(@CheckForNull java.awt.Graphics2D g2, @Nonnull java.awt.geom.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 java.awt.Graphics2D g2, @Nonnull java.awt.geom.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
public static java.awt.geom.Rectangle2D getBezierBounds(@Nonnull java.awt.geom.Point2D[] p)
computer the bounds of a Bezier curve.- Parameters:
p
- the control points- Returns:
- the bounds of the Bezier curve
-
intersect
@CheckReturnValue public static java.awt.geom.Point2D intersect(@Nonnull java.awt.geom.Point2D p1, @Nonnull java.awt.geom.Point2D p2, @Nonnull java.awt.geom.Point2D p3, @Nonnull java.awt.geom.Point2D p4)
Find intersection of two lines.- Parameters:
p1
- the first point on the first linep2
- the second point on the first linep3
- the first point on the second linep4
- 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 java.awt.geom.Point2D p1, @Nonnull java.awt.geom.Point2D p2, @Nonnull java.awt.geom.Point2D p3)
get (signed) distance p3 is from line segment defined by p1 and p2- Parameters:
p1
- the first point on the line segmentp2
- the second point on the line segmentp3
- 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 java.awt.geom.Point2D midPoint(java.util.List<java.awt.geom.Point2D> points)
return average point in points- Parameters:
points
- to average- Returns:
- the average point
-
isPointInPolygon
public static boolean isPointInPolygon(java.awt.geom.Point2D pointT, java.util.List<java.awt.geom.Point2D> points)
- Parameters:
pointT
- the pointpoints
- the polygon- Returns:
- true if pointT is in the polygon made up of the points
-
convexHull
public static java.util.List<java.awt.geom.Point2D> convexHull(java.util.List<java.awt.geom.Point2D> points)
compute convex hull (outline of polygon)- Parameters:
points
- of the polygon- Returns:
- points of the convex hull
-
isCounterClockWise
public static boolean isCounterClockWise(java.awt.geom.Point2D a, java.awt.geom.Point2D b, java.awt.geom.Point2D c)
isCounterClockWise- Parameters:
a
- the first pointb
- the second pointc
- the third point- Returns:
- true if the three points make a counter-clockwise turn
-
-