A vector is a point in 2D space that has a x and y position.
property | type | description | default |
---|---|---|---|
x | number | The x position of the vector. | 0 |
y | number | The y position of the vector. | 0 |
example:
const vec1 = new Vector(10, 25);
Returns the x value of the vector.
Returns the y value of the vector.
Copies another vector's values into this vector.
property | type | description | default |
---|---|---|---|
other | Vector | The other vector whose values should be copied into this one. |
example:
const vec1 = new Vector(10, 10);
const vec2 = new Vector(15, 20);
vec1.copy(vec2); // vec1 is now at (10, 10)
Returns a new vector with the same coordinates as this one.
example:
const vec1 = new Vector(10, 10);
const vec2 = vec1.clone(); // vec2 is at (10, 10)
Change this vector to be perpendicular to what is was before. This effectively rotates it 90 degrees in a clockwise direction.
example:
const vec1 = new Vector(5, 10);
vec1.perp(); // vec1 is now (10, -5)
Rotate this vector counter-clockwise by the specified angle (in radians).
property | type | description | default |
---|---|---|---|
angle | number | The angle to rotate rotate by, in radians |
example:
const vec1 = new Vector(5, 10);
vec1.rotate(45);
Reverse the vector.
example:
const vec1 = new Vector(5, 10);
vec1.reverse(); // vec1 is now (-5, -10)
Make this vecotr have a length of 1.
example:
const vec1 = new Vector(5, 10);
vec1.normalize();
Add another vector to this one.
property | type | description | default |
---|---|---|---|
other | Vector | The other vector to add to this one. |
example:
const vec1 = new Vector(5, 10);
const vec2 = new Vector(10, 20);
vec1.add(vec2); // vec1 is now (15, 30)
Subtract another vector from this one.
property | type | description | default |
---|---|---|---|
other | Vector | The other vector to subtract from this one. |
example:
const vec1 = new Vector(15, 35);
const vec2 = new Vector(10, 20);
vec1.sub(vec2); // vec1 is now (5, 15)
Scale this vector.
An independent scaling factor can be provided for each axis, or a single scaling factor will scale both x
and y
.
property | type | description | default |
---|---|---|---|
x | number | The scaling factor in the x direction | |
y | number | The scaling factor in the y direction | x |
example:
const vec1 = new Vector(15, 35);
vec1.scale(2, 5); // vec1 is now (30, 175)
Project this vector onto another vector.
property | type | description | default |
---|---|---|---|
other | Vector | The vector to project onto |
example:
const vec1 = new Vector(5, 5);
const vec2 = new Vector(2, 5);
vec1.project(vec2);
Project this Vector onto a Vector of unit length.
This is slightly more efficient than project
when dealing with unit vectors.
property | type | description | default |
---|---|---|---|
other | Vector | The unit vector to project onto |
example:
const vec1 = new Vector(5, 5);
const vec2 = new Vector(2, 5);
vec1.projectN(vec2);
Reflect this Vector on an arbitrary axis.
property | type | description | default |
---|---|---|---|
axis | Vector | The vector representing the axis to reflect on. |
example:
const vec1 = new Vector(5, 5);
const axis = new Vector(0, 1);
vec1.reflect(axis);
Reflect this Vector on an arbitrary axis.
This is slightly more efficient than reflect
when dealing with an axis that is a unit vector.
property | type | description | default |
---|---|---|---|
axis | Vector | The vector representing the axis to reflect on. |
example:
const vec1 = new Vector(5, 5);
const axis = new Vector(0, 1);
vec1.reflectN(axis);
Get the dot product of this vector and another.
property | type | description | default |
---|---|---|---|
other | Vector | The vector to dot this one against. |
example:
const vec1 = new Vector(5, 5);
const vec2 = new Vector(10, 25);
vec1.dot(vec2); // 175
Get the squared length of this Vector.
example:
const vec1 = new Vector(3, 4);
vec1.len2(); // 25
Get the length of this vector.
example:
const vec1 = new Vector(3, 4);
vec1.len(); // 5