The dimension of this Vector
, or the last index that has a value (plus one).
const a = new Vector(1, 1, 1);
a.dimension; // 3
const b = new Vector(1, 2, 3, 4, 5);
b.dimension; // 5
Dimensions can be dynamically updated after a Vector
is created:
const v = new Vector(0);
v.dimension; // 1
v.set(3, 10);
v.dimension; // 4 (indices start at 0)
a
for 'alpha' (the 4th value)
a
for 'alpha' (the 4th value)
b
for 'blue' (the 3rd value)
b
for 'blue' (the 3rd value)
g
for 'green' (the 2nd value)
g
for 'green' (the 2nd value)
r
for 'red' (the 1st value)
r
for 'red' (the 1st value)
Add a scalar number to all of this Vector
's values'. This does mutate the Vector
that calls this method.
Create a copy of this Vector
.
Get the dot product of this Vector
with another.
Retrieve a value from a Vector
by its index. If the given index is greater than the
Vector
's dimension, this returns 0
by default.
const v = new Vector(1, 2, 4);
v.index(0); // returns 1
v.index(2); // returns 4
v.index(5); // returns 0
Computes the Euclidean length (straight-line length) from the origin to this vector.
Linearly interpolate between this Vector
and another Vector
. This does not mutate the original Vector
that calls this method, but returns a new Vector
.
const a = new Vector(1, 3, -5);
const b = new Vector(4, -2);
a.lerp(b, 0); // returns a clone of Vector a
a.lerp(b, 1); // returns a clone of Vector b
const mid = a.lerp(b, 0.5); // returns a Vector halfway between a and b
mid.xyz; // returns [2.5, 0.5, -2.5]
The other vector.
The amount by which to interpolate (usually between 0
and 1
, although it can be any number).
Multiply this Vector
by a scalar number. This does mutate the Vector
that calls this method.
const v = new Vector(1, 2);
v.multiplyScalar(5);
v.xy; // returns [5, 10]
v.multiplyScalar(-0.5);
v.xy; // returns [-2.5, -5]
Normalize the Vector
(turn it into a Vector
with length = 1
). Has no effect on the 0 Vector
. This does mutate the Vector
that calls this method.
const v = new Vector(5, 3, -1);
v.normalize();
v.length(); // returns 1
Rotate the Vector
about the z-axis by angle
radians (updating its x
and y
values). This does mutate the Vector
that calls this method.
const v = new Vector(1, 0);
v.rotateZ(Math.PI / 2); // rotate by PI / 2 radians = 90 degrees
v.xy; // returns [0, 1]
Set the value at a given index. If the index is greater than the dimension
of this Vector
, the dimension will be increased to the dimensionality implied by the index.
The numerical index (0-based) or lowercase string value (e.g. "x"
) to set.
The value to set at this index/position.
const vector = new Vector();
vector.set(0, 10);
vector.set('y', 2);
vector.set(2, 4);
vector.xyz; // [10, 2, 4]
Generated using TypeDoc
A
Vector
contains multi-dimensional numeric data.0.1.0