Constructor
new Vector(0opt, 0opt, 0opt) → {Vector}
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
0 |
number |
<optional> |
x - The x value |
0 |
number |
<optional> |
y - The y value |
0 |
number |
<optional> |
z - The z value |
- Source:
Returns:
- The new vector
- Type
- Vector
Examples
v1 = new Vector(1, 4, -3);
v2 = new Vector(3, -5);
Members
x
Get the x component of the vector
- Source:
x
Set the x component of the vector
- Source:
y
Get the y component of the vector
- Source:
y
Set the y component of the vector
- Source:
z
Get the z component of the vector
- Source:
z
Set the z component of the vector
- Source:
Methods
(static) fromAngle2D(0opt) → {Vector}
Create a 2D vector from its angle
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
0 |
number |
<optional> |
theta - Theta angle (radians) |
- Source:
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector.fromAngle2D(2.42);
// v1 = Vector(-0.7507546047254909,0.6605812012792007, 0);
(static) fromAngle3D(0opt, 0opt) → {Vector}
Create a 3D vector from its angles
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
0 |
number |
<optional> |
theta - Theta angle (radians) |
0 |
number |
<optional> |
phi - Phi angle (radians) |
- Source:
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector.fromAngle2D(1.33, -2.44);
// v1 = Vector(-0.1821516349441893, -0.6454349983343708, -0.7417778945292652);
(static) fromArray(a) → {Vector}
Create a vector from an Array
Parameters:
| Name | Type | Description |
|---|---|---|
a |
Array | The array |
- Source:
Returns:
- The new vector
- Type
- Vector
Examples
// return Vector(4, 5, 6)
v = new Vector.fromArray([4, 5, 6])
// return Vector(1, 7, 0)
v = new Vector.fromArray([1, 7])
(static) fromObject() → {Vector}
Create a vector from an object
- Source:
Returns:
- The new vector
- Type
- Vector
Examples
// return Vector(1, 5, 9)
v = new Vector.fromArray([1, 5, 9]])
// return Vector(3, 0, 4)
v = new Vector.fromArray([3, 0, 4]])
(static) fromPolar(r, theta, 0opt) → {Vector}
Create a vector from its polar coordinates
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
r |
number | The radius | |
theta |
number | The theta angle (radians) | |
0 |
number |
<optional> |
phi - The phi angle (radians) |
- Source:
Throws:
-
If the arguments are not numbers
- Type
- Error
Returns:
- The new vector
- Type
- Vector
(static) fromString(s) → {Vector}
Create a vector from a string
Parameters:
| Name | Type | Description |
|---|---|---|
s |
string | The string |
- Source:
Throws:
-
If the string is not in the correct format
- Type
- Error
Returns:
- The new vector
- Type
- Vector
(static) random2D() → {Vector}
Create a random 2D vector
- Source:
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector.random2D();
// v1 = Vector(0.2090564102081952, -0.977903582849998, 0);
(static) random3D() → {Vector}
Create a random 3D vector
- Source:
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector.random3D();
// v1 = Vector(-0.7651693875628326, -0.43066633476756877, 0.47858365667309205);
add(v) → {Vector}
Add a vector
Parameters:
| Name | Type | Description |
|---|---|---|
v |
Vector | The vector to be added |
- Source:
Throws:
-
- If the argument is not a vector
- Type
- TypeError
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector(1, -4, 12);
v2 = new Vector(2, 9, -3);
v1.add(v2);
// v1 = Vector(3, 5, 9);
angleBetween(v) → {number}
Angle between vectors
Parameters:
| Name | Type | Description |
|---|---|---|
v |
Vector | The vector whose contained angle will be calculated |
- Source:
Throws:
-
- If the argument is not a vector
- Type
- TypeError
Returns:
Return a vector containing the angle in radians
- Type
- number
Example
v1 = new Vector(1, 4, -3);
v2 = new Vector(6, -6, 7);
v1.angleBetween(v2);
// return 1.0888019833827516
copy() → {Vector}
Copy the vector into a new object
- Source:
Returns:
The new copied vector
- Type
- Vector
Example
v1 = new Vector(8, 144, -32);
v2 = v1.copy();
// v2 = Vector(8, 144, -32);
cross(v) → {Vector}
Cross function
Parameters:
| Name | Type | Description |
|---|---|---|
v |
Vector | The vector to perform cross operation with |
- Source:
Throws:
-
- If the argument is not a vector
- Type
- TypeError
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector(1, 4, 3);
v2 = new Vector(2, -6, 9);
v1.cross(v2);
// v1 = Vector(54, -3, -14);
dist(v) → {number}
Distance between vectors
Parameters:
| Name | Type | Description |
|---|---|---|
v |
Vector | The vector whose distance will be calculated |
- Source:
Throws:
-
- If the argument is not a vector
- Type
- TypeError
Returns:
Return a number containing the distance
- Type
- number
Example
v1 = new Vector(1, 4, -3);
v2 = new Vector(6, -6, 7);
v1.dist(v2);
// return 15
distSq(v) → {number}
Square distance between vectors
Parameters:
| Name | Type | Description |
|---|---|---|
v |
Vector | The vector whose distance will be calculated |
- Source:
Throws:
-
- If the argument is not a vector
- Type
- TypeError
Returns:
Return a number containing the square distance
- Type
- number
Example
v1 = new Vector(1, 4, -3);
v2 = new Vector(6, -6, 7);
v1.distSq(v2);
// return 225
div()
Alias for divide
- Source:
divide(v) → {Vector}
Divide by a vector or a scalar
Parameters:
| Name | Type | Description |
|---|---|---|
v |
Vector | number | The vector or scalar to be divided by |
- Source:
Throws:
-
- If the argument is not a vector or a number v1 = new Vector(4, 12, 9); v2 = new Vector(4, 6, 3); v1.divide(v2); // v1 = Vector(1, 2, 3);
- Type
- TypeError
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector(9, 3, 6);
v1.divide(3);
// v1 = Vector(3, 1, 2);
dot(v) → {Vector}
Dot function
Parameters:
| Name | Type | Description |
|---|---|---|
v |
Vector | The vector to perform dot operation with |
- Source:
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector(1, 4, 3);
v2 = new Vector(2, -6, 9);
v1.dot(v2);
// return 5;
equals(v) → {boolean}
Check if two vectors are equals
Parameters:
| Name | Type | Description |
|---|---|---|
v |
Vector | The vector that will be compared |
- Source:
Throws:
-
- If the argument is not a vector
- Type
- TypeError
Returns:
Return true or false
- Type
- boolean
Example
v1 = new Vector(1, 4, -3);
v2 = new Vector(6, -6, 7);
v1.equals(v2);
// return false;
heading2D() → {number}
Calculate the vector heading (radians) - only for 2D vectors
- Source:
Returns:
The vector heading (radians)
- Type
- number
Example
v1 = new Vector(3, 3);
v1.heading2D();
// return 0.7853981633974483
invert(x, y, z) → {Vector}
Invert some (or all) components of the vector
Parameters:
| Name | Type | Description |
|---|---|---|
x |
boolean | The x component |
y |
boolean | The y component |
z |
boolean | The z component |
- Source:
Returns:
- The new vector
- Type
- Vector
Examples
v1 = new Vector(4, -5, 7);
v1.invert(true, true, true);
// v1 = Vector(-4, 5, -7);
v2 = new Vector(4, -1, -3);
v2.invert(true, false);
// v2 = Vector(-4, -1, -3);
v3 = new Vector(4, -1, -3);
v3.invert();
// v3 = Vector(-4, 1, 3);
invertX() → {Vector}
Invert the x component of the vector
- Source:
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector(4, -5, 7);
v1.invertX();
// v1 = Vector(-4, -5, 7);
invertY() → {Vector}
Invert the y component of the vector
- Source:
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector(4, -5, 7);
v1.invertY();
// v1 = Vector(4, 5, 7);
invertZ() → {Vector}
Invert the z component of the vector
- Source:
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector(4, -5, 7);
v1.invertZ();
// v1 = Vector(4, -5, -7);
limit(s) → {Vector}
Limit the vector magnitude to a set value
Parameters:
| Name | Type | Description |
|---|---|---|
s |
number | The maximum magnitude |
- Source:
Throws:
-
- If the argument is not a number
- Type
- TypeError
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector(2, 0, 2);
v1.limit(2);
// v1 = Vector(1.414213562373095, 0, 1.414213562373095);
mag() → {number}
Calculate the vector magnitude
- Source:
Returns:
- The vector magnitude
- Type
- number
Example
v1 = new Vector(6, -2, -1);
v1.mag();
// return 6.4031242374328485;
magnitude()
Alias for mag()
- Source:
magnitudeSq()
Alias for magSq()
- Source:
magSq() → {number}
Calculate the vector square magnitude
- Source:
Returns:
The vector square magnitude
- Type
- number
Example
v1 = new Vector(6, -2, -1);
v1.magSq();
// return 41;
max() → {number}
Return maximum component of a vector
- Source:
Returns:
The biggest component
- Type
- number
Example
v1 = new Vector(3, -8, 12);
v1.max();
// -12
min() → {number}
Return minimum component of a vector
- Source:
Returns:
The smallest component
- Type
- number
Example
v1 = new Vector(3, -8, 12);
v1.min();
// -8
mult(v) → {Vector}
Multiply by a vector or a scalar
Parameters:
| Name | Type | Description |
|---|---|---|
v |
Vector | number | The vector or scalar to be multiplied by |
- Source:
Throws:
-
- If the argument is not a vector or a number
- Type
- TypeError
Returns:
- The new vector
- Type
- Vector
Examples
v1 = new Vector(1, 2, 3);
v2 = new Vector(2, 5, 0);
v1.mult(v2);
// v1 = Vector(2, 10, 0);
v1 = new Vector(7, 4, 2);
v1.mult(3);
// v1 = Vector(21, 12, 6);
multiply()
Alias for mult
- Source:
normalize() → {Vector}
Normalize a vector (its magnitude will be unitary)
- Source:
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector(5, 2, -4);
v1.normalize();
// v1 = Vector(0.7453559924999299, 0.29814239699997197, -0.5962847939999439);
rotate(t) → {Vector}
Rotate a vector by an angle in radians
Parameters:
| Name | Type | Description |
|---|---|---|
t |
number | The rotation angle |
- Source:
Throws:
-
- If the argument is not a number
- Type
- TypeError
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector(2, 1);
v1.rotate(Math.PI);
// v1 = Vector(-2, -1, 0);
setMag(s) → {Vector}
Set the vector magnitude
Parameters:
| Name | Type | Description |
|---|---|---|
s |
number | Magnitude |
- Source:
Throws:
-
- If the argument is not a number
- Type
- TypeError
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector(2, 0, 2);
v1.setMag(4);
// v1 = Vector(2.82842712474619, 0, 2.82842712474619);
sub(v) → {Vector}
Subtract a vector
Parameters:
| Name | Type | Description |
|---|---|---|
v |
Vector | The vector to be subtracted |
- Source:
Throws:
-
- If the argument is not a vector
- Type
- TypeError
Returns:
- The new vector
- Type
- Vector
Example
v1 = new Vector(10, -3, 12);
v2 = new Vector(7, -8, 3);
v1.sub(v2);
// v1 = Vector(3, 5, 9);
subtract()
Alias for sub
- Source:
toArray() → {array}
Return an array with the vector components
- Source:
Returns:
Array with the vector components
- Type
- array
Example
v1 = new Vector(3, 3, -4);
v1.toArray();
// return [3, 3, -4]
toObject() → {object}
Return an object with the vector components
- Source:
Returns:
Object with the vector components
- Type
- object
Example
v1 = new Vector(3, 3, -4);
v1.toObject();
// return { x: 3, y: 3, z: -4 }
toString() → {string}
Return a printable string of the vector
- Source:
Returns:
Printable string
- Type
- string
Example
v1 = new Vector(3, 3, -4);
v1.toString();
// return "x: 3, y: 3, z: -4"