Skip to main content
A collection of math utilities for common operations including 2D vector math, rectangle operations, and value clamping.

Installation

npm install @temelj/math

Clamping operations

Utilities for constraining numeric values within specified ranges.

clamp

Clamps a value between a minimum and maximum.
function clamp<T extends number | bigint>(x: T, min: T, max: T): T
import { clamp } from "@temelj/math";

clamp(5, 0, 10);   // 5
clamp(12, 0, 10);  // 10
clamp(-2, 0, 10);  // 0

clampWithOverflow

Clamps a value with wrapping overflow behavior.
function clampWithOverflow<T extends number | bigint>(x: T, min: T, max: T): T
import { clampWithOverflow } from "@temelj/math";

clampWithOverflow(5, 0, 10);   // 5
clampWithOverflow(12, 0, 10);  // 2
clampWithOverflow(-2, 0, 10);  // 8
clampWithOverflow wraps values that exceed the range back into the valid range, making it useful for circular buffers and cyclic operations.

Vector2

A 2D vector type with comprehensive operations.

Types

interface Vector2 {
  x: number;
  y: number;
}

Construction

Creates a new vector from x and y coordinates.
vector2.of(x: number, y: number): Vector2
import { vector2 } from "@temelj/math";

const v = vector2.of(10, 20);
// { x: 10, y: 20 }

Arithmetic operations

vector2.plus(a: Vector2, b: Vector2): Vector2
vector2.add(v: Vector2, x: number, y: number): Vector2
import { vector2 } from "@temelj/math";

const v1 = vector2.of(1, 2);
const v2 = vector2.of(3, 4);

vector2.plus(v1, v2);    // { x: 4, y: 6 }
vector2.add(v1, 10, 20); // { x: 11, y: 22 }

Manipulation

Negates both components of a vector.
vector2.negate(v: Vector2): Vector2
import { vector2 } from "@temelj/math";

const v = vector2.of(5, -10);
vector2.negate(v); // { x: -5, y: 10 }

Utility

vector2.equals(a: Vector2, b: Vector2): boolean
vector2.displayString(v: Vector2): string
import { vector2 } from "@temelj/math";

const v1 = vector2.of(5, 10);
const v2 = vector2.of(5, 10);
const v3 = vector2.of(3, 7);

vector2.equals(v1, v2); // true
vector2.equals(v1, v3); // false

vector2.displayString(v1); // "Vector2(5, 10)"

Rectangle

Rectangle type and operations for 2D bounding boxes.

Types

interface Rectangle {
  position: Vector2;
  size: Vector2;
}

Construction

Creates a rectangle from position and size vectors.
rectangle.of(position: Vector2, size: Vector2): Rectangle
import { rectangle, vector2 } from "@temelj/math";

const rect = rectangle.of(
  vector2.of(10, 20),
  vector2.of(100, 50)
);

Operations

Expands rectangle to include another rectangle.
rectangle.expand(a: Rectangle, b: Rectangle): Rectangle
import { rectangle } from "@temelj/math";

const rect1 = rectangle.ofBounds(0, 0, 100, 100);
const rect2 = rectangle.ofBounds(50, 50, 100, 100);

const expanded = rectangle.expand(rect1, rect2);
// position: { x: 0, y: 0 }
// size: { x: 150, y: 150 }

Utility

rectangle.displayString(r: Rectangle): string
import { rectangle } from "@temelj/math";

const rect = rectangle.ofBounds(10, 20, 100, 50);
rectangle.displayString(rect);
// "Rectangle(10, 20, 100, 50)"
All vector and rectangle operations are immutable - they return new objects rather than modifying existing ones.

Build docs developers (and LLMs) love