Skip to main content

Class: Vector

components.Vector

Represents a structure used to process vectors.

remarks Note that the operations in this class will not modify the original vector, except for the property assignments. This is to ensure that vectors are not unintentionally modified.

example

const vectorA = new Vector(1, 3, 5);const vectorB = new Vector(2, 3, 1);const vectorC = vectorA.add(vectorB); // (3, 6, 6)

Constructors#

constructor#

• new Vector(x?, y?, z?)

Creates a new vector with optional x-, y-, and z-components. Omitted components are defaulted to 0.

Parameters#

NameTypeDefault value
xnumber0
ynumber0
znumber0

Defined in#

components/vector.ts:73

Properties#

forward#

â–ª Static Readonly forward: Vector

Returns (0, 0, 1).

Defined in#

components/vector.ts:102


one#

â–ª Static Readonly one: Vector

Returns (1, 1, 1).

Defined in#

components/vector.ts:90


right#

â–ª Static Readonly right: Vector

Returns (1, 0, 0).

Defined in#

components/vector.ts:94


up#

â–ª Static Readonly up: Vector

Returns (0, 1, 0).

Defined in#

components/vector.ts:98


zero#

â–ª Static Readonly zero: Vector

Returns (0, 0, 0).

Defined in#

components/vector.ts:86

Accessors#

x#

• get x(): number

Returns the x-component of the vector.

Returns#

number

Defined in#

components/vector.ts:22

• set x(value): void

Modifies the x-component of the vector.

Parameters#

NameType
valuenumber

Returns#

void

Defined in#

components/vector.ts:28


xyz#

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

Returns the xyz-components of the vector, bundled as a copied array.

Returns#

[number, number, number]

Defined in#

components/vector.ts:61

• set xyz(values): void

Simultaneously updates the xyz-components of the vector, by passing an array.

Parameters#

NameType
values[number, number, number]

Returns#

void

Defined in#

components/vector.ts:67


y#

• get y(): number

Returns the y-component of the vector.

Returns#

number

Defined in#

components/vector.ts:35

• set y(value): void

Modifies the y-component of the vector.

Parameters#

NameType
valuenumber

Returns#

void

Defined in#

components/vector.ts:41


z#

• get z(): number

Returns the z-component of the vector.

Returns#

number

Defined in#

components/vector.ts:48

• set z(value): void

Modifies the z-component of the vector.

Parameters#

NameType
valuenumber

Returns#

void

Defined in#

components/vector.ts:54

Methods#

add#

â–¸ add(vector): Vector

Adds the two vectors together, component-wise.

Parameters#

NameType
vectorVector

Returns#

Vector

Defined in#

components/vector.ts:121


angle#

â–¸ angle(vector): number

Returns the angle between two vectors, in degrees.

Parameters#

NameType
vectorVector

Returns#

number

Defined in#

components/vector.ts:174


cross#

â–¸ cross(vector): Vector

Returns the cross-product of two vectors.

Parameters#

NameType
vectorVector

Returns#

Vector

Defined in#

components/vector.ts:187


dot#

â–¸ dot(vector): number

returns the dot-product of two vectors.

Parameters#

NameType
vectorVector

Returns#

number

Defined in#

components/vector.ts:198


magnitude#

â–¸ magnitude(): number

Returns the length of the vector.

Returns#

number

Defined in#

components/vector.ts:107


normalized#

â–¸ normalized(): Vector

Normalizes the vector to a length of 1. If the length was previously zero, then a zero-length vector will be returned.

Returns#

Vector

Defined in#

components/vector.ts:163


scale#

â–¸ scale(scalar): Vector

Scales the lefthand vector by another vector or by a number.

Parameters#

NameType
scalarnumber | Vector

Returns#

Vector

Defined in#

components/vector.ts:143


sqrMagnitude#

â–¸ sqrMagnitude(): number

Returns the squared length of the vector.

Returns#

number

Defined in#

components/vector.ts:114


subtract#

â–¸ subtract(vector): Vector

Subtracts the right vector from the left one, component-wise.

Parameters#

NameType
vectorVector

Returns#

Vector

Defined in#

components/vector.ts:132


toString#

â–¸ toString(): string

Returns a formatted representation of the vector.

Returns#

string

Defined in#

components/vector.ts:209


from2dAngle#

â–¸ Static from2dAngle(angle): Vector

Creates a new vector from an angle, in degrees. Note that the z-component will be zero.

Parameters#

NameType
anglenumber

Returns#

Vector

Defined in#

components/vector.ts:216