Alias for horizontalAngle
Kept for compatibility with Victor.js. Might change later
Gets the angle θ in the plane (in radians, -π < θ < π ) between the positive X axis and the ray from (0, 0) to the point (x, y).
This is also the phase of the complex number x + iy.
Caution: The direction is not the same as verticalAngle()
The angle in radians
Alias for .horizontalAngleDeg()
Kept for compatibility with Victor.js. Might change later
Gets the angle θ in the plane (in degrees, -180 < θ < 180 ) between the positive X axis and the ray from (0, 0) to the point (x, y)
This is also the phase of the complex number x + iy.
Caution: The direction is not the same as verticalAngleDeg()
The angle in degrees
Alias for horizontalAngle()
Kept for compatibility with Victor.js. Might change later
Gets the angle θ in the plane (in radians, -π < θ < π ) between the positive X axis and the ray from (0, 0) to the point (x, y).
This is also the phase of the complex number x + iy.
Caution: The direction is not the same as verticalAngle()
The angle in radians
Gets the angle in degrees (0 < θ <= 180 between this vector and another one
If both vectors are null the method returns NaN
The second vector
The angle between both vectors in degrees
Gets the angle in radian (0 < θ <= π) between this vector and another one
The second vector
The angle between both vectors in radians
Gets the angle θ in the plane (in radians, -π < θ < π ) between the positive X axis and the ray from (0, 0) to the point (x, y).
This is also the phase of the complex number x + iy.
Caution: The direction is not the same as verticalAngle()
The angle in radians
Gets the angle θ in the plane (in degrees, -180 < θ < 180 ) between the positive X axis and the ray from (0, 0) to the point (x, y)
This is also the phase of the complex number x + iy.
Caution: The direction is not the same as verticalAngleDeg()
The angle in degrees
Gets the angle in degrees ( -180 < θ <= 180) between this vector and another one measured in a counterclockwise direction from this to the other one.
This method is roughly 20% slower than this.angleDegWith()
The second vector
The angle between both vectors in radians
Gets the angle in radian ( -π < θ <= π) between this vector and another one measured in a counterclockwise direction from this to the other one.
This method is roughly 20% slower than this.angleWith()
The second vector
The angle between both vectors in radians
const vec1 = new Vector(1, 0);
a = vec1.orientedAngleWith(new Vector(1, 0));
assert.equal(a, 0)
a = vec1.orientedAngleWith(new Vector(1, 1));
assert.equal(a, Math.PI / 4)
a = vec1.orientedAngleWith(new Vector(1, -1));
assert.equal(a, -Math.PI / 4)
a = vec1.orientedAngleWith(new Vector(-1, 0));
assert.equal(a, Math.PI)
Gets the angle θ in the plane (in rads -π < θ < π) between the positive Y axis and the ray from (0, 0) to the point (x, y)
Caution: The direction is not the same as horizontalAngle()
The angle in degrees
Gets the angle θ in the plane (in degrees -180 < θ < 180) between the positive Y axis and the ray from (0, 0) to the point (x, y)
Caution: The direction is not the same as horizontalAngleDeg()
The angle in degrees
Adds the given scalar to both vector axes
The scalar to add
this
for chaining capabilities
Adds the given scalar to the X axis
The scalar to add
this
for chaining capabilities
Adds the given scalar to the Y axis
The scalar to add
this
for chaining capabilities
Divides both axes of this vector by those of another one
The vector to divide by
this
for chaining capabilities
const vec1 = new Vector(100, 50);
const vec2 = new Vector(2, 2);
vec1.divide(vec2);
assert.equal(vec1.x, 50)
assert.equal(vec1.y, 25)
DivisionByZeroError If any axis of the argument vector is 0
Divides both vector axes by the given scalar
The scalar to divide by
this
for chaining capabilities
const vec = new Vector(100, 50);
vec.divideScalar(2);
assert.equal(vec.x, 50)
assert.equal(vec.y, 25)
DivisionByZeroError If the argument scalar is 0
Divides the X axis by the given scalar
The scalar to divide by
this
for chaining capabilities
const vec = new Vector(100, 50);
vec.divideScalarX(2);
assert.equal(vec.x, 50)
assert.equal(vec.y, 50)
DivisionByZeroError If x axis of argument vector is 0
Divides the Y axis by the given scalar
The scalar to divide by
this
for chaining capabilities
const vec = new Vector(100, 50);
vec.divideScalarY(2);
assert.equal(vec.x, 100)
assert.equal(vec.y, 25)
DivisionByZeroError If x axis of argument vector is 0
Divides the X axis of this vector by the X axis of another one
The other vector you want divide by
this
for chaining capabilities
const vec1 = new Vector(100, 50);
const vec2 = new Vector(2, 0);
vec1.divideX(vec2);
assert.equal(vec1.x, 50)
assert.equal(vec1.y, 50)
DivisionByZeroError If the X axis of the argument vector is 0
Divides the Y axis of this vector by the Y axis of another one
The other vector you want divide by
this
for chaining capabilities
const vec1 = new Vector(100, 50);
const vec2 = new Vector(0, 2);
vec1.divideY(vec2);
assert.equal(vec1.x, 100)
assert.equal(vec1.y, 25)
DivisionByZeroError If the Y axis of the argument vector is 0
Multiplies both vector axes by the given scalar
The scalar to multiply by
this
for chaining capabilities
Multiplies the X axis by the given scalar
The scalar to multiply by
this
for chaining capabilities
Multiplies the Y axis by the given scalar
The scalar to multiply by
this
for chaining capabilities
Subtracts the given scalar from both axes
The scalar to subtract
this
for chaining capabilities
Subtracts the given scalar from the X axis
The scalar to subtract
this
for chaining capabilities
Subtracts the given scalar from the Y axis
The scalar to subtract
this
for chaining capabilities
Returns true if this vector axes values are the same as another
The second vector
true if the vector magnitude is 0, false otherwise
Returns true if this vector is parallel to another one.
This method has a small tolerance so that vector which seems almost parallel are considered parallel. This is to avoid rounding errors inherent to floating point programming. (For example v.isParallelTo(v.rotateBy(2 * Math.PI)) would be likely to be false without this small tolerance).
true if the vector is parallel to the other one
Returns true if this vector is perpendicular to another one.
This method has a small tolerance so that vector which seems almost perpendicular are considered perpendicular. This is to avoid rounding errors inherent to floating point programming. (For example v.isParallelTo(v.rotateBy(Math.PI / 2)) would be likely to be false without this small tolerance).
true if the vector is perpendicular to the other one
A simple 2D vector class
Value of the X axis
Value of the Y axis
Returns an object representation of the vector
An object representation of the vector
Static
fromCreates a new instance from an array using first two items as x and y. (The array lenght must be at least 2)
Array with the x and y values at index 0 and 1 respectively
A new Vector instance
Static
fromCreates a new instance from an object ressembling a vector
(Object must have a x: number
and a y: number
property)
Object with properties x and/or y
A new Vector instance
Static
fromCreates a new instance from an angle in radians and a magnitude (The angle is from the positive x axis)
Object with properties x and/or y
Object with properties x and/or y
A new Vector instance
Same as distanceX()
but always returns an absolute number
The second vector
The absolute distance between the X axes
Same as distanceY()
but always returns an absolute number
The second vector
The absolute distance between the Y axes
Calculates the euclidean distance between this vector and another
The second vector
The euclidian distance between the vectors
Calculates the squared euclidean distance between this vector and another
The second vector
The squared euclidian distance between the vectors
Calculates the distance between the X axis of this vector the X axis of another
The second vector
The distance between the X axes
Calculates the distance between the X axis of this vector the X axis of another
The second vector
The distance between the Y axes
Alias for .length()
Calculates the length or magnitude of the vector
The magnitude of the vector
Alias for Vector.normalize
Resize the vector to clamp the magnitude to the max value. Also clamp to a minimum value if a second argument is specified.
This preserves the angle of the vector.
The maximum value for the Y axis
Optional
min: number(optional) The maximum value for the Y axis
this
for chaining capabilities
Clamp the value of the x axis to a maximum value. Also clamp to a minimum value if a second argument is specified
The maximum value for the X axis
Optional
min: number(optional) The maximum value for the X axis
this
for chaining capabilities
Clamp the value of the y axis to a maximum value. Also clamp to a minimum value if a second argument is specified
The maximum value for the Y axis
Optional
min: number(optional) The maximum value for the Y axis
this
for chaining capabilities
If the absolute value of the axes is greater than max
,
multiplies the axis by factor
The maximum value for both X and Y axes
Factor by which the axes are to be multiplied by
this
for chaining capabilities
If the absolute value of the X axis is greater than max
,
multiplies its value by factor
The maximum value for the X axis
Factor by which the axis is to be multiplied by
this
for chaining capabilities
If the absolute value of the Y axis is greater than max
,
multiplies its value by factor
The maximum value for the Y axes
Factor by which the axis is to be multiplied by
this
for chaining capabilities
Normalize the vector. (Keep direction but reduce length to 1)
this
for chaining capabilities
Resize the vector so that its direction is not changed but it's
magnitude is set to the new value. If the magnitude
argument is
negative, the angle of the resulting vector is rotated by 180 degees.
The new value of the vector's magnitude
this
for chaining capabilities
Fix both axes to a certain precision using Number.toFixed() on each axis
(default: 8)
this
for chaining capabilities
Rounds both axes to an integer value using Math.round()
this
for chaining capabilities
Calculates the cross product of this vector and another.
Note that the resulting scalar value is due to considering the z axes as 0 https://stackoverflow.com/a/243977
The second vector
The cross product of this vector and the other one
Calculates the dot product of this vector and another
The second vector
The dot product of this vector and the other one
Randomizes both vector axes with a value between 2 vectors
this
for chaining capabilities
Randomly choses one axis and randomizes it with a value between the corresponding axes of 2 other vectors
this
for chaining capabilities
Randomizes the X axis with a value between the X axes of 2 others vectors
this
for chaining capabilities
Randomizes the Y axis with a value between the Y axes of 2 others vectors
this
for chaining capabilities
Rotate the vector counter-clockwise by an angle in radians
The angle in radians to rotate the vector by
this
for chaining capabilities
Rotate the vector counter-clockwise by an angle in degrees
this
for chaining capabilities
Rotate the vector to an angle in radians using the positif X axis as origin, move counter-clockwise
this
for chaining capabilities
Rotate the vector to an angle in degrees using the positif X axis as origin, move counter-clockwise
this
for chaining capabilities
A simple 2D vector class