Skip to main content

Overview

The Vector2 API provides immutable operations for working with 2D vectors. All operations return new vector instances rather than modifying existing ones.

Types

Vector2

Represents a point or direction in 2D space.
interface Vector2 {
  x: number;
  y: number;
}
x
number
required
The x coordinate
y
number
required
The y coordinate

Constructor functions

of

Creates a new vector with the specified coordinates.
vector2.of(x: number, y: number): Vector2
x
number
required
The x coordinate
y
number
required
The y coordinate
result
Vector2
A new vector with the specified coordinates
Example:
import { vector2 } from "@temelj/math";

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

zero

Creates a vector at the origin (0, 0).
vector2.zero(): Vector2
result
Vector2
A vector with x and y set to 0
Example:
const origin = vector2.zero();
console.log(origin); // { x: 0, y: 0 }

copy

Creates a shallow copy of a vector.
vector2.copy(v: Vector2): Vector2
v
Vector2
required
The vector to copy
result
Vector2
A new vector with the same coordinates
Example:
const original = vector2.of(5, 10);
const duplicate = vector2.copy(original);
console.log(duplicate); // { x: 5, y: 10 }

Comparison

equals

Checks if two vectors have the same coordinates.
vector2.equals(a: Vector2, b: Vector2): boolean
a
Vector2
required
First vector to compare
b
Vector2
required
Second vector to compare
result
boolean
True if both x and y coordinates are equal
Example:
const v1 = vector2.of(10, 20);
const v2 = vector2.of(10, 20);
const v3 = vector2.of(15, 20);

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

Arithmetic operations

plus

Adds two vectors component-wise.
vector2.plus(a: Vector2, b: Vector2): Vector2
a
Vector2
required
First vector
b
Vector2
required
Second vector
result
Vector2
A new vector with summed coordinates
Example:
const v1 = vector2.of(10, 20);
const v2 = vector2.of(5, 3);
const result = vector2.plus(v1, v2);
console.log(result); // { x: 15, y: 23 }

add

Adds scalar values to a vector’s coordinates.
vector2.add(v: Vector2, x: number, y: number): Vector2
v
Vector2
required
The base vector
x
number
required
Value to add to x coordinate
y
number
required
Value to add to y coordinate
result
Vector2
A new vector with added values
Example:
const v = vector2.of(10, 20);
const result = vector2.add(v, 5, 3);
console.log(result); // { x: 15, y: 23 }

minus

Subtracts one vector from another component-wise.
vector2.minus(a: Vector2, b: Vector2): Vector2
a
Vector2
required
Vector to subtract from
b
Vector2
required
Vector to subtract
result
Vector2
A new vector with subtracted coordinates
Example:
const v1 = vector2.of(10, 20);
const v2 = vector2.of(3, 5);
const result = vector2.minus(v1, v2);
console.log(result); // { x: 7, y: 15 }

subtract

Subtracts scalar values from a vector’s coordinates.
vector2.subtract(v: Vector2, x: number, y: number): Vector2
v
Vector2
required
The base vector
x
number
required
Value to subtract from x coordinate
y
number
required
Value to subtract from y coordinate
result
Vector2
A new vector with subtracted values
Example:
const v = vector2.of(10, 20);
const result = vector2.subtract(v, 3, 5);
console.log(result); // { x: 7, y: 15 }

times

Multiplies two vectors component-wise.
vector2.times(a: Vector2, b: Vector2): Vector2
a
Vector2
required
First vector
b
Vector2
required
Second vector
result
Vector2
A new vector with multiplied coordinates
Example:
const v1 = vector2.of(4, 5);
const v2 = vector2.of(2, 3);
const result = vector2.times(v1, v2);
console.log(result); // { x: 8, y: 15 }

multiply

Multiplies a vector’s coordinates by scalar values.
vector2.multiply(v: Vector2, x: number, y: number): Vector2
v
Vector2
required
The base vector
x
number
required
Multiplier for x coordinate
y
number
required
Multiplier for y coordinate
result
Vector2
A new vector with multiplied values
Example:
const v = vector2.of(4, 5);
const result = vector2.multiply(v, 2, 3);
console.log(result); // { x: 8, y: 15 }

scale

Scales a vector by a uniform factor.
vector2.scale(v: Vector2, x: number): Vector2
v
Vector2
required
The vector to scale
x
number
required
The scale factor applied to both coordinates
result
Vector2
A new vector scaled by the factor
Example:
const v = vector2.of(3, 4);
const scaled = vector2.scale(v, 2);
console.log(scaled); // { x: 6, y: 8 }

divided

Divides one vector by another component-wise.
vector2.divided(a: Vector2, b: Vector2): Vector2
a
Vector2
required
Numerator vector
b
Vector2
required
Denominator vector
result
Vector2
A new vector with divided coordinates
Example:
const v1 = vector2.of(10, 20);
const v2 = vector2.of(2, 4);
const result = vector2.divided(v1, v2);
console.log(result); // { x: 5, y: 5 }

divide

Divides a vector’s coordinates by scalar values.
vector2.divide(v: Vector2, x: number, y: number): Vector2
v
Vector2
required
The vector to divide
x
number
required
Divisor for x coordinate
y
number
required
Divisor for y coordinate
result
Vector2
A new vector with divided values
Example:
const v = vector2.of(10, 20);
const result = vector2.divide(v, 2, 4);
console.log(result); // { x: 5, y: 5 }

negate

Negates both components of a vector.
vector2.negate(v: Vector2): Vector2
v
Vector2
required
The vector to negate
result
Vector2
A new vector with negated coordinates
Example:
const v = vector2.of(5, -3);
const negated = vector2.negate(v);
console.log(negated); // { x: -5, y: 3 }

Utility operations

snap

Snaps a vector’s coordinates to the nearest grid point.
vector2.snap(v: Vector2, size: number): Vector2
v
Vector2
required
The vector to snap
size
number
required
The grid size
result
Vector2
A new vector snapped to the grid
Example:
const v = vector2.of(13, 27);
const snapped = vector2.snap(v, 10);
console.log(snapped); // { x: 10, y: 30 }

clamp

Clamps a vector’s coordinates between minimum and maximum values.
vector2.clamp(v: Vector2, min: Vector2, max: Vector2): Vector2
v
Vector2
required
The vector to clamp
min
Vector2
required
Minimum bounds
max
Vector2
required
Maximum bounds
result
Vector2
A new vector with clamped coordinates
Example:
const v = vector2.of(150, -10);
const min = vector2.of(0, 0);
const max = vector2.of(100, 100);
const clamped = vector2.clamp(v, min, max);
console.log(clamped); // { x: 100, y: 0 }

clampMin

Clamps a vector’s coordinates to minimum values only.
vector2.clampMin(v: Vector2, min: Vector2): Vector2
v
Vector2
required
The vector to clamp
min
Vector2
required
Minimum bounds
result
Vector2
A new vector with coordinates not below minimum
Example:
const v = vector2.of(-5, 10);
const min = vector2.of(0, 0);
const clamped = vector2.clampMin(v, min);
console.log(clamped); // { x: 0, y: 10 }

clampMax

Clamps a vector’s coordinates to maximum values only.
vector2.clampMax(v: Vector2, max: Vector2): Vector2
v
Vector2
required
The vector to clamp
max
Vector2
required
Maximum bounds
result
Vector2
A new vector with coordinates not above maximum
Example:
const v = vector2.of(150, 50);
const max = vector2.of(100, 100);
const clamped = vector2.clampMax(v, max);
console.log(clamped); // { x: 100, y: 50 }

displayString

Converts a vector to a readable string representation.
vector2.displayString(v: Vector2): string
v
Vector2
required
The vector to convert
result
string
A formatted string representation
Example:
const v = vector2.of(10, 20);
console.log(vector2.displayString(v)); // "Vector2(10, 20)"

Build docs developers (and LLMs) love