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#
| Name | Type | Default value |
|---|---|---|
x | number | 0 |
y | number | 0 |
z | number | 0 |
Defined in#
Properties#
forward#
â–ª Static Readonly forward: Vector
Returns (0, 0, 1).
Defined in#
one#
â–ª Static Readonly one: Vector
Returns (1, 1, 1).
Defined in#
right#
â–ª Static Readonly right: Vector
Returns (1, 0, 0).
Defined in#
up#
â–ª Static Readonly up: Vector
Returns (0, 1, 0).
Defined in#
zero#
â–ª Static Readonly zero: Vector
Returns (0, 0, 0).
Defined in#
Accessors#
x#
• get x(): number
Returns the x-component of the vector.
Returns#
number
Defined in#
• set x(value): void
Modifies the x-component of the vector.
Parameters#
| Name | Type |
|---|---|
value | number |
Returns#
void
Defined in#
xyz#
• get xyz(): [number, number, number]
Returns the xyz-components of the vector, bundled as a copied array.
Returns#
[number, number, number]
Defined in#
• set xyz(values): void
Simultaneously updates the xyz-components of the vector, by passing an array.
Parameters#
| Name | Type |
|---|---|
values | [number, number, number] |
Returns#
void
Defined in#
y#
• get y(): number
Returns the y-component of the vector.
Returns#
number
Defined in#
• set y(value): void
Modifies the y-component of the vector.
Parameters#
| Name | Type |
|---|---|
value | number |
Returns#
void
Defined in#
z#
• get z(): number
Returns the z-component of the vector.
Returns#
number
Defined in#
• set z(value): void
Modifies the z-component of the vector.
Parameters#
| Name | Type |
|---|---|
value | number |
Returns#
void
Defined in#
Methods#
add#
â–¸ add(vector): Vector
Adds the two vectors together, component-wise.
Parameters#
| Name | Type |
|---|---|
vector | Vector |
Returns#
Defined in#
angle#
â–¸ angle(vector): number
Returns the angle between two vectors, in degrees.
Parameters#
| Name | Type |
|---|---|
vector | Vector |
Returns#
number
Defined in#
cross#
â–¸ cross(vector): Vector
Returns the cross-product of two vectors.
Parameters#
| Name | Type |
|---|---|
vector | Vector |
Returns#
Defined in#
dot#
â–¸ dot(vector): number
returns the dot-product of two vectors.
Parameters#
| Name | Type |
|---|---|
vector | Vector |
Returns#
number
Defined in#
magnitude#
â–¸ magnitude(): number
Returns the length of the vector.
Returns#
number
Defined in#
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#
Defined in#
scale#
â–¸ scale(scalar): Vector
Scales the lefthand vector by another vector or by a number.
Parameters#
| Name | Type |
|---|---|
scalar | number | Vector |
Returns#
Defined in#
sqrMagnitude#
â–¸ sqrMagnitude(): number
Returns the squared length of the vector.
Returns#
number
Defined in#
subtract#
â–¸ subtract(vector): Vector
Subtracts the right vector from the left one, component-wise.
Parameters#
| Name | Type |
|---|---|
vector | Vector |
Returns#
Defined in#
toString#
â–¸ toString(): string
Returns a formatted representation of the vector.
Returns#
string
Defined in#
from2dAngle#
â–¸ Static from2dAngle(angle): Vector
Creates a new vector from an angle, in degrees. Note that the z-component will be zero.
Parameters#
| Name | Type |
|---|---|
angle | number |