A two dimensional vector
import {Vector2} from '@math.gl/core';
const vector = new Vector2(1, 1);
Vector2
extends Vector
extends MathArray
extends Array
Many commonly used Vector2
methods are inherited from Vector
and MathArray
:
Vector2.clone()
Vector2.copy(array)
Vector2.set(...args)
Vector2.fromArray(array, offset = 0)
Vector2.toString()
Vector2.toArray(array = [], offset = 0)
Vector2.equals(array)
Vector2.exactEquals(array)
Vector2.validate(array = this)
Vector2.check(array = this)
Vector2.normalize()
Also note that Vector2
is a subclass of the built in JavaScript Array
and can thus be used wherever an Array is expected. It can e.g. supplied as a parameter to any function expecting an Array
.
Gets or sets element 0 or 1 respectively
Creates a new, empty Vector2
, or copies an existing Vector2
constructor((x = 0), (y = 0));
constructor([x, y]);
set(x, y)
Add zero or more vectors to current vector.
add(...vectors)
Subtract zero or more vectors from current vector
subtract(...vectors)
Multiply zero or more vectors with current vector
multiply(...vectors)
Divide zero or more vectors with current vector
divide(...vectors)
scale(scale)
scaleAndAdd(vector, scale)
negate()
normalize()
dot(vector)
lerp(vector, coeff)
Calculates counterclockwise angle in radians starting from positive x axis
horizontalAngle()
Note: returns Math.atan2(this.y, this.x)
Calculates clockwise angle in radians starting from positive y axis
verticalAngle()
Note: returns Math.atan2(this.x, this.y)
Equivalent to transformAsPoint
.
Transforms this vector by the provided 4x4 matrix as a point (i.e includes translations).
Note: Implicitly extends the vector to [x, y, 0, 1]
before applying the 4x4 transformation.
Transforms this vector by the provided 4x4 matrix as a vector (i.e does not include translations).
Note: Implicitly extends the vector to [x, y, 0, 0]
before applying the 4x4 transformation.
Transforms this vector by the provided 3x3 matrix.
Transforms this vector by the provided 2x3 matrix (A pure 2D transform that can incorporate translations).
Transforms this vector by the provided 2x2 matrix.