Class: Vector2D

vector.Vector2D(xopt, yopt)

A vector in a two-dimensional space.

Constructor

new Vector2D(xopt, yopt)

Creates a new vector.

Examples:

new Vector2D(1, 2)
new Vector2D([1, 2])
new Vector2D({ x: 1, y: 2 })
Parameters:
Name Type Attributes Default Description
x number | Array.<number> | Vector2D <optional>
0

The x coordinate or an array or another vector.

y number <optional>
0

The y coordinate or undefined if x is an array or another vector.

Methods

add(v) → {Vector2D}

Adds another vector to this vector and return the new vector.

Parameters:
Name Type Description
v Vector2D

The vector to add.

Returns:

The new vector.

Type
Vector2D

distance(v) → {number}

Returns the distance between this vector and another vector.

Parameters:
Name Type Description
v Vector2D

The other vector.

Returns:

The distance.

Type
number

dot(v) → {number}

Multiplies the vector with another vector and returns the scalar.

Parameters:
Name Type Description
v Vector2D

The vector to multiply with.

Returns:

The scalar.

Type
number

length() → {number}

Returns the length of the vector.

Returns:

The length of the vector.

Type
number

normalize() → {Vector2D}

Returns the unit vector of this vector.

Returns:

The unit vector.

Type
Vector2D

rotate(angle) → {Vector2D}

Returns the rotated vector by the given angle in radians.

Parameters:
Name Type Description
angle number

The angle in radians.

Returns:

The rotated vector.

Type
Vector2D

scale(scalar) → {Vector2D}

Multiplies the vector with a scalar and returns the new vector.

Parameters:
Name Type Description
scalar number

The scalar to multiply with.

Returns:

The new vector.

Type
Vector2D

subtract(v) → {Vector2D}

Subtracts another vector from this vector and return the new vector.

Parameters:
Name Type Description
v Vector2D

The vector to subtract.

Returns:

The new vector.

Type
Vector2D

(static) fromPoints(a, b) → {Vector2D}

Creates a vector from 2 points.

Parameters:
Name Type Description
a Vector2D

The first point.

b Vector2D

The second point.

Returns:

The vector from a to b.

Type
Vector2D