Vector4
is designed to hold three dimensional coordinates in projective space. Using the projective representation allows JavaScript applications to perform the same calculations that the GPU does.
A main feature of vectors is that they can be transformed by matrices and quaternions. And Vector4
s are particular general when transformed with 4x4 matrices (Matrix4
or just arrays of 16 numbers), as those can include translations, projections and other transformations that cannot be expressed by e.g. 3x3 matrices or quaternions alone.
Note that the fourth element w
is not a coordinate but a scaling factor. The fourth component (w
) is usually set to either
0
to represent a vector1
to represent a pointVector4
methods will keep the vector scaled so that w
(if non-zero) is 1
.
The math behind Vector4
comes from projective geometry, which significantly generalizes calculations and removes a number of special cases compared to affine geometry. It is not necessary to understand the details to use Vector4
, but see the developer guide for some additional xbackground.
import {Vector4} from '@math.gl/core';
const vector = new Vector4(1, 1, 1, 0);
const point = new Vector4(0, 0, 0, 1);
Vector4
extends Vector
extends MathArray
extends Array
Gets or sets element 0, 1, 2 or 3 respectively
Many of the most commonly used Vector2
methods are inherited from MathArray
:
Vector4.clone()
Vector4.copy(array)
Vector4.set(...args)
Vector4.fromArray(array, offset = 0)
Vector4.toString()
Vector4.toArray(array = [], offset = 0)
Vector4.equals(array)
Vector4.exactEquals(array)
Vector4.validate(array = this)
Vector4.check(array = this)
Vector4.normalize()
Note that Vector2
is a subclass of the built in JavaScript Array
and can thus e.g. be supplied as a parameter to any function expecting an Array
.
new Vector4(x = 0, y = 0, z = 0, w = 0)
Creates a new, empty Vector4
Updates a Vector4
Returns the distance to the specifed Vector.
Returns the squared distance to the specifed Vector. Fast to calculate than distance and often sufficient for e.g. sorting etc.
Calculates the dot product with the supplied vector
.
add(...vectors)
subtract(...vectors)
multiply(...vectors)
divide(...vectors)
scale(scale)
Negates each element in the vector.
Inverses (x = 1/x
) each element in the vector.
Normalizes the vector. Same direction but len()
will now return 1
.
Linearly interpolates between the vectors current value and the supplied vector
.
Equivalent to transformByMatrix4
.
Transforms a vector by the provided 4x4 matrix.
Note: Scales the resulting vector to ensure that w
, if non-zero, is set to 1
.
Transforms the vector's x
, y
and z
values by the provided 3x3 matrix.
Transform the vector's x
and y
values by the provided 2x2 matrix.
Transform the vector by the provided quaternion
.