Constructor
new Vector(x, y)
Create a vector from coordinates.
Name | Type | Default | Description |
---|---|---|---|
x | number | 0 | x |
y | number | 0 | y |
Classes
Methods
add(vector) → {Vector}
Add a vector to this vector.
Name | Type | Description |
---|---|---|
vector | Vector |
- Type:
- Vector
clone() → {Vector}
Make a copy of this vector.
- Type:
- Vector
distance(vector) → {number}
Calculate the distance to another vector from this vector
Name | Type | Description |
---|---|---|
vector | Vector |
- Type:
- number
equals(vector) → {boolean}
Check if this vector is equivalent to another vector.
Name | Type | Description |
---|---|---|
vector | Vector |
- Type:
- boolean
getAngle() → {number}
Get the angle of this vector with respect to the positive x-axis.
Angle in radians
- Type:
- number
getMagnitude() → {number}
Get the magnitude of this vector.
The magnitude (Euclidean distance) of this vector
- Type:
- number
isOnLine(line) → {boolean}
Check if this vector is a point on a Line.
Name | Type | Description |
---|---|---|
line | Line |
- Type:
- boolean
multiply(scalar) → {Vector}
Multiply this vector by a scalar.
Name | Type | Description |
---|---|---|
scalar | number |
- Type:
- Vector
nearestNeighbour(array, n) → {Array.<Vector>}
Finds the n
nearest neighbours to this vector, from a given array of vectors.
Name | Type | Description |
---|---|---|
array | Array.<Vector> | An array of Vectors to check. |
n | number | The number of neighbours to find (must be > 0). |
- Type:
- Array.<Vector>
normalize() → {Vector}
Normalize this vector by setting it's magnitude to 1.
- Type:
- Vector
rotate(angle) → {Vector}
Rotate this vector by a specified angle.
Name | Type | Description |
---|---|---|
angle | number | The angle to rotate the vector by, in radians. |
This vector after rotation.
- Type:
- Vector
setMagnitude(magnitude) → {Vector}
Set the magnitude of this vector.
Name | Type | Description |
---|---|---|
magnitude | number |
- Type:
- Vector
subtract(vector) → {Vector}
Subtract a vector from this vector.
Name | Type | Description |
---|---|---|
vector | Vector |
- Type:
- Vector
(static) add(v1, v2) → {Vector}
Add two vectors.
- Type:
- Vector
(static) cross(v1, v2) → {number}
Calculate the cross product of two 2D vectors.
- Type:
- number
(static) distance(v1, v2) → {number}
Calculate the distance between two vectors.
- Type:
- number
(static) dot(v1, v2) → {number}
Calculate the dot product of two vectors.
- Type:
- number
(static) fromAngle(angle, magnitudeopt) → {Vector}
Create a vector from an angle.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
angle | number | The angle of the vector in radians. | ||
magnitude | number | <optional> | 1 | The magnitude of the vector. |
- Type:
- Vector
(static) fromArray(array) → {Vector}
Create a vector from an array.
Name | Type | Description |
---|---|---|
array | array | [x, y] |
- Type:
- Vector
(static) lerp(v1, v2, amount) → {Vector}
Linear interpolation between vectors.
- Type:
- Vector
(static) subtract(v1, v2) → {Vector}
Subtract two vectors.
- Type:
- Vector