Hide menu
Loading...
Searching...
No Matches
Direction

Defines a 3D direction.

Direction coordinates are always normalized.

Constructors

Constructor

new Direction(x?, y?, z?): Direction

Creates a direction from coordinates.

If no arguments are provided, the direction is created with coordinates (0, 0, 1). If z is omitted while x or y is provided, z defaults to 0. The resulting coordinates are normalized.

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

x?

number

X coordinate of 3D direction.

y?

number

Y coordinate of 3D direction.

z?

number

Z coordinate of 3D direction.

Returns

Direction

Accessors

x

Get Signature

get x(): number

Returns the X coordinate of the direction.

Returns

number

Set Signature

set x(x): void

Sets the X coordinate of the direction and normalizes the result.

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

x

number

X coordinate.

Returns

void


y

Get Signature

get y(): number

Returns the Y coordinate of the direction.

Returns

number

Set Signature

set y(y): void

Sets the Y coordinate of the direction and normalizes the result.

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

y

number

Y coordinate.

Returns

void


z

Get Signature

get z(): number

Returns the Z coordinate of the direction.

Returns

number

Set Signature

set z(z): void

Sets the Z coordinate of the direction and normalizes the result.

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

z

number

Z coordinate.

Returns

void

Methods

add()

add(other): this

Adds other direction to this direction and normalizes the result.

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

other

Direction

Other direction.

Returns

this


added()

added(other): Direction

Returns a new direction obtained by applying add() to this direction.

Parameters

other

Direction

Other direction.

Returns

Direction


angle()

angle(other): number

Returns the angle between this and other direction.

Parameters

other

Direction

Other direction.

Returns

number


clone()

clone(): Direction

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

Returns

Direction


copy()

copy(other): this

Copies the coordinates of other to this direction.

Parameters

other

Direction

Direction to copy.

Returns

this


cross()

cross(other): this

Sets this direction to the cross product of this and other directions and normalizes the result.

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

other

Direction

Other direction.

Returns

this


crossCross()

crossCross(coord1, coord2): this

Computes this = this.cross(coord1.cross(coord2)).

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

coord1

Direction

First direction.

coord2

Direction

Second direction.

Returns

this


crossCrossed()

crossCrossed(coord1, coord2): Direction

Returns a new direction obtained by applying crossCross() to this direction.

Parameters

coord1

Direction

First direction.

coord2

Direction

Second direction.

Returns

Direction


crossed()

crossed(other): Direction

Returns a new direction obtained by applying cross() to this direction.

Parameters

other

Direction

Other direction.

Returns

Direction


dot()

dot(other): number

Returns the dot product of this and other directions.

Parameters

other

Direction

Other direction.

Returns

number


fromXYZ()

static fromXYZ(xyz): Direction

Creates a direction from an object implementing BaseXYZ.

Parameters

xyz

BaseXYZ

Object implementing BaseXYZ interface.

Returns

Direction


get()

get(index): number

Returns the coordinate value of the direction:

  • 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 true if other direction is equal to this direction within the specified tolerance.

Parameters

other

Direction

Other 3D direction.

tolerance?

number = Direction.angularTolerance

Angular comparison tolerance. Defaults to 1e-5.

Returns

boolean


isNormal()

isNormal(other, tolerance?): boolean

Returns true if other direction is perpendicular to this direction within the specified tolerance.

Parameters

other

Direction

Other direction.

tolerance?

number = Direction.angularTolerance

Angular comparison tolerance. Defaults to 1e-5.

Returns

boolean


isOpposite()

isOpposite(other, tolerance?): boolean

Returns true if other direction is opposite to this direction within the specified tolerance.

Parameters

other

Direction

Other direction.

tolerance?

number = Direction.angularTolerance

Angular comparison tolerance. Defaults to 1e-5.

Returns

boolean


isParallel()

isParallel(other, tolerance?): boolean

Returns true if other direction is parallel to this direction within the specified tolerance.

Parameters

other

Direction

Other direction.

tolerance?

number = Direction.angularTolerance

Angular comparison tolerance. Defaults to 1e-5.

Returns

boolean


mirror()

Call Signature

mirror(direction): this

Mirrors this direction relative to direction.

Parameters

direction

Direction

Other direction.

Returns

this

Call Signature

mirror(axis): this

Mirrors this direction relative to axis.

Parameters

axis

Axis1d

Axis.

Returns

this

Call Signature

mirror(axis): this

Mirrors this direction relative to axis.

Parameters

axis

Axis3d

Axis.

Returns

this


mirrored()

Call Signature

mirrored(direction): Direction

Returns a new direction obtained by applying mirror() to this direction.

Parameters

direction

Direction

Direction.

Returns

Direction

Call Signature

mirrored(axis): Direction

Returns a new direction obtained by applying mirror() to this direction.

Parameters

axis

Axis1d

Axis.

Returns

Direction

Call Signature

mirrored(axis): Direction

Returns a new direction obtained by applying mirror() to this direction.

Parameters

axis

Axis3d

Axis.

Returns

Direction


multiplied()

Call Signature

multiplied(other): Direction

Returns a new direction obtained by applying multiply() to this direction.

Parameters

other

Direction

Other direction.

Returns

Direction

Call Signature

multiplied(matrix): Direction

Returns a new direction obtained by applying multiply() to this direction.

Parameters

matrix

Matrix3d

3D matrix.

Returns

Direction


multiply()

Call Signature

multiply(other): this

Multiplies the coordinates of this direction by the coordinates of other and normalizes the result.

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

other

Direction

Other direction.

Returns

this

Call Signature

multiply(matrix): this

Multiplies the coordinates of this direction by matrix and normalizes the result.

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

matrix

Matrix3d

3D matrix.

Returns

this


reverse()

reverse(): this

Reverses this direction.

Returns

this


reversed()

reversed(): Direction

Returns a new direction obtained by applying reverse() to this direction.

Returns

Direction


rotate()

rotate(axis, angle): this

Rotates this direction around axis by angle.

Parameters

axis

Axis1d

Rotation axis.

angle

number

Rotation angle.

Returns

this


rotated()

rotated(axis, angle): Direction

Returns a new direction obtained by applying rotate() to this direction.

Parameters

axis

Axis1d

Rotation axis.

angle

number

Rotation angle.

Returns

Direction


set()

set(index, value): this

Sets the coordinate value of the direction and normalizes the result:

  • 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] or if the resulting direction magnitude is not greater than 1e-7.

Parameters

index

number

Coordinate index.

value

number

Coordinate value.

Returns

this


setCoord()

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

Sets the specified coordinates of the direction and normalizes the result.

Coordinates passed as undefined are left unchanged. Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

x?

number

X coordinate of 3D direction.

y?

number

Y coordinate of 3D direction.

z?

number

Z coordinate of 3D direction.

Returns

this


setX()

setX(x): this

Sets the X coordinate of the direction and normalizes the result.

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

x

number

X coordinate.

Returns

this


setY()

setY(y): this

Sets the Y coordinate of the direction and normalizes the result.

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

y

number

Y coordinate.

Returns

this


setZ()

setZ(z): this

Sets the Z coordinate of the direction and normalizes the result.

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

z

number

Z coordinate.

Returns

this


subtract()

subtract(other): this

Subtracts other direction from this direction and normalizes the result.

Throws a RangeError if the resulting direction magnitude is not greater than 1e-5.

Parameters

other

Direction

Other direction.

Returns

this


subtracted()

subtracted(other): Direction

Returns a new direction obtained by applying subtract() to this direction.

Parameters

other

Direction

Other direction.

Returns

Direction


toJSON()

toJSON(): BaseXYZ

Returns a JSON-serializable representation of this direction.

Returns

BaseXYZ


toString()

toString(precision?): string

Returns a string representation of this direction with the specified precision.

Parameters

precision?

number = 3

Precision.

Returns

string


transform()

transform(transformation): this

Transforms this direction according to transformation.

Only the rotational part of the transformation affects the direction. If the transformation scale factor is negative, the direction is reversed.

Parameters

transformation

Transformation

Transformation.

Returns

this


transformed()

transformed(transformation): Direction

Returns a new direction obtained by applying transform() to this direction.

Parameters

transformation

Transformation

Transformation.

Returns

Direction


xDir()

static xDir(): Direction

Returns a direction with coordinates (1, 0, 0).

Returns

Direction


yDir()

static yDir(): Direction

Returns a direction with coordinates (0, 1, 0).

Returns

Direction


zDir()

static zDir(): Direction

Returns a direction with coordinates (0, 0, 1).

Returns

Direction