MathUtils

object MathUtils

A collection of math utility functions.

Ported from three.js MathUtils. Modelled as a Kotlin object so that ported three.js code keeps calling MathUtils.clamp(...) etc. verbatim.

Properties

Link copied to clipboard
const val DEG2RAD: Double

Multiply by this to convert degrees to radians.

Link copied to clipboard
const val RAD2DEG: Double

Multiply by this to convert radians to degrees.

Functions

Link copied to clipboard

Returns the smallest power of two greater than or equal to value (which must be greater than 0).

Link copied to clipboard
fun clamp(value: Double, minVal: Double, maxVal: Double): Double

Clamps value between minVal and maxVal.

Link copied to clipboard
fun damp(x: Double, y: Double, lambda: Double, dt: Double): Double

Frame-rate-independent damping: smoothly moves x toward y using lambda and delta time dt in seconds.

Link copied to clipboard
fun degToRad(degrees: Double): Double

Converts degrees to radians.

Link copied to clipboard

Denormalizes value according to the component storage type.

Link copied to clipboard

Computes the Euclidean modulo ((n % m) + m) % m.

Link copied to clipboard

Returns the largest power of two less than or equal to value (which must be greater than 0).

Link copied to clipboard

Generates a UUID.

Link copied to clipboard
fun inverseLerp(x: Double, y: Double, value: Double): Double

Returns the fraction in [0, 1] that value represents between x and y. Returns 0 when x == y (rather than NaN).

Link copied to clipboard
fun isPowerOfTwo(value: Int): Boolean

Returns true if value is a power of two.

Link copied to clipboard
fun lerp(x: Double, y: Double, t: Double): Double

Linearly interpolates between x and y by t; t = 0 returns x, t = 1 returns y.

Link copied to clipboard
fun mapLinear(x: Double, a1: Double, a2: Double, b1: Double, b2: Double): Double

Linearly maps x from range [a1, a2] to range [b1, b2].

Link copied to clipboard
fun normalize(value: Double, type: ComponentType): Double

Normalizes value (in [0, 1] for unsigned, [-1, 1] for signed) according to the component storage type.

Link copied to clipboard
fun pingpong(x: Double, length: Double = 1.0): Double

Returns a value that alternates ("ping-pongs") between 0 and length.

Link copied to clipboard
fun radToDeg(radians: Double): Double

Converts radians to degrees.

Link copied to clipboard
fun randFloat(low: Double, high: Double): Double

Returns a random float in the interval [low, high).

Link copied to clipboard

Returns a random float in the interval (-range/2, range/2).

Link copied to clipboard
fun randInt(low: Int, high: Int): Int

Returns a random integer in the closed interval [low, high].

Link copied to clipboard
fun seededRandom(s: Int? = null): Double

Returns a deterministic pseudo-random float in [0, 1) (Mulberry32).

Link copied to clipboard

Sets the given quaternion q from the Intrinsic Proper Euler Angles defined by a, b, c and order.

Link copied to clipboard
fun smootherstep(x: Double, minVal: Double, maxVal: Double): Double

Smootherstep: a variation on smoothstep with zero 1st and 2nd derivatives at both ends.

Link copied to clipboard
fun smoothstep(x: Double, minVal: Double, maxVal: Double): Double

Smoothstep: a value in [0, 1] for x between minVal and maxVal, eased at both ends.