disko:math
Deterministic scalars, vectors, angles, elementary functions and seeded random numbers.
Import with import { … } from "disko:math". Generated from the module's
shipped declarations. See Randomness & determinism for a
guide.
disko:math — deterministic scalars, vectors, angles, elementary functions and seeded random numbers.
Every function is pure and returns bit-identical results on every host. Wrong types throw TypeError; invalid values (non-finite numbers, zero vectors, empty arrays, reversed ranges) throw RangeError. Error messages name the function and the argument, for example normalize: vector has zero length.
Angles are radians. The y axis points down on screen, so a positive rotation turns clockwise on screen.
Types
Vector
A point or vector. Other properties on input objects are ignored.
Tolerance
Optional absolute tolerance that widens every boundary comparison.
| Member | Description |
|---|---|
epsilon | A finite number >= 0. 0 is the same as omitting it. |
RandomState
Seeded xoshiro128** state: four uint32 words, never all zero. Frozen.
Draw
One draw: the value and the state to use for the following draw.
Constants
PI
3.141592653589793.
TAU
2 * PI.
ZERO
The frozen zero vector { x: 0, y: 0 }.
Functions
clamp
Clamps value to [min, max].
Throws: RangeError when min > max.
lerp
Linear interpolation a + (b - a) * t; t is not clamped.
inverseLerp
The inverse of lerp: (value - a) / (b - a).
Throws: RangeError when a === b.
remap
Maps value from [fromMin, fromMax] onto [toMin, toMax]: lerp(toMin, toMax, inverseLerp(fromMin, fromMax, value)), unclamped.
Throws: RangeError when fromMin === fromMax.
smoothstep
Hermite smoothstep t² (3 − 2t) with t clamped to [0, 1].
Throws: RangeError when edge0 === edge1.
approxEqual
Whether |a − b| <= epsilon. The tolerance is required.
Throws: RangeError when epsilon is negative.
sin
Sine (FreeBSD msun s_sin.c), identical on every host.
cos
Cosine (FreeBSD msun s_cos.c), identical on every host.
tan
Tangent (FreeBSD msun s_tan.c), identical on every host.
asin
Arcsine in [−π/2, π/2] (msun e_asin.c).
Throws: RangeError outside [−1, 1].
acos
Arccosine in [0, π] (msun e_acos.c).
Throws: RangeError outside [−1, 1].
atan
Arctangent in [−π/2, π/2] (msun s_atan.c).
atan2
Angle of (x, y) in [−π, π] (msun e_atan2.c), with IEEE 754 signed zeros: atan2(0, -0) === PI.
exp
e^x (msun e_exp.c); overflows to Infinity above ~709.78.
log
Natural logarithm (msun e_log.c); log(0) === -Infinity.
Throws: RangeError when x < 0.
log2
Base-2 logarithm (msun e_log2.c); log2(0) === -Infinity.
Throws: RangeError when x < 0.
pow
x^y (msun e_pow.c, including its IEEE special cases). Overflows to ±Infinity, and pow(±0, y < 0) is ±Infinity.
Throws: RangeError when x < 0 and y is not an integer (a NaN result).
hypot
sqrt(x² + y²) without intermediate overflow or underflow (msun e_hypot.c); overflows to Infinity only when the result does.
vec
Creates the vector { x, y }.
add
Component-wise a + b.
sub
Component-wise a − b.
scale
v multiplied by the scalar s.
negate
{ x: −v.x, y: −v.y }.
dot
Dot product a.x·b.x + a.y·b.y.
cross
2D cross product a.x·b.y − a.y·b.x.
length
Euclidean length sqrt(x² + y²).
lengthSquared
Squared length x² + y².
distance
Euclidean distance between two points.
distanceSquared
Squared distance between two points.
normalize
The unit vector in the direction of v.
Throws: RangeError on a zero vector.
withLength
v rescaled to length (negative lengths flip it).
Throws: RangeError on a zero vector.
perpendicular
{ x: −v.y, y: v.x }: +90°, clockwise on screen.
rotate
(x·cos − y·sin, x·sin + y·cos) using the deterministic trigonometry.
rotateAround
Rotates point around pivot by angle.
midpoint
The point halfway between a and b.
lerpVector
Component-wise lerp; t is not clamped.
project
The projection of v onto the direction of onto.
Throws: RangeError when onto is zero.
reflect
v reflected about the line with the given normal, which is normalized internally.
Throws: RangeError when normal is zero.
equals
Exact equality, or each component within opts.epsilon.
angleOf
The angle of v in (−π, π].
Throws: RangeError on a zero vector.
fromAngle
The vector (cos angle, sin angle) · length.
normalizeAngle
angle wrapped into (−π, π].
angleDifference
The shortest signed turn from from to to, in (−π, π].
angleBetween
The signed angle from vector a to vector b, in (−π, π].
Throws: RangeError when either vector is zero.
lerpAngle
Interpolates along the shortest turn; the result is normalized.
radians
Degrees to radians.
degrees
Radians to degrees.
seed
A random state from a safe integer (its low and high 32 bits) or a string (FNV-1a over its UTF-8 bytes), expanded through splitmix32. The same seed gives the same sequence on every host.
Throws: RangeError for a number that is not a safe integer.
random
A number in [0, 1) with 53 random bits (two generator outputs).
randomRange
A number in [min, max).
Throws: RangeError when min >= max or max − min overflows.
randomInt
An unbiased integer in [min, max] (Lemire's method with rejection).
Throws: RangeError unless both are safe integers, min <= max and max − min < 2³².
randomBool
true with the given probability in [0, 1] (default 0.5).
Throws: RangeError outside [0, 1].
pick
One element chosen uniformly.
Throws: RangeError on an empty array.
shuffle
A new array shuffled with Fisher–Yates; the input is untouched.
fork
An independent child state seeded through splitmix32 from two parent outputs; next continues the parent.