Options
All
• Public
• Public/Protected
• All

# Class Vector

A `Vector` contains multi-dimensional numeric data.

since

0.1.0

• Vector

• Point

## Constructors

### constructor

• new Vector(...data: number[]): Vector

## Properties

data: number[]

### dimension

dimension: number

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)
``````

## Accessors

### a

• get a(): number
• set a(n: number): void
• `a` for 'alpha' (the 4th value)

since

0.1.0

#### Returns number

• `a` for 'alpha' (the 4th value)

since

0.1.0

### b

• get b(): number
• set b(n: number): void
• `b` for 'blue' (the 3rd value)

since

0.1.0

#### Returns number

• `b` for 'blue' (the 3rd value)

since

0.1.0

### g

• get g(): number
• set g(n: number): void
• `g` for 'green' (the 2nd value)

since

0.1.0

#### Returns number

• `g` for 'green' (the 2nd value)

since

0.1.0

### r

• get r(): number
• set r(n: number): void
• `r` for 'red' (the 1st value)

since

0.1.0

#### Returns number

• `r` for 'red' (the 1st value)

since

0.1.0

### rgb

• get rgb(): [number, number, number]
• since

0.2.4

### rgba

• get rgba(): [number, number, number, number]
• since

0.2.4

### w

• get w(): number
• set w(n: number): void
• since

0.1.0

• since

0.1.0

### x

• get x(): number
• set x(n: number): void
• since

0.1.0

• since

0.1.0

### xy

• get xy(): [number, number]
• since

0.2.4

### xyz

• get xyz(): [number, number, number]
• since

0.2.4

### xz

• get xz(): [number, number]
• since

0.2.4

### y

• get y(): number
• set y(n: number): void
• since

0.1.0

• since

0.1.0

### yz

• get yz(): [number, number]
• since

0.2.4

### z

• get z(): number
• set z(n: number): void
• since

0.1.0

• since

0.1.0

## Methods

• Add another `Vector` to this `Vector`. This does mutate the `Vector` that calls this method.

since

0.1.0

#### Returns Vector

• Add a scalar number to all of this `Vector`'s values'. This does mutate the `Vector` that calls this method.

since

0.1.0

### clone

• Create a copy of this `Vector`.

since

0.1.0

### dot

• Get the dot product of this `Vector` with another.

since

0.2.4

### index

• index(i: number): number
• 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
``````
since

0.1.0

### length

• length(): number
• Computes the Euclidean length (straight-line length) from the origin to this vector.

since

0.1.0

### lerp

• 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]
``````
since

0.2.4

#### Parameters

• ##### v: Vector

The other vector.

• ##### t: number

The amount by which to interpolate (usually between `0` and `1`, although it can be any number).

#### Returns Vector

• The new, interpolated vector.

### multiplyScalar

• multiplyScalar(n: number): Vector
• 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]
``````
since

0.1.0

### normalize

• 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
``````
since

0.1.0

### rotateZ

• rotateZ(angle: number): Vector
• 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]
``````
since

0.2.2

### set

• set(i: number | "x" | "y" | "z" | "w" | "r" | "g" | "b" | "a", value: number): Vector
• 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.

since

0.1.0

#### Parameters

• ##### i: number | "x" | "y" | "z" | "w" | "r" | "g" | "b" | "a"

The numerical index (0-based) or lowercase string value (e.g. `"x"`) to set.

• ##### value: number

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]
``````

#### Returns Vector

Generated using TypeDoc