#Math & utilities
A complete set of math, array, and logging helpers is available to every script.
#Math functions
| Function |
Description |
min(a, b, c?, d?) |
Minimum of 2–4 values. |
max(a, b, c?, d?) |
Maximum of 2–4 values. |
clamp(value, min, max) |
Constrain value to [min, max]. |
abs(x) |
Absolute value. |
sign(x) |
-1, 0, or 1. |
saturate(x) |
Clamp to [0, 1]. |
lerp(a, b, t) |
Linear interpolation: a + (b - a) * t. |
smoothstep(edge0, edge1, x) |
Smooth Hermite interpolation. |
step(edge, x) |
0 if x < edge, else 1. |
remap(x, fromMin, fromMax, toMin, toMax) |
Remap value from one range to another. |
#Power & exponential
| Function |
Description |
sqrt(x) |
Square root (negative values clamped to 0). |
pow(base, exp) |
base^exp. |
exp(x) |
e^x. |
log(x) |
Natural log (requires x > 0). |
#Trigonometry
| Function |
Description |
sin(rad) / cos(rad) / tan(rad) |
Standard trig (radians). |
atan2(y, x) |
Arctangent — angle of (x, y) vector. |
degrees(rad) |
Radians → degrees. |
radians(deg) |
Degrees → radians. |
#Vector
| Function |
Description |
length(x, y) |
2D vector length: sqrt(x² + y²). |
hypot(x, y) |
Alias for length. |
#Rounding
| Function |
Description |
round(x, digits?) |
Round to nearest integer or N decimal places. |
floor(x) |
Largest integer ≤ x. |
ceil(x) |
Smallest integer ≥ x. |
#Array functions
| Function |
Description |
append(arr, item1, item2?, ...) |
New array with items appended. |
pop(arr, count?) |
New array with last count items removed (default 1). |
slice(arr, start, end?) |
Sub-array [start, end). |
merge(arr1, arr2, ...) |
Concatenate two or more arrays. |
array_avg(arr) |
Average of numeric array. |
range(n) / range(start, end) |
Integer array [start, end). |
len(arr_or_obj) |
Array length or object member count. |
#Logging & monitoring
| Function |
Description |
print(val1, val2?, ...) |
Log to device log (up to 16 args). |
printSlow(val1, ...) |
Same as print with a 200ms throttle. |
monitor(label, value) |
Display label = value in the monitor panel. |
monitor("fps", captureFps);
monitor("err", x);
#Drawing
For overlays drawn on the preview window:
| Function |
Description |
drawRectangle(x1, y1, x2, y2, color, thickness?) |
Draw a rectangle. |
drawCircle(cx, cy, radius, color, thickness?) |
Draw a circle. |
drawLine(x1, y1, x2, y2, color, thickness?) |
Draw a line. |
drawArrow(x1, y1, x2, y2, color, thickness?) |
Draw an arrow. |
drawText(text, x, y, color, fontSize?) |
Draw text at (x, y). |
Colors accept named ("red") or hex ("#FF0000") strings.
#Common patterns
Exponential smoothing (low-pass filter):
alpha = 1.0 - exp(-rate * dt / 1000);
value = value + (target - value) * alpha;
Gamma curve (apply non-linear distance scaling):
r = hypot(x, y);
t = clamp(r / span, 0, 1);
r2 = span * pow(t, gamma);
x = (x / r) * r2;
y = (y / r) * r2;
Polar conversion:
r = hypot(x, y);
theta = atan2(y, x);
// ...modify r or theta...
x = r * cos(theta);
y = r * sin(theta);