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
xcomponent.
y: number
- The vector's
ycomponent.
Constructors
()
- Default constructor.
xandyare initialized to0.
(n: number)
- Initialize
xandyton.
(x: number, y: number)
- Explicitly initialize
xandy.
Instance Methods
add(v: Vector2|number): Vector2
- Adds the like components of
vinto 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
vout 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
vinto 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
vinto 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
v1andv2
cross(v1: Vector2, v2: Vector2): number
- Returns the cross product of
v1andv2. - 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
v1andv2at timet. tis a value representing a period in time where0will returnv1(start) and1will returnv2end.- Important
tis not clamped, the linear period can be extended beyond bounds. - Optionally,
outcan 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
v1andv2when 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
v1andv2when each is considered a coordinate vector. - Optionally,
outcan 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
v1andv2in the range -PI to PI.