Hide menu
Loading...
Searching...
No Matches
Vector2d

Defines a 2D vector.

Constructors

Constructor

new Vector2d(x?, y?): Vector2d

Creates a vector from coordinates.

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

Parameters

x?

number

X coordinate of 2D vector.

y?

number

Y coordinate of 2D vector.

Returns

Vector2d

Constructor

new Vector2d(start, end): Vector2d

Creates a vector from start to end.

Parameters

start

Point2d

Start point.

end

Point2d

End point.

Returns

Vector2d

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

Methods

add()

add(other): this

Adds the coordinates of other to this vector.

Parameters

other

Vector2d

Other vector.

Returns

this


added()

added(other): Vector2d

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

Parameters

other

Vector2d

Other vector.

Returns

Vector2d


angle()

angle(other): number

Returns the angle between this vector and other.

Parameters

other

Vector2d

Other vector.

Returns

number


clone()

clone(): Vector2d

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

Returns

Vector2d


copy()

copy(other): this

Copies the coordinates of other to this vector.

Parameters

other

Vector2d

Vector to copy.

Returns

this


crossed()

crossed(other): number

Returns the scalar cross product of this vector and other.

Parameters

other

Vector2d

Other vector.

Returns

number


divide()

divide(scalar): this

Divides the coordinates of this vector by scalar.

Parameters

scalar

number

Scalar value.

Returns

this


divided()

divided(scalar): Vector2d

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

Parameters

scalar

number

Scalar value.

Returns

Vector2d


dot()

dot(other): number

Returns the dot product of this vector and other.

Parameters

other

Vector2d

Other vector.

Returns

number


fromXY()

static fromXY(xy): Vector2d

Creates a vector from an object implementing the BaseXY interface.

Parameters

xy

BaseXY

Object implementing BaseXY interface.

Returns

Vector2d


get()

get(index): number

Returns the coordinate value of the vector:

  • 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 whether this vector is equal to other within the provided coordinate tolerance.

Parameters

other

Vector2d

Other 2D vector.

tolerance?

number = Vector2d.coincidenceTolerance

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

Returns

boolean


isNormal()

isNormal(other, tolerance?): boolean

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

Parameters

other

Vector2d

Other vector.

tolerance?

number = Vector2d.angularTolerance

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

Returns

boolean


isOpposite()

isOpposite(other, tolerance?): boolean

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

Parameters

other

Vector2d

Other vector.

tolerance?

number = Vector2d.angularTolerance

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

Returns

boolean


isParallel()

isParallel(other, tolerance?): boolean

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

Parameters

other

Vector2d

Other vector.

tolerance?

number = Vector2d.angularTolerance

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

Returns

boolean


length()

length(): number

Returns the length of the vector.

Returns

number


multiplied()

Call Signature

multiplied(other): Vector2d

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

Parameters

other

Vector2d

Other vector.

Returns

Vector2d

Call Signature

multiplied(matrix): Vector2d

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

Parameters

matrix

Matrix2d

2D matrix.

Returns

Vector2d

Call Signature

multiplied(scalar): Vector2d

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

Parameters

scalar

number

Scalar value.

Returns

Vector2d


multiply()

Call Signature

multiply(other): this

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

Parameters

other

Vector2d

Other vector.

Returns

this

Call Signature

multiply(matrix): this

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

Parameters

matrix

Matrix2d

2D 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(): Vector2d

Returns a new vector with normalized coordinates of this vector.

Returns

Vector2d


reverse()

reverse(): this

Reverses the coordinates of this vector.

Returns

this


reversed()

reversed(): Vector2d

Returns a new vector resulting from reversing this vector.

Returns

Vector2d


set()

set(index, value): this

Sets the coordinate value of the vector:

  • 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.

value

number

Coordinate value.

Returns

this


setCoord()

setCoord(x?, y?): this

Sets the specified coordinates of the vector.

Parameters

x?

number

X coordinate of 2D vector.

y?

number

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


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

Vector2d

Other vector.

Returns

this


subtracted()

subtracted(other): Vector2d

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

Parameters

other

Vector2d

Other vector.

Returns

Vector2d


toJSON()

toJSON(): BaseXY

Returns the JSON representation of this vector.

Returns

BaseXY


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

Transformation2d

Transformation.

Returns

this


transformed()

transformed(transformation): Vector2d

Returns a new vector resulting from transforming this vector.

Parameters

transformation

Transformation2d

Transformation.

Returns

Vector2d