Foundation.Vector2

A 2 vector implementation optimized for in-place operations.


Implementation Details

Reference paths

  • '/foundation/_digest.ts'
  • '/foundation/core/math/_digest.ts'
  • '/foundation/core/math/Vector2.ts'

Extends

This type does not extend a base type.

Implements

This type implements the following interfaces:

All implementations exhibit default defined behaviors.

Storage types

Components x and y are stored internally in a 2-slot Float32Array. This should be kept in mind with respect to the precision of those values when accessing them.


Public Properties

x: number

  • The vector's x component.

y: number

  • The vector's y component.

Constructors

()

  • Default constructor.
  • x and y are initialized to 0.

(n: number)

  • Initialize x and y to n.

(x: number, y: number)

  • Explicitly initialize x and y.

Instance Methods

add(v: Vector2|number): Vector2

  • Adds the like components of v into the current vector. Or, add a scalar value to both components.
  • Returns the current vector for chaining.

subtract(v: Vector2|number): Vector2

  • Subtracts the like components of v out of the current vector. Or, subtract a scalar value from both components.
  • Returns the current vector for chaining.

multiply(v: Vector2|number): Vector2

  • Multiplies the like components of v into the current vector. Or, multiply both components by a scalar value.
  • Returns the current vector for chaining.

divide(v: Vector2|number): Vector2

  • Divides the like components of v into the current vector. Or, divide both components by a scalar value.
  • Returns the current vector for chaining.

negate(): Vector2

  • Reverses the current vector's direction.
  • Returns the current vector for chaining.

normalize(): Vector2

  • Converts the current vector to a unit vector. I.e. gives it a magnitude of 1.
  • Returns the current vector for chaining.

Static Functions

magnitude(v: Vector2): number

  • Returns the magnitude (length) of v

dot(v1: Vector2, v2: Vector2): number

  • Returns the dot (scalar) product of v1 and v2

cross(v1: Vector2, v2: Vector2): number

  • Returns the cross product of v1 and v2.
  • Important: in 2D space this returns a scalar value which is equivalent to the area of the parallelogram formed by the two vectors.

lerp(v1: Vector2, v2: Vector2, t: number, out?: Vector2): Vector2

  • Returns a new vector which is the linear interpolation between v1 and v2 at time t.
  • t is a value representing a period in time where 0 will return v1 (start) and 1 will return v2 end.
  • Important t is not clamped, the linear period can be extended beyond bounds.
  • Optionally, out can be passed to serve as the output vector, to avoid instantiation of a new object.

distance(v1: Vector2, v2: Vector2): number

  • Returns the distance between v1 and v2 when each is considered a coordinate vector.

midpoint(v1: Vector2, v2: Vector2, t: number, out?: Vector2): Vector2

  • Returns a new vector which is the midpoint of the line segment formed by v1 and v2 when each is considered a coordinate vector.
  • Optionally, out can be passed to serve as the output vector, to avoid instantiation of a new object.

angle(v1: Vector2, v2: Vector2): number

  • Returns the radian angle between v1 and v2 in the range -PI to PI.