Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Vector

A Vector contains multi-dimensional numeric data.

since

0.1.0

Hierarchy

  • Vector

Implements

  • Point

Index

Constructors

constructor

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

Properties

data

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

b

  • get b(): number
  • set b(n: number): void

g

  • get g(): number
  • set g(n: number): void

r

  • get r(): number
  • set r(n: number): void

rgb

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

rgba

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

w

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

x

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

xy

  • get xy(): [number, number]

xyz

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

xz

  • get xz(): [number, number]

y

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

yz

  • get yz(): [number, number]

z

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

Methods

add

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

    since

    0.1.0

    Parameters

    Returns Vector

addScalar

  • addScalar(n: number): 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

    Parameters

    • n: number

    Returns Vector

clone

dot

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

    Parameters

    • i: number

    Returns number

length

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

    since

    0.1.0

    Returns number

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

    Parameters

    • n: number

    Returns Vector

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

    Returns Vector

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

    Parameters

    • angle: number

    Returns Vector

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