math
ƿit provides three math modules with identical functions but different angle representations:
def math = use('math/radians') // angles in radians
def math = use('math/degrees') // angles in degrees
def math = use('math/cycles') // angles in cycles (0-1)
Trigonometry
math.sine(pi / 2) // 1 (radians)
math.sine(90) // 1 (degrees)
math.sine(0.25) // 1 (cycles)
sine(angle), cosine(angle), tangent(angle) — standard trig functions.
arc_sine(n), arc_cosine(n) — inverse trig functions.
arc_tangent(n, denominator) — inverse tangent. With two arguments, computes atan2.
Exponentials and Logarithms
e(power) — Euler’s number raised to a power. Default power is 1.
ln(n) — natural logarithm (base e).
log(n) — base 10 logarithm.
log2(n) — base 2 logarithm.
Powers and Roots
power(base, exponent) — raise base to exponent.
sqrt(n) — square root.
root(radicand, n) — nth root.
Constants
e is supplied by the module:
math.e() // 2.71828...
pi is a language-level global and needs no module:
pi // 3.14159...
Example
def math = use('math/radians')
// Distance between two points
var distance = function(x1, y1, x2, y2) {
var dx = x2 - x1
var dy = y2 - y1
return math.sqrt(dx * dx + dy * dy)
}
// Angle between two points
var angle = function(x1, y1, x2, y2) {
return math.arc_tangent(y2 - y1, x2 - x1)
}
// Rotate a point
var rotate = function(x, y, a) {
var c = math.cosine(a)
var s = math.sine(a)
return {
x: x * c - y * s,
y: x * s + y * c
}
}