glector

Types

pub type Vector2 {
  Vector2(x: Float, y: Float)
}

Constructors

  • Vector2(x: Float, y: Float)

Constants

pub const zero: Vector2

The zero vector, or null vector (0, 0).

Functions

pub fn add(a: Vector2, b: Vector2) -> Vector2

Adds vector a to vector b.

add(Vector2(1.0, 2.0), Vector2(3.0, 4.0)) // -> Vector2(4.0, 6.0)
pub fn cross(a: Vector2, b: Vector2) -> Float

Gets the value of the z component of the cross product of vectors a and b.

Note: the 2D vectors a and b are treated as 3D vectors where their z component is zero.

cross(Vector2(1.0, 2.0), Vector2(3.0, 4.0)) // -> -2.0
pub fn dot(a: Vector2, b: Vector2) -> Float

Gets the dot product of vectors a and b.

dot(Vector2(-6.0, 8.0), Vector2(5.0, 12.0)) // -> 66.0
pub fn invert(v: Vector2) -> Vector2

Flips or reverses a vector’s direction.

invert(Vector2(1.0, 2.0)) // -> Vector2(-1.0, -2.0)
pub fn length(v: Vector2) -> Float

Gets the length of a vector.

Note: it’s faster to get a vector’s square length than its actual length. So for certain use cases like comparing the length of 2 vectors, I recommend using length_sq instead.

length(Vector2(8.0, -6.0)) // -> 10.0
pub fn length_sq(v: Vector2) -> Float

Gets the square length of a vector.

length_sq(Vector2(8.0, -6.0)) // -> 100.0
pub fn lerp(a: Vector2, b: Vector2, t: Float) -> Vector2

Gets the linear interpolation between two vectors.

lerp(Vector2(10.0, 0.0), Vector2(0.0, -10.0), 0.5) // -> Vector2(5.0, -5.0)
pub fn multiply(a: Vector2, b: Vector2) -> Vector2

Multiplies the x and y components of vectors a and b.

multiply(Vector2(1.0, 2.0), Vector2(3.0, 4.0)) // -> Vector2(3.0, 8.0)
pub fn normal(v: Vector2) -> Vector2

Gets the normal of a vector representing an edge. The winding direction is assumed to be counter-clockwise.

normal(Vector2(-123.0, 0.0)) // -> Vector2(0.0, -1.0)
pub fn normalize(v: Vector2) -> Vector2

Scales a vector such that its length is 1.0.

normalize(Vector2(100.0, 0.0)) // -> Vector2(1.0, 0.0)
pub fn scale(v: Vector2, scalar: Float) -> Vector2

Multiplies both components of vector v by scalar.

scale(Vector2(1.0, 2.0), 2.0) // -> Vector2(2.0, 4.0)
pub fn subtract(a: Vector2, b: Vector2) -> Vector2

Subtracts vector b from vector a.

subtract(Vector2(1.0, 2.0), Vector2(3.0, 4.0)) // -> Vector2(-2.0, -2.0)
pub fn swap(v: Vector2) -> Vector2

Swaps the x and y components of a vector around.

swap(Vector2(1.0, -2.0)) // -> Vector2(-2.0, 1.0)
Search Document