Hide menu
Loading...
Searching...
No Matches
Vector

Defines a 3D vector.

Constructors

Constructor

‍new Vector(x?, y?, z?): Vector

Creates a vector from coordinates.

If all coordinates are omitted, the vector is created with coordinates (0, 0, 1). If z is omitted while x or y is provided, z defaults to 0.

Parameters

x?

number

X coordinate of 3D vector.

y?

number

Y coordinate of 3D vector.

z?

number

Z coordinate of 3D vector.

Returns

Vector

Constructor

‍new Vector(start, end): Vector

Creates a vector from start to end.

Parameters

start

Point

Start point.

end

Point

End point.

Returns

Vector

Accessors

x

Get Signature

‍get x(): number

X coordinate of the vector.

Returns

number

Set Signature

‍set x(x): void

Sets the X coordinate of the vector.

Parameters

x

number

X coordinate.

Returns

void


y

Get Signature

‍get y(): number

Y coordinate of the vector.

Returns

number

Set Signature

‍set y(y): void

Sets the Y coordinate of the vector.

Parameters

y

number

Y coordinate.

Returns

void


z

Get Signature

‍get z(): number

Z coordinate of the vector.

Returns

number

Set Signature

‍set z(z): void

Sets the Z coordinate of the vector.

Parameters

z

number

Z coordinate.

Returns

void

Methods

add()

‍add(other): this

Adds the coordinates of other to this vector.

Parameters

other

Vector

Other vector.

Returns

this


added()

‍added(other): Vector

Returns a new vector resulting from adding other to this vector.

Parameters

other

Vector

Other vector.

Returns

Vector


angle()

‍angle(other): number

Returns the angle between this vector and other.

Parameters

other

Vector

Other vector.

Returns

number


clone()

‍clone(): Vector

Creates a new vector with the same coordinates as this vector.

Returns

Vector


copy()

‍copy(other): this

Copies the coordinates of other to this vector.

Parameters

other

Vector

Vector to copy.

Returns

this


cross()

‍cross(other): this

Sets the coordinates of this vector to the cross product of this vector and other.

Parameters

other

Vector

Other vector.

Returns

this


crossed()

‍crossed(other): Vector

Returns a new vector resulting from taking the cross product of this vector and other.

Parameters

other

Vector

Other vector.

Returns

Vector


divide()

‍divide(scalar): this

Divides the coordinates of this vector by scalar.

Parameters

scalar

number

Scalar value.

Returns

this


divided()

‍divided(scalar): Vector

Returns a new vector resulting from dividing this vector by scalar.

Parameters

scalar

number

Scalar value.

Returns

Vector


dot()

‍dot(other): number

Returns the dot product of this vector and other.

Parameters

other

Vector

Other vector.

Returns

number


fromXYZ()

‍static fromXYZ(xyz): Vector

Creates a vector from an object implementing the BaseXYZ interface.

Parameters

xyz

BaseXYZ

Object implementing BaseXYZ interface.

Returns

Vector


get()

‍get(index): number

Returns the coordinate value of the vector:

  • X coordinate if index equals 0
  • Y coordinate if index equals 1
  • Z coordinate if index equals 2

Throws a RangeError if index is not in the range [0, 2].

Parameters

index

number

Coordinate index.

Returns

number


isEqual()

‍isEqual(other, tolerance?): boolean

Returns whether this vector is equal to other within the provided coordinate tolerance.

Parameters

other

Vector

Other 3D vector.

tolerance?

number = Vector.coincidenceTolerance

Coordinate comparison tolerance (1e-7 by default).

Returns

boolean


isNormal()

‍isNormal(other, tolerance?): boolean

Returns whether other is perpendicular to this vector within the provided angular tolerance.

Parameters

other

Vector

Other vector.

tolerance?

number = Vector.angularTolerance

Angular comparison tolerance (1e-5 by default).

Returns

boolean


isOpposite()

‍isOpposite(other, tolerance?): boolean

Returns whether other is opposite to this vector within the provided angular tolerance.

Parameters

other

Vector

Other vector.

tolerance?

number = Vector.angularTolerance

Angular comparison tolerance (1e-5 by default).

Returns

boolean


isParallel()

‍isParallel(other, tolerance?): boolean

Returns whether other is parallel to this vector within the provided angular tolerance.

Parameters

other

Vector

Other vector.

tolerance?

number = Vector.angularTolerance

Angular comparison tolerance (1e-5 by default).

Returns

boolean


length()

‍length(): number

Returns the length of the vector.

Returns

number


mirror()

Call Signature

‍mirror(direction): this

Mirrors this vector about direction.

Parameters

direction

Direction

Direction.

Returns

this

Call Signature

‍mirror(axis): this

Mirrors this vector about axis.

Parameters

axis

Axis1d

Axis.

Returns

this

Call Signature

‍mirror(axis): this

Mirrors this vector about axis.

Parameters

axis

Axis3d

Axis.

Returns

this


mirrored()

Call Signature

‍mirrored(direction): Vector

Returns a new vector resulting from mirroring this vector about direction.

Parameters

direction

Direction

Direction.

Returns

Vector

Call Signature

‍mirrored(axis): Vector

Returns a new vector resulting from mirroring this vector about axis.

Parameters

axis

Axis1d

Axis.

Returns

Vector

Call Signature

‍mirrored(axis): Vector

Returns a new vector resulting from mirroring this vector about axis.

Parameters

axis

Axis3d

Axis.

Returns

Vector


multiplied()

Call Signature

‍multiplied(other): Vector

Returns a new vector resulting from multiplying this vector by other.

Parameters

other

Vector

Other vector.

Returns

Vector

Call Signature

‍multiplied(matrix): Vector

Returns a new vector resulting from multiplying this vector by matrix.

Parameters

matrix

Matrix3d

3D matrix.

Returns

Vector

Call Signature

‍multiplied(scalar): Vector

Returns a new vector resulting from multiplying this vector by scalar.

Parameters

scalar

number

Scalar value.

Returns

Vector


multiply()

Call Signature

‍multiply(other): this

Multiplies the coordinates of this vector by the coordinates of other component-wise.

Parameters

other

Vector

Other vector.

Returns

this

Call Signature

‍multiply(matrix): this

Multiplies the coordinates of this vector by a 3D matrix.

Parameters

matrix

Matrix3d

3D matrix.

Returns

this

Call Signature

‍multiply(scalar): this

Multiplies the coordinates of this vector by a scalar value.

Parameters

scalar

number

Scalar value.

Returns

this


normalize()

‍normalize(): this

Normalizes the coordinates of this vector.

Returns

this


normalized()

‍normalized(): Vector

Returns a new vector with normalized coordinates of this vector.

Returns

Vector


reverse()

‍reverse(): this

Reverses the coordinates of this vector.

Returns

this


reversed()

‍reversed(): Vector

Returns a new vector resulting from reversing this vector.

Returns

Vector


rotate()

‍rotate(axis, angle): this

Rotates this vector around axis by angle.

Parameters

axis

Axis1d

Rotation axis.

angle

number

Rotation angle.

Returns

this


rotated()

‍rotated(axis, angle): Vector

Returns a new vector resulting from rotating this vector.

Parameters

axis

Axis1d

Rotation axis.

angle

number

Rotation angle.

Returns

Vector


set()

‍set(index, value): this

Sets the coordinate value of the vector:

  • X coordinate if index equals 0
  • Y coordinate if index equals 1
  • Z coordinate if index equals 2

Throws a RangeError if index is not in the range [0, 2].

Parameters

index

number

Coordinate index.

value

number

Coordinate value.

Returns

this


setCoord()

‍setCoord(x?, y?, z?): this

Sets the specified coordinates of the vector.

Parameters

x?

number

X coordinate of 3D vector.

y?

number

Y coordinate of 3D vector.

z?

number

Z coordinate of 3D vector.

Returns

this


setX()

‍setX(x): this

Sets the X coordinate of the vector.

Parameters

x

number

X coordinate.

Returns

this


setY()

‍setY(y): this

Sets the Y coordinate of the vector.

Parameters

y

number

Y coordinate.

Returns

this


setZ()

‍setZ(z): this

Sets the Z coordinate of the vector.

Parameters

z

number

Z coordinate.

Returns

this


squareLength()

‍squareLength(): number

Returns the squared length of the vector.

Returns

number


subtract()

‍subtract(other): this

Subtracts the coordinates of other from this vector.

Parameters

other

Vector

Other vector.

Returns

this


subtracted()

‍subtracted(other): Vector

Returns a new vector resulting from subtracting other from this vector.

Parameters

other

Vector

Other vector.

Returns

Vector


toJSON()

‍toJSON(): BaseXYZ

Returns the JSON representation of this vector.

Returns

BaseXYZ


toString()

‍toString(precision?): string

Returns a string representation of this vector with the provided precision.

Parameters

precision?

number = 3

Precision.

Returns

string


transform()

‍transform(transformation): this

Transforms this vector according to transformation.

Only the rotation and scale factor of the transformation affect the vector. Translation is ignored.

Parameters

transformation

Transformation

Transformation.

Returns

this


transformed()

‍transformed(transformation): Vector

Returns a new vector resulting from transforming this vector.

Parameters

transformation

Transformation

Transformation.

Returns

Vector