-
Notifications
You must be signed in to change notification settings - Fork 0
Math
SalamiVG ships with some math-y functions
hypot(x: number, y: number): number
Returns the hypothenuse of a right triangle where origin is at (0, 0)
and the corner is at (x, y)
. This is equivalent to the Euclidean length of the vector, e.g. vec2(x, y).length() === hypoth(x, y)
Examples:
hypot(3, 4) === 5
vec2(3, 4).length() === hypoth(3, 4)
isWithin(min: number, max: number, value: number): boolean
Returns true
when value
is in the range [min, max]
. This can sometimes be useful when dealing with floating point arithmetic.
Examples:
isWithin(2.001, 1.998, 1.999) === true
isWithinError(target: number, error: number, value: number): boolean
Identical to isWithin
, but the arguments are arranged differently so you can check for an "error" amount around a target value.
Examples:
isWithinError(2, 0.002, 1.999) === true
smallestAngularDifference(angle1: Radians, angle2: Radians): Radians
Returns the smallest angular difference between two angles, regardless of their orientation. This is sometimes useful when two angles can exist on opposite sides of the π/-π or 0/2π boundaries.
Examples:
smallestAngularDifference(2 * Math.PI - 0.05, 0.05) === 0.1
smallestAngularDifference(-Math.PI - 0.05, Math.PI + 0.05) === 0.1
angleOfVertex(a: Vector2, b: Vector2, c: Vector2): Radians
Three points define an angle. The vertex of the angle is b
. Points a
and c
are the two end points.
Examples:
angleOfVertex(vec2(2, 0), vec2(0, 0), vec2(4, 4)) === Math.PI / 4
haveSameSign(a: number, b: number): boolean
Returns true
when the two numbers have the same sign (i.e. are both positive, or both negative)
Examples:
haveSameSign(1, 2) === true
haveSameSign(1, -2) === false
toFixedPrecision(value: number, precision: number): number
Similar to toFixed
, but returns a number with a fixed amount of precision. Decimals are rounded at the level of precision specified.
Examples:
toFixedPrecision(1.23456, 1) === 1.2
toFixedPrecision(1.23456, 2) === 1.23
toFixedPrecision(1.23456, 4) === 1.2345