Skip to content

Vector2

Eric Yancey Dauenhauer edited this page Feb 5, 2024 · 3 revisions

The root of all that is good!

Vector2 is a supremely useful class to represent a 2D Cartsian point in space. Here are a list of methods and properties.

Constructor

constructor(x: number, y: number)

new Vector2(3, 4)

Or, with the vec2 helper function

vec2(3, 4)

add

add(other: Vector2): Vector2

Adds the two vectors together with vector addition.

Examples

  • vec2(2, 4).add(vec2(2, 4)) returns Vector2 { x: 4, y: 8 }

subtract

subtract(other: Vector2): Vector2

Subtracts other from `this with vector subtraction.

Examples

  • vec2(2, 4).subtract(vec2(1, 2)) returns Vector2 { x: 1, y: 2 }

divide

divide(n: number): Vector2

Divides the Vector2 by the scalar n.

Examples

  • vec2(2, 4).divide(2) returns Vector2 { x: 1, y: 2 }

multiply

multiply(n: number): Vector2

Multiplies the Vector2 by the scalar n.

Examples

  • vec2(2, 4).multiply(2) returns Vector2 { x: 4, y: 8 }

scale

Alias for multiply

static mix

Vector2.mix(a: Vector2, b: Vector2, mix: number): Vector2

Linearly interpolates between vectors a and b. This method might be called lerp in other frameworks.

Examples

  • Vector2.mix(vec2(10, 0), vec2(0, 0), 0.5) returns Vector2 { x: 5, y: 0}

distanceTo

distanceTo(other: Vector2): number

Returns the Euclidean distance to the other vector.

Examples

  • vec2(0, 0).distanceTo(vec2(3, 4)) returns 5

length

lenght(): number

Returns the Euclidean length of the vector.

Examples

  • vec2(3, 4).length() returns 5

dot

dot(other: Vector2): Vector2

Returns the dot product of the two vectors.

Examples

  • vec2(2, 3).dot(vec2(3, 4)) returns Vector2 { x: 6, y: 12 }

angleTo

angleTo(other: Vector2): Radians

Returns the angle made between the two vectors.

Examples

  • vec2(2, 0).angleTo(vec2(0, 2)) returns -Math.PI / 2

static midpoint

Vector2.midpoint(a: Vector2, b: Vector2): Vector2

Returns a vector at the midpoint of a and b.

Examples

  • Vector2.midpoint(vec2(0, 0), vec2(4, 4)) returns Vector2 { x: 2, y: 2 }

static random

Vector2.random(xMin: number, xMax: number, yMin: number, yMax: number, rng: Rng): Vector2

Returns a random point in the provided bounds.

Examples

  • Vector2.random(0, 10, 15, 20, rng) returns some Vector with x in range [0, 10) and y in range [15, 20)

static randomInCircle

Vector2.randomInCircle(circle: Circle, rng: Rng): Vector2

Returns a random point in the provided circle.

Examples

  • Vector2.randomInCircle(circle({ center: vec2(6, 8), radius: 5 }), rng) returns some Vector2 within a distance of 5 from the point centered at (6, 8)

static fromAngle

Vector2.fromAngle(angle: number): Vector2

Returns a Vector2 with length 1 pointing in direction of angle

Examples

  • Vector2.fromAngle(0) returns Vector2 { x: 1, y: 0 }
  • Vector2.fromAngle(Math.PI) returns Vector2 { x: -1, y: 0 }
  • Vector2.fromAngle(Math.PI / 2) returns Vector2 { x: 0, y: 1 }
  • Vector2.fromAngle(-Math.PI / 2) returns Vector2 { x: 0, y: -1 }

jitter

jitter(amount: number, rng): Vector2

Returns a new Vector2 with x and y offset by a random amount in range [x - amount, x + amount), [y - amount, y + amount)

Examples

  • vec2(2, 3).jitter(0.5, rng) returns some Vector2 with x in range [1.5, 2.5) and y in range [2.5, 3.5)

eq

eq(other: Vector2): boolean

A simple value-equality check. Returns true when the x and y values are equivalent.

Examples

  • vec2(3, 3).eq(vec2(3, 3)) returns true
  • vec2(3, 3).eq(vec2(3, 4)) returns false