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.
Functions
Section titled “Functions”vec.V2
Section titled “vec.V2”vec.V2(x: number, y: number): Vec2Create a new 2d vector
vec.fromAngle
Section titled “vec.fromAngle”vec.fromAngle(angle: number, length: number?): Vec2Create 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
Section titled “Vec2:scale”Vec2:scale(k: number): Vec2Scales the vector by the given factor
Vec2:cmul
Section titled “Vec2:cmul”Vec2:cmul(b: Vec2): Vec2Performs 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
Section titled “Vec2:dot”Vec2:dot(b: Vec2): numberReturns the dot product of the two vectors
Vec2:length
Section titled “Vec2:length”Vec2:length(): numberReturns the length of the vector, defined as math.sqrt(v.x*v.x + v.y*v.y)
Vec2:lengthSq
Section titled “Vec2:lengthSq”Vec2:lengthSq(): numberReturns the square of the length of the vector, defined as v.x*v.x + v.y*v.y
Vec2:distance
Section titled “Vec2:distance”Vec2:distance(b: Vec2): numberReturns the distance between the two vectors.
This is the same as (a - b):length()
Vec2:angle
Section titled “Vec2:angle”Vec2:angle(): numberReturns the angle (in radians) of the vector
Vec2:normalized
Section titled “Vec2:normalized”Vec2:normalized(): Vec2Returns a normalized version of the vector If the vector has zero length, returns a unit vector pointing in an arbitrary direction
Vec2:abs
Section titled “Vec2:abs”Vec2:abs(): Vec2Returns a vector with all components absolute values
Vec2:round
Section titled “Vec2:round”Vec2:round(digits_of_precision: number?): Vec2Returns 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
Section titled “Vec2:floor”Vec2:floor(): Vec2Returns a vector with all components rounded down to the nearest integer
Vec2:ceil
Section titled “Vec2:ceil”Vec2:ceil(): Vec2Returns a vector with all components rounded up to the nearest integer
Vec2:max
Section titled “Vec2:max”Vec2:max(b: Vec2): Vec2Returns the component-wise maximum of the two vectors
Vec2:min
Section titled “Vec2:min”Vec2:min(b: Vec2): Vec2Returns the component-wise minimum of the two vectors
Vec2:lerp
Section titled “Vec2:lerp”Vec2:lerp(b: Vec2, k: number): Vec2Returns a vector linearly interpolated between a and b by the factor k
Vec2:sign
Section titled “Vec2:sign”Vec2:sign(): Vec2Returns the vector containing the sign of each component of the vector
Vec2:noise
Section titled “Vec2:noise”Vec2:noise(): numberReturns the noise value at the given point Uses simplex noise
Vec2:worleyNoise
Section titled “Vec2:worleyNoise”Vec2:worleyNoise(): numberReturns the noise value at the given point Uses Worley noise
Vec2:add
Section titled “Vec2:add”Vec2:add(b: Vec2): Vec2Adds b to a, modifying a in-place
Vec2:sub
Section titled “Vec2:sub”Vec2:sub(b: Vec2): Vec2Subtracts b from a, modifying a in-place. Returns a.
Vec2:mul
Section titled “Vec2:mul”Vec2:mul(b: Vec2): Vec2Multiplies a by b, modifying a in-place. Returns a.
Vec2:rescale
Section titled “Vec2:rescale”Vec2:rescale(k: number): Vec2Multiplies a by k, modifying a in-place. Returns a.
Vec2Data
Section titled “Vec2Data”export type Vec2Data = ...The type of 2d things
Constants
Section titled “Constants”vec.ZERO2
Section titled “vec.ZERO2”vec.ZERO2 = ...The zero vector
