Direction coordinates are always normalized.
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.
number
X coordinate of 2D direction.
number
Y coordinate of 2D direction.
Direction2d
get x():
number
Returns the X coordinate of the direction.
number
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.
number
X coordinate.
void
get y():
number
Returns the Y coordinate of the direction.
number
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.
number
Y coordinate.
void
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.
Direction2d
Other direction.
this
added(
other):Direction2d
Returns a new direction obtained by applying add() to this direction.
Direction2d
Other direction.
Direction2d
angle(
other):number
Returns the angle between this and other direction.
Direction2d
Other direction.
number
clone():
Direction2d
Creates a new direction with the same coordinates as this direction.
Direction2d
copy(
other):this
Copies the coordinates of other to this direction.
Direction2d
Direction to copy.
this
crossed(
other):number
Returns the cross product of this and other directions.
Direction2d
Other direction.
number
dot(
other):number
Returns the dot product of this and other directions.
Direction2d
Other direction.
number
staticfromXY(xy):Direction2d
Creates a direction from an object implementing BaseXY.
Object implementing BaseXY interface.
Direction2d
get(
index):number
Returns the coordinate value of the direction:
index equals 0index equals 1Throws a RangeError if index is not in the range [0, 1].
number
Coordinate index.
number
isEqual(
other,tolerance?):boolean
Returns true if other direction is equal to this direction within the specified tolerance.
Direction2d
Other 2D direction.
number = Direction2d.angularTolerance
Angular comparison tolerance. Defaults to 1e-5.
boolean
isNormal(
other,tolerance?):boolean
Returns true if other direction is perpendicular to this direction within the specified tolerance.
Direction2d
Other direction.
number = Direction2d.angularTolerance
Angular comparison tolerance. Defaults to 1e-5.
boolean
isOpposite(
other,tolerance?):boolean
Returns true if other direction is opposite to this direction within the specified tolerance.
Direction2d
Other direction.
number = Direction2d.angularTolerance
Angular comparison tolerance. Defaults to 1e-5.
boolean
isParallel(
other,tolerance?):boolean
Returns true if other direction is parallel to this direction within the specified tolerance.
Direction2d
Other direction.
number = Direction2d.angularTolerance
Angular comparison tolerance. Defaults to 1e-5.
boolean
multiplied(
other):Direction2d
Returns a new direction obtained by applying multiply() to this direction.
Direction2d
Other direction.
Direction2d
multiplied(
matrix):Direction2d
Returns a new direction obtained by applying multiply() to this direction.
2D matrix.
Direction2d
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.
Direction2d
Other direction.
this
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.
2D matrix.
this
reverse():
this
Reverses this direction.
this
reversed():
Direction2d
Returns a new direction obtained by applying reverse() to this direction.
Direction2d
set(
index,value):this
Sets the coordinate value of the direction and normalizes the result:
index equals 0index equals 1Throws a RangeError if index is not in the range [0, 1] or if the resulting direction magnitude is not greater than 1e-7.
number
Coordinate index.
number
Coordinate value.
this
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.
number
X coordinate of 2D direction.
number
Y coordinate of 2D direction.
this
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.
number
X coordinate.
this
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.
number
Y coordinate.
this
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.
Direction2d
Other direction.
this
subtracted(
other):Direction2d
Returns a new direction obtained by applying subtract() to this direction.
Direction2d
Other direction.
Direction2d
toJSON():
BaseXY
Returns a JSON-serializable representation of this direction.
toString(
precision?):string
Returns a string representation of this direction with the specified precision.
number = 3
Precision.
string
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.
Transformation.
this
transformed(
transformation):Direction2d
Returns a new direction obtained by applying transform() to this direction.
Transformation.
Direction2d
staticxDir():Direction2d
Returns a direction with coordinates (1, 0).
Direction2d
staticyDir():Direction2d
Returns a direction with coordinates (0, 1).
Direction2d