The comfortable framework for making games in Wren
The math module provides utilities for performing mathematical calculations.
It contains the following classes:
The Elegant class supplies a useful utility for combining pairs of signed integers, based on “An Elegant Pairing Method” by Matthew Szudzik
pair(vec: Vector): NumberPairs the x and y of vec into a single result.
pair(x: Number, y: Number): NumberPairs the x and y into a single result.
unpair(z: Number): VectorReverses the value z into a vector (x, y).
unpairAsList(z: Number): List<Number>Reverses the value z into a list [ x, y ].
The Math class provides various common mathematical functions. For convenience, you can also import this class M, like so:
import "math" for M, Math
System.print(M.min(21, 42))
System.print(Math.max(21, 42))
abs(n: Number): NumberReturns the absolute magnitude of n.
acos(n: Number): NumberReturns the arccosine of n.
asin(n: Number): NumberReturns the arcsine of n.
atan(n: Number): Numberatan(n1: Number, n2: Number): NumberReturns the arctan of n.
ceil(n: Number): NumberRounds n up to the next largest integer value.
clamp(number : Number, min : Number, max : Number) : NumberClamps number between min and max.
cos(n: Number): NumberReturns the cosine of n.
floor(n: Number): NumberRounds n down to the next smallest integer value.
lerp(low: Number, value: Number, high: Number): NumberThis performs a linear interpolation between low and high, based on the value of value, which will be clamped to a range of [0..1].
log(n: Number): NumberReturns the natural logarithm of n.
max(n1: Number, n2: Number): NumberReturns the larger of n1 and n2.
mid(n1: Number, n2: Number, n3: Number): NumberReturns the number which is in the middle of the others. This can be used as an inclusive clamping function.
min(n1: Number, n2: Number): NumberReturns the smaller of n1 and n2.
round(n: Number): NumberRounds n to the nearest integer value.
sign(n: Number): NumberIf n is negative, this returns -1. If n is positive, this returns 1. If n is equal to zero, it returns 0.
sin(n: Number): NumberReturns the sine of n.
tan(n: Number): NumberReturns the tan of n.
The Vector class works as a vector of up to 4 dimensions. You can also refer to it as a Point or Vec.
This class also implements DOME’s Hashable interface, allowing it to be used
with our collections.
Vector.new(): VectorVector.new(x, y): VectorVector.new(x, y, z): VectorVector.new(x, y, z, w): VectorCreate a vector. If a value isn’t provided, it is set to (0, 0, 0, 0).
Unless you specifically need 3 or 4-dimensional vectors, you can ignore z and w.
Vector.fromPair(z: Number): VectorReverses an Elegant pairing to produce the corresponding vector.
x: Numbery: Numberz: Numberw: Numbermanhattan: NumberThis returns the “taxicab length” of the vector, easily calculated as x + y + z + w.
length: NumberThis returns the Euclidean magnitude of the vector.
perp: VectorReturns the 2D vector perpendicular to the current vector. This doesn’t work for vectors with 3 or more dimensions.
unit: VectorReturns a copy of the current vector, where it’s arguments have been scaled such that it’s length is 1.
pair: NumberUses the Elegant algorithm to pair the x and y coordinates. This can be reversed with Elegant.unpair(_).
dot(vec: Vector): NumThis returns the dot-product of the vector with another vector.
cross(vec: Vector): NumThis returns the cross-product of the vector with another vector, resulting in a new vector that is perpendicular to the other two. This only works for 3-dimensional vectors. The w component will be discarded.
-Vector: VectorReturns the vector, but it’s elements are multiplied by -1.
Vector + Vector: VectorReturns an element-wise addition of the two Vectors. This will error if you try to add a Vector to something other than a Vector.
Vector - Vector: VectorReturns an element-wise subtraction of the two Vectors. This will error if you try to subtract a Vector from something other than a Vector, or vice versa.
Vector * Number: VectorReturns the given vector, where it’s elements have been multipled by the scalar. This will error if the multiplier is not a number.
Vector / Number: VectorReturns the given vector, where it’s elements have been divided by the scalar. This will error if the divisor is not a Number.