Red Otter logo
Red Otter
0.1.15

Math Library

Since the whole project is entirely zero-dependency, it comes with its own math library.

API


class

Mat4

/math/Mat4.ts
method

identity

Type declaration

TypeScript
() => Mat4;
method

scale

Parameter
Type and description
scale
Vec3
returns
Mat4

Type declaration

TypeScript
(scale: Vec3) => Mat4;
method

translate

Parameter
Type and description
offset
Vec3
returns
Mat4

Type declaration

TypeScript
(offset: Vec3) => Mat4;
method

xRotation

Parameter
Type and description
angle
number
returns
Mat4

Type declaration

TypeScript
(angle: number) => Mat4;
method

yRotation

Parameter
Type and description
angle
number
returns
Mat4

Type declaration

TypeScript
(angle: number) => Mat4;
method

zRotation

Parameter
Type and description
angle
number
returns
Mat4

Type declaration

TypeScript
(angle: number) => Mat4;
method

rotate

Parameter
Type and description
angle
Vec3
returns
Mat4

Type declaration

TypeScript
(angle: Vec3) => Mat4;
Parameter
Type and description
q
Vec4

rotation quaternion

v
Vec4

translation vector

returns
Mat4

a matrix combining rotation and translation

Type declaration

TypeScript
(q: Vec4, v: Vec4) => Mat4;

Generates orthogonal projection matrix with near/far clip planes corresponding to a normalized NDC z-range of [0, 1].

Parameter
Type and description
left
number
right
number
bottom
number
top
number
near
number
far
number
returns
Mat4

Type declaration

TypeScript
(
  left: number,
  right: number,
  bottom: number,
  top: number,
  near: number,
  far: number,
) => Mat4;

fov is in radians. Assumes WebGPU NDC z-range of [0, 1].

Parameter
Type and description
fov
number
aspect
number
near
number
far
number
returns
Mat4

Type declaration

TypeScript
(fov: number, aspect: number, near: number, far: number) => Mat4;
method

lookAt

Parameter
Type and description
position
Vec3
target
Vec3
up
Vec3
returns
Mat4

Type declaration

TypeScript
(position: Vec3, target: Vec3, up: Vec3) => Mat4;
method

transpose

Type declaration

TypeScript
() => Mat4;
Parameter
Type and description
vec
Vec4
returns
Vec4

Type declaration

TypeScript
(vec: Vec4) => Vec4;
method

multiply

Parameter
Type and description
other
Mat4
returns
Mat4

Type declaration

TypeScript
(other: Mat4) => Mat4;
method

invert

Type declaration

TypeScript
() => Mat4;

class

Vec2

/math/Vec2.ts

A 2D vector.

method

add

Parameter
Type and description
other
Vec2
returns
Vec2

Type declaration

TypeScript
(other: Vec2) => Vec2;
method

subtract

Parameter
Type and description
other
Vec2
returns
Vec2

Type declaration

TypeScript
(other: Vec2) => Vec2;
method

length

Type declaration

TypeScript
() => number;
method

normalize

Type declaration

TypeScript
() => Vec2;
method

scale

Parameter
Type and description
scalar
number
returns
Vec2

Type declaration

TypeScript
(scalar: number) => Vec2;
method

cross

Parameter
Type and description
other
Vec2
returns
number

Type declaration

TypeScript
(other: Vec2) => number;
method

dot

Parameter
Type and description
other
Vec2
returns
number

Type declaration

TypeScript
(other: Vec2) => number;
method

distance

Parameter
Type and description
other
Vec2
returns
number

Type declaration

TypeScript
(other: Vec2) => number;
method

lerp

Parameter
Type and description
other
Vec2
t
number
returns
Vec2

Type declaration

TypeScript
(other: Vec2, t: number) => Vec2;
Parameter
Type and description
other
Vec2
epsilon
number
returns
boolean

Type declaration

TypeScript
(other: Vec2, epsilon: number) => boolean;
method

equals

Parameter
Type and description
other
Vec2
returns
boolean

Type declaration

TypeScript
(other: Vec2) => boolean;
method

data

Type declaration

TypeScript
() => number[]
method

toString

Type declaration

TypeScript
() => string;

class

Vec3

/math/Vec3.ts

A 3-dimensional vector.

method

add

Parameter
Type and description
other
Vec3
returns
Vec3

Type declaration

TypeScript
(other: Vec3) => Vec3;
method

subtract

Parameter
Type and description
other
Vec3
returns
Vec3

Type declaration

TypeScript
(other: Vec3) => Vec3;
method

length

Type declaration

TypeScript
() => number;
method

normalize

Type declaration

TypeScript
() => Vec3;
method

scale

Parameter
Type and description
scalar
number
returns
Vec3

Type declaration

TypeScript
(scalar: number) => Vec3;
method

cross

Parameter
Type and description
other
Vec3
returns
Vec3

Type declaration

TypeScript
(other: Vec3) => Vec3;
method

dot

Parameter
Type and description
other
Vec3
returns
number

Type declaration

TypeScript
(other: Vec3) => number;
method

distance

Parameter
Type and description
other
Vec3
returns
number

Type declaration

TypeScript
(other: Vec3) => number;
method

lerp

Parameter
Type and description
other
Vec3
t
number
returns
Vec3

Type declaration

TypeScript
(other: Vec3, t: number) => Vec3;
Parameter
Type and description
other
Vec3
epsilon
number
returns
boolean

Type declaration

TypeScript
(other: Vec3, epsilon: number) => boolean;
method

equals

Parameter
Type and description
other
Vec3
returns
boolean

Type declaration

TypeScript
(other: Vec3) => boolean;
method

toString

Type declaration

TypeScript
() => string;

class

Vec4

/math/Vec4.ts

A 4-dimensional vector.

method

add

Parameter
Type and description
other
Vec4
returns
Vec4

Type declaration

TypeScript
(other: Vec4) => Vec4;
method

subtract

Parameter
Type and description
other
Vec4
returns
Vec4

Type declaration

TypeScript
(other: Vec4) => Vec4;
method

length

Type declaration

TypeScript
() => number;
method

normalize

Type declaration

TypeScript
() => Vec4;
method

scale

Parameter
Type and description
scalar
number
returns
Vec4

Type declaration

TypeScript
(scalar: number) => Vec4;
method

cross

Parameter
Type and description
other
Vec4
returns
Vec4

Type declaration

TypeScript
(other: Vec4) => Vec4;
method

dot

Parameter
Type and description
other
Vec4
returns
number

Type declaration

TypeScript
(other: Vec4) => number;
method

distance

Parameter
Type and description
other
Vec4
returns
number

Type declaration

TypeScript
(other: Vec4) => number;
method

lerp

Parameter
Type and description
other
Vec4
t
number
returns
Vec4

Type declaration

TypeScript
(other: Vec4, t: number) => Vec4;
method

xy

Type declaration

TypeScript
() => Vec2;
method

zw

Type declaration

TypeScript
() => Vec2;
method

xyz

Type declaration

TypeScript
() => Vec3;
Parameter
Type and description
other
Vec4
epsilon
number
returns
boolean

Type declaration

TypeScript
(other: Vec4, epsilon: number) => boolean;
method

equals

Parameter
Type and description
other
Vec4
returns
boolean

Type declaration

TypeScript
(other: Vec4) => boolean;
method

data

Type declaration

TypeScript
() => number[]
method

toString

Type declaration

TypeScript
() => string;

function

packShelves

/math/packShelves.ts

Takes sizes of rectangles and packs them into a single texture. Width and height will be the next power of two.

Parameter
Type and description
sizes
Vec2[]
returns
Packing

Type declaration

TypeScript
(sizes: Vec2[]) => Packing;

/math/triangulateLine.ts

Given a line defined by a list of points, returns a flat array of points which are vertices of triangles.

Parameter
Type and description
points
Vec2[]
thickness
number
returns
Vec2[]

Type declaration

TypeScript
(points: Vec2[], thickness: number) => Vec2[]

/math/triangulatePolygon.ts

Triangulates a polygon. Assumes that polygon is clockwise.

Parameter
Type and description
polygon
Vec2[]
returns
Vec2[]

Type declaration

TypeScript
(polygon: Vec2[]) => Vec2[]

function

clamp

/math/utils.ts

Makes sure that the given value is within the given range using combination of min() and max().

Parameter
Type and description
value
number
min
number
max
number
returns
number

Type declaration

TypeScript
(value: number, min: number, max: number) => number;

function

lerp

/math/utils.ts

Performs a linear interpolation between two values.

Parameter
Type and description
a
number
b
number
t
number
returns
number

Type declaration

TypeScript
(a: number, b: number, t: number) => number;

function

smoothstep

/math/utils.ts

Performs a smooth interpolation between two values. t should be in the range [0, 1].

Parameter
Type and description
a
number
b
number
t
number
returns
number

Type declaration

TypeScript
(a: number, b: number, t: number) => number;

function

toRadians

/math/utils.ts

Converts degrees to radians.

Parameter
Type and description
degrees
number
returns
number

Type declaration

TypeScript
(degrees: number) => number;

function

toDegrees

/math/utils.ts

Converts radians to degrees.

Parameter
Type and description
radians
number
returns
number

Type declaration

TypeScript
(radians: number) => number;

/math/utils.ts

Returns the next power of two that is greater than or equal to the given value.

Parameter
Type and description
value
number
returns
number

Type declaration

TypeScript
(value: number) => number;

function

intersection

/math/utils.ts

Returns intersection of two rectangles. If there is no intersection, returns a Vec4(0, 0, 0, 0).

Parameter
Type and description
a
Vec4
b
Vec4
returns
Vec4

Type declaration

TypeScript
(a: Vec4, b: Vec4) => Vec4;

function

isInside

/math/utils.ts

Checks if the given point is inside the given rectangle.

Parameter
Type and description
point
Vec2
rectangle
Vec4
returns
boolean

Type declaration

TypeScript
(point: Vec2, rectangle: Vec4) => boolean;

Copyright © Tomasz Czajęcki 2023