Hide menu
Loading...
Searching...
No Matches
Direction2d

Defines a 2D direction.

Direction coordinates are always normalized.

Constructors

Constructor

new Direction2d(x?, y?): Direction2d

Creates a direction from coordinates.

If no arguments are provided, the direction is created with coordinates (0, 1). If y is omitted while x is provided, y 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 2D direction.

y?

number

Y coordinate of 2D direction.

Returns

Direction2d

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

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

Direction2d

Other direction.

Returns

this


added()

added(other): Direction2d

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

Parameters

other

Direction2d

Other direction.

Returns

Direction2d


angle()

angle(other): number

Returns the angle between this and other direction.

Parameters

other

Direction2d

Other direction.

Returns

number


clone()

clone(): Direction2d

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

Returns

Direction2d


copy()

copy(other): this

Copies the coordinates of other to this direction.

Parameters

other

Direction2d

Direction to copy.

Returns

this


crossed()

crossed(other): number

Returns the cross product of this and other directions.

Parameters

other

Direction2d

Other direction.

Returns

number


dot()

dot(other): number

Returns the dot product of this and other directions.

Parameters

other

Direction2d

Other direction.

Returns

number


fromXY()

static fromXY(xy): Direction2d

Creates a direction from an object implementing BaseXY.

Parameters

xy

BaseXY

Object implementing BaseXY interface.

Returns

Direction2d


get()

get(index): number

Returns the coordinate value of the direction:

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

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

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

Direction2d

Other 2D direction.

tolerance?

number = Direction2d.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

Direction2d

Other direction.

tolerance?

number = Direction2d.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

Direction2d

Other direction.

tolerance?

number = Direction2d.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

Direction2d

Other direction.

tolerance?

number = Direction2d.angularTolerance

Angular comparison tolerance. Defaults to 1e-5.

Returns

boolean


multiplied()

Call Signature

multiplied(other): Direction2d

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

Parameters

other

Direction2d

Other direction.

Returns

Direction2d

Call Signature

multiplied(matrix): Direction2d

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

Parameters

matrix

Matrix2d

2D matrix.

Returns

Direction2d


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

Direction2d

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

Matrix2d

2D matrix.

Returns

this


reverse()

reverse(): this

Reverses this direction.

Returns

this


reversed()

reversed(): Direction2d

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

Returns

Direction2d


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

Throws a RangeError if index is not in the range [0, 1] 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?): 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 2D direction.

y?

number

Y coordinate of 2D 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


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

Direction2d

Other direction.

Returns

this


subtracted()

subtracted(other): Direction2d

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

Parameters

other

Direction2d

Other direction.

Returns

Direction2d


toJSON()

toJSON(): BaseXY

Returns a JSON-serializable representation of this direction.

Returns

BaseXY


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

Transformation2d

Transformation.

Returns

this


transformed()

transformed(transformation): Direction2d

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

Parameters

transformation

Transformation2d

Transformation.

Returns

Direction2d


xDir()

static xDir(): Direction2d

Returns a direction with coordinates (1, 0).

Returns

Direction2d


yDir()

static yDir(): Direction2d

Returns a direction with coordinates (0, 1).

Returns

Direction2d