API Reference /Math

API Reference

Math

Trigonometry, rounding, random numbers and the handful of formulas every game uses.

Codetoy doesn't add its own math library. Each language's standard one is available:

Where math lives
// Global Math, like JavaScript's (all f64)
Math.sin(x)   Math.cos(x)   Math.atan2(y, x)
Math.sqrt(x)  Math.pow(b, e)  Math.abs(x)
Math.floor(x) Math.ceil(x)  Math.round(x)
Math.min(a, b) Math.max(a, b)
Math.random() // 0 ≤ n < 1
Math.PI       Math.E
// System.Math (imported automatically)
Math.Sin(x)   Math.Cos(x)   Math.Atan2(y, x)
Math.Sqrt(x)  Math.Pow(b, e)  Math.Abs(x)
Math.Floor(x) Math.Ceiling(x) Math.Round(x)
Math.Min(a, b) Math.Max(a, b) Math.Clamp(v, lo, hi)
Random.Shared.NextDouble() // 0 ≤ n < 1
Math.PI       Math.E       Math.Tau
import math, random

math.sin(x)   math.cos(x)   math.atan2(y, x)
math.sqrt(x)  math.pow(b, e)  abs(x)
math.floor(x) math.ceil(x)  round(x)
min(a, b)     max(a, b)
random.random()  # 0 ≤ n < 1
random.uniform(a, b)  random.randint(a, b)
math.pi       math.e       math.tau

AssemblyScript's Math works on f64. For 32-bit floats there's Mathf (Mathf.sin, …). The full list is in the AssemblyScript Math reference.

#Angles are radians

Every angle in Codetoy (rotate()Canvas.Rotate()rotate(), arc()not in C#arc(), Math.sin()Math.Sin()math.sin(), Math.cos()Math.Cos()math.cos()) is in radians. A full turn is 2π, about 6.283. To convert degrees: radians = degrees * π / 180.

#Recipes

#Distance between two points

const dx = x2 - x1, dy = y2 - y1
const dist = Math.sqrt(dx * dx + dy * dy)
const hit = dist < r1 + r2   // two circles overlap
double dx = x2 - x1, dy = y2 - y1;
double dist = Math.Sqrt(dx * dx + dy * dy);
bool hit = dist < r1 + r2;   // two circles overlap
dist = math.hypot(x2 - x1, y2 - y1)
hit = dist < r1 + r2   # two circles overlap

#Angle toward a point

const angle = Math.atan2(input.mouseY() - y, input.mouseX() - x)
x += Math.cos(angle) * speed * deltaTime()
y += Math.sin(angle) * speed * deltaTime()
double angle = Math.Atan2(Input.MouseY - y, Input.MouseX - x);
x += Math.Cos(angle) * speed * Screen.DeltaTime;
y += Math.Sin(angle) * speed * Screen.DeltaTime;
angle = math.atan2(mouseY() - y, mouseX() - x)
x += math.cos(angle) * speed * deltaTime()
y += math.sin(angle) * speed * deltaTime()

#Random number in a range

function randomRange(min: f64, max: f64): f64 {
  return min + Math.random() * (max - min)
}
double RandomRange(double min, double max) => min + Random.Shared.NextDouble() * (max - min);
random.uniform(min_value, max_value)

#Clamp and lerp

function clamp(v: f64, lo: f64, hi: f64): f64 { return Math.max(lo, Math.min(hi, v)) }
function lerp(a: f64, b: f64, t: f64): f64 { return a + (b - a) * t }
double clamped = Math.Clamp(v, lo, hi);
double Lerp(double a, double b, double t) => a + (b - a) * t;
def clamp(v, lo, hi): return max(lo, min(hi, v))
def lerp(a, b, t): return a + (b - a) * t

#Rectangle overlap (AABB)

function overlaps(ax: f64, ay: f64, aw: f64, ah: f64, bx: f64, by: f64, bw: f64, bh: f64): bool {
  return ax < bx + bw && ax + aw > bx && ay < by + bh && ay + ah > by
}
bool Overlaps(double ax, double ay, double aw, double ah, double bx, double by, double bw, double bh) =>
    ax < bx + bw && ax + aw > bx && ay < by + bh && ay + ah > by;
def overlaps(ax, ay, aw, ah, bx, by, bw, bh):
    return ax < bx + bw and ax + aw > bx and ay < by + bh and ay + ah > by
Esc