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