Skip to content

Low-level mathematical primitives for Euclidean geometry: 2D/3D vectors (vec2, vec3), 4D vectors (vec4), 4×4 matrices (mat4), planes, and 2D/3D lines. All data structures are plain arrays treated as immutable; each operation returns a new value via an output parameter.

See also:

namespace vec2

2D vector operations (immutable style — each function returns a new or output vector).

Common operations: create, add, subtract, scale, normalize, dot, cross, distance, lerp, rotate, angle, fromAngleDegrees, fromAngleRadians, snap, transform.

const { vec2 } = jscad.maths
const a = vec2.fromValues(3, 4)
const b = vec2.fromValues(1, 0)
const sum = vec2.add(vec2.create(), a, b) // [4, 4]
const len = vec2.length(a) // 5

See also:

namespace vec3

3D vector operations (immutable style — each function returns a new or output vector).

Common operations: create, add, subtract, scale, normalize, dot, cross, distance, lerp, rotateX, rotateY, rotateZ, angle, squaredLength, fromVec2, snap, transform.

const { vec3 } = jscad.maths
const a = vec3.fromValues(1, 0, 0)
const b = vec3.fromValues(0, 1, 0)
const c = vec3.cross(vec3.create(), a, b) // [0, 0, 1]
const d = vec3.normalize(vec3.create(), vec3.fromValues(3, 0, 4)) // [0.6, 0, 0.8]

See also:

namespace mat4

4×4 matrix operations for building and composing transforms.

Common operations: create, identity, multiply, invert, fromTranslation, fromScaling, fromRotation, fromXRotation, fromYRotation, fromZRotation, fromTaitBryanRotation, translate, rotate, scale, mirror, mirrorByPlane, leftMultiplyVec3, rightMultiplyVec3.

Useful for building custom transform matrices and applying them to geometry via the transforms module.

const { mat4, vec3 } = jscad.maths
const m = mat4.multiply(mat4.create(),
mat4.fromTranslation(mat4.create(), vec3.fromValues(5, 0, 0)),
mat4.fromXRotation(mat4.create(), Math.PI / 4)
)

See also:

namespace plane

3D plane operations (a plane is a vec4 [nx, ny, nz, w] where [nx, ny, nz] is the normal).

Common operations: create, clone, copy, fromNormalAndPoint, fromPoints, flip, signedDistanceToPoint, intersectSub.

const { plane } = jscad.maths
const p = plane.fromPoints(plane.create(),
[0,0,0], [1,0,0], [0,1,0]
)
const d = plane.signedDistanceToPoint(p, [0, 0, 5]) // 5 (above XY plane)

See also:

namespace line2

2D line operations (a line2 is a vec3 [normal.x, normal.y, distance]).

Common operations: create, clone, copy, fromPoints, direction, intersectToPoint, distanceToPoint, reverse, transform, closestPoint.

const { line2 } = jscad.maths
const l = line2.fromPoints(line2.create(), [0, 0], [1, 1])

See also:

namespace line3

3D line operations (a line3 is two vec3 points — origin and direction).

Common operations: create, clone, copy, fromPoints, direction, intersectToPoints, distanceToPoint, reverse, transform, closestPoint.

const { line3 } = jscad.maths
const l = line3.fromPoints(line3.create(), [0,0,0], [1,1,1])

See also: