Class Maths
java.lang.Object
lol.trq.helios.math.Maths
Small numeric helpers used across rendering and animation. All static and allocation-free.
-
Method Summary
Modifier and TypeMethodDescriptionstatic doubleapproach(double current, double target, double maxStep) Moves a value towards a target by at most a step, never past it.static doubleclamp(double v, double min, double max) Clamps a value.static floatclamp(float v, float min, float max) Clamps a value.static doublegaussian(double x, double sigma) The normal distribution's density.static voidgaussianKernel(double sigma, float[] out) Fills normalized one-sided Gaussian weights for a separable blur:out[0]is the centre tap, and the centre plus twice the rest sums to 1.static doubleinverseLerp(double a, double b, double v) The position of a value between two others.static doublelerp(double a, double b, double t) Linear interpolation, not clamped.static floatlerp(float a, float b, float t) Linear interpolation, not clamped.static doublelerpDegrees(double a, double b, double t) Interpolates angles the short way round.static doublerandom(double min, double max) A uniformly random value, from a thread-local generator.static intrandom(int min, int max) A uniformly random int, from a thread-local generator.static doubleremap(double v, double fromMin, double fromMax, double toMin, double toMax) Maps a value from one range to another, not clamped.static doubleround(double v, int places) Rounds to decimal places, half away from zero, without BigDecimal.static doublesnap(double v, double step) Rounds to the nearest multiple of a step, such as 0.5 or a pixel size.static doublewrapDegrees(double degrees) Wraps an angle to[-180, 180).
-
Method Details
-
clamp
public static double clamp(double v, double min, double max) Clamps a value.- Parameters:
v- the valuemin- the lower boundmax- the upper bound- Returns:
vwithin[min, max]
-
clamp
public static float clamp(float v, float min, float max) Clamps a value.- Parameters:
v- the valuemin- the lower boundmax- the upper bound- Returns:
vwithin[min, max]
-
lerp
public static double lerp(double a, double b, double t) Linear interpolation, not clamped.- Parameters:
a- the value at 0b- the value at 1t- the position- Returns:
a + (b - a) * t
-
lerp
public static float lerp(float a, float b, float t) Linear interpolation, not clamped.- Parameters:
a- the value at 0b- the value at 1t- the position- Returns:
a + (b - a) * t
-
inverseLerp
public static double inverseLerp(double a, double b, double v) The position of a value between two others.- Parameters:
a- the value at 0b- the value at 1v- the value- Returns:
(v - a) / (b - a), or 0 whena == b
-
remap
public static double remap(double v, double fromMin, double fromMax, double toMin, double toMax) Maps a value from one range to another, not clamped.- Parameters:
v- the valuefromMin- source range startfromMax- source range endtoMin- target range starttoMax- target range end- Returns:
- the mapped value
-
approach
public static double approach(double current, double target, double maxStep) Moves a value towards a target by at most a step, never past it.- Parameters:
current- the valuetarget- the targetmaxStep- the largest move, not negative- Returns:
- the moved value
-
wrapDegrees
public static double wrapDegrees(double degrees) Wraps an angle to[-180, 180).- Parameters:
degrees- the angle- Returns:
- the wrapped angle
-
lerpDegrees
public static double lerpDegrees(double a, double b, double t) Interpolates angles the short way round.- Parameters:
a- the angle at 0, degreesb- the angle at 1, degreest- the position- Returns:
- the interpolated angle, not wrapped
-
round
public static double round(double v, int places) Rounds to decimal places, half away from zero, without BigDecimal.- Parameters:
v- the valueplaces- decimal places, 0 to 15- Returns:
- the rounded value
-
snap
public static double snap(double v, double step) Rounds to the nearest multiple of a step, such as 0.5 or a pixel size.- Parameters:
v- the valuestep- the step, positive- Returns:
- the snapped value
-
gaussian
public static double gaussian(double x, double sigma) The normal distribution's density.- Parameters:
x- the offset from the meansigma- the standard deviation- Returns:
- the density
-
gaussianKernel
public static void gaussianKernel(double sigma, float[] out) Fills normalized one-sided Gaussian weights for a separable blur:out[0]is the centre tap, and the centre plus twice the rest sums to 1.- Parameters:
sigma- the standard deviation in tapsout- receives the weights; its length is the radius plus one
-
random
public static double random(double min, double max) A uniformly random value, from a thread-local generator.- Parameters:
min- inclusive lower boundmax- exclusive upper bound- Returns:
- the value
-
random
public static int random(int min, int max) A uniformly random int, from a thread-local generator.- Parameters:
min- inclusive lower boundmax- inclusive upper bound- Returns:
- the value
-