Skip to content

Vec4

const vec4 = require("@vectarine/vec4")

Module for 4D vectors. 4D vectors can represent colors and 3D rotations. You can use them for 3D by ignoring the last component.

vec4.V4(x: number, y: number, z: number, w: number): Vec4

Create a new 4D vector

vec4.fromRotation(axis: Vec4, angle: number): Vec4

Returns a quaternion representing a rotation of angle radians around the given axis The 4th component of the axis is ignored, it is treated as a 3d vector. The length of the result is always 1.

vec4.createColor(r: number, g: number, b: number, a: number): Vec4

Alias for V4, but indicate that the Vec4 will be used as a color.

export type Vec4 = ...

Implementation of a 4D vector

Operators

a + b — (a: Vec4, b: Vec4): Vec4

a - b — (a: Vec4, b: Vec4): Vec4

-a — (a: Vec4): Vec4

a * b — (a: Vec4, b: Vec4): Vec4

a / b — (a: Vec4, b: Vec4): Vec4

Vec4:scale(k: number): Vec4

Scales the vector by the given factor

Vec4:qmul(b: Vec4): Vec4

Performs a quaternion multiplication of a and b, treating them as quaternions This is useful to apply rotations to 3D vectors Note that a:qmul(b) != b:qmul(a) in general

Vec4:dot(b: Vec4): number

Returns the dot product of the two vectors

Vec4:length(): number

Returns the length of the vector, defined as math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w)

Vec4:lengthSq(): number

Returns the square of the length of the vector, defined as v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w

Vec4:distance(b: Vec4): number

Returns the distance between the two vectors. This is the same as (a - b):length()

Vec4:normalized(): Vec4

Returns a normalized version of the vector If the vector has zero length, returns a unit vector pointing in an arbitrary direction

Vec4:abs(): Vec4

Returns a vector with all components absolute values

Vec4:round(digits_of_precision: number?): Vec4

Returns a version of the vector rounded to the given number of digits of precision

If digits_of_precision is not provided, it defaults to 0 (round to nearest integer)

Vec4:floor(): Vec4

Returns a vector with all components rounded down to the nearest integer

Vec4:ceil(): Vec4

Returns a vector with all components rounded up to the nearest integer

Vec4:max(b: Vec4): Vec4

Returns the component-wise maximum of the two vectors

Vec4:min(b: Vec4): Vec4

Returns the component-wise minimum of the two vectors

Vec4:lerp(b: Vec4, k: number): Vec4

Returns a vector linearly interpolated between a and b by the factor k

Vec4:sign(): Vec4

Returns the vector containing the sign of each component of the vector

Vec4:noise(): number

Returns the noise value at the given point Uses simplex noise

Vec4:worleyNoise(): number

Returns the noise value at the given point Uses Worley noise

export type Vec4Data = ...

The type of 4D things

vec4.ZERO4 = ...

The zero vector

vec4.BLACK = ...
vec4.WHITE = ...
vec4.TRANSPARENT = ...
vec4.RED = ...
vec4.GREEN = ...
vec4.BLUE = ...
vec4.YELLOW = ...
vec4.MAGENTA = ...
vec4.CYAN = ...
vec4.AZURE = ...
vec4.ORANGE = ...
vec4.PURPLE = ...
vec4.SPRING = ...
vec4.LIME = ...
vec4.PINK = ...
vec4.LIGHT_GRAY = ...
vec4.DARK_GRAY = ...
vec4.GRAY = ...
vec4.DARK_RED = ...
vec4.DARK_GREEN = ...
vec4.DARK_BLUE = ...