Skip to content

Vec

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

Module for 2D vectors. Because 2D vectors are so common (your screen is 2D!), this module is called vec instead of vec2. See vec4 for 3D and 4D vectors.

vec.V2(x: number, y: number): Vec2

Create a new 2d vector

vec.fromAngle(angle: number, length: number?): Vec2

Create a new 2d vector of length length pointing towards the given angle (in radians) If length is not provided, it defaults to 1 This is the same as: V2(math.cos(angle), math.sin(angle)) * length

export type Vec2 = ...

Implementation of a 2D vector

Operators

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

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

-a — (a: Vec2): Vec2

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

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

Vec2:scale(k: number): Vec2

Scales the vector by the given factor

Vec2:cmul(b: Vec2): Vec2

Performs the complex multiplication of a and b, treating them as complex numbers (a.x + i a.y) * (b.x + i b.y) = (a.x*b.x - a.y*b.y) + i(a.x*b.y + a.y*b.x)

Vec2:dot(b: Vec2): number

Returns the dot product of the two vectors

Vec2:length(): number

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

Vec2:lengthSq(): number

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

Vec2:distance(b: Vec2): number

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

Vec2:angle(): number

Returns the angle (in radians) of the vector

Vec2:normalized(): Vec2

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

Vec2:abs(): Vec2

Returns a vector with all components absolute values

Vec2:round(digits_of_precision: number?): Vec2

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)

Vec2:floor(): Vec2

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

Vec2:ceil(): Vec2

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

Vec2:max(b: Vec2): Vec2

Returns the component-wise maximum of the two vectors

Vec2:min(b: Vec2): Vec2

Returns the component-wise minimum of the two vectors

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

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

Vec2:sign(): Vec2

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

Vec2:noise(): number

Returns the noise value at the given point Uses simplex noise

Vec2:worleyNoise(): number

Returns the noise value at the given point Uses Worley noise

Vec2:add(b: Vec2): Vec2

Adds b to a, modifying a in-place

Vec2:sub(b: Vec2): Vec2

Subtracts b from a, modifying a in-place. Returns a.

Vec2:mul(b: Vec2): Vec2

Multiplies a by b, modifying a in-place. Returns a.

Vec2:rescale(k: number): Vec2

Multiplies a by k, modifying a in-place. Returns a.

export type Vec2Data = ...

The type of 2d things

vec.ZERO2 = ...

The zero vector