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 degrees
const vec1 = new Vector(1, 0);
a = vec1.orientedAngleDegWith(new Vector(1, 0));
assert.equal(a, 0)
a = vec1.orientedAngleDegWith(new Vector(1, 1));
assert.equal(a, 45)
a = vec1.orientedAngleDegWith(new Vector(1, -1));
assert.equal(a, -45)
a = vec1.orientedAngleDegWith(new Vector(-1, 0));
assert.equal(a, 180)
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)
Computes the slope (or gradient) of the line passing by the vector.
Note that the slope is positive for vectors in quadrants I and III,
negative for vectors in quadrants II and IV, 0
for horizontal vectors
and Infinity
for vertical vectors
The slope of the line passing by the vector
assert.equal(0, (new Vector(1, 0)).slope());
assert.equal(0, (new Vector(-1, 0)).slope());
assert.equal(1, (new Vector(1, 1)).slope());
assert.equal(1, (new Vector(-1, -1)).slope());
assert.equal(-1, (new Vector(-1, 1)).slope());
assert.equal(-1, (new Vector(1, -1)).slope());
assert.equal(±Infinity, (new Vector(0, 1)).slope());
assert.equal(±Infinity, (new Vector(0, -1)).slope());
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 radians
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
DivisionByZeroError If any axis of the argument vector is 0
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)
Divides both vector axes by the given scalar
The scalar to divide by
this
for chaining capabilities
DivisionByZeroError If the argument scalar is 0
const vec = new Vector(100, 50);
vec.divideScalar(2);
assert.equal(vec.x, 50)
assert.equal(vec.y, 25)
Divides the X axis by the given scalar
The scalar to divide by
this
for chaining capabilities
DivisionByZeroError If x axis of argument vector is 0
const vec = new Vector(100, 50);
vec.divideScalarX(2);
assert.equal(vec.x, 50)
assert.equal(vec.y, 50)
Divides the Y axis by the given scalar
The scalar to divide by
this
for chaining capabilities
DivisionByZeroError If x axis of argument vector is 0
const vec = new Vector(100, 50);
vec.divideScalarY(2);
assert.equal(vec.x, 100)
assert.equal(vec.y, 25)
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
DivisionByZeroError If the X axis of the argument vector is 0
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)
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
DivisionByZeroError If the Y axis of the argument vector is 0
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)
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
Enforce the value of both axes to be between a max and min value. See clampX and clampY for details.
This is equivalent to calling .clampX(min, max).clampY(min, max)
this
for chaining capabilities
Use a number to enforce the max value of the axes of this vector.
The maximum value for the axes
Resize the vector to enforce its magnitude to be between and max and a min value.
If only one value is provided, it is used as the max bound and no min bound is enforced.
If two values are provided the method will sort them to use the largest
one as the max bound and the smallest one as the min bound.
These values must be of the same type (either two Vector or two
number
)
This preserves the angle of the vector.
The minimum value for the magnitude
The maximum value for the magnitude
this
for chaining capabilities
Use a number to enforce the max value of the magnitude of this vector.
The maximum value for the magnitude
Enforce the value of the X axis to be between a max and min value.
If only one value is provided, it is used as the max bound and no min bound is enforced.
If two values are provided the method will sort them to use the largest
one as the max bound and the smallest one as the min bound.
These values must be of the same type (either two Vector or two
number
)
The minimum value for the X axis
The maximum value for the X axis
this
for chaining capabilities
Use a number to enforce the max value of the X axis of this vector.
The maximum value for the X axis
Enforce the value of the Y axis to be between a max and min value.
If only one value is provided, it is used as the max bound and no min bound is enforced.
If two values are provided the method will sort them to use the largest
one as the max bound and the smallest one as the min bound.
These values must be of the same type (either two Vector or two
number
)
The minimum value for the Y axis
The maximum value for the Y axis
this
for chaining capabilities
Use a number to enforce the max value of the Y axis of this vector.
The maximum value for the Y axis
Returns true if this vector axes values are close to the axes of the other
vector, within the margin of the epsilon
parameter.
This is useful when dealing with floating point errors. You probably want to use this method rather than isEqualTo.
The second vector
(default 1e-6
) The size of the margin, you want to keep this value small
true if the vector axes are close to the other one's within the provided margin
const vec1 = new Vector(100, 50);
const vec2 = new Vector(100.000000001, 49.99999999998);
assert.false(vec1.isEqualTo(vec2);
assert.true(vec1.isCloseTo(vec2);
// Useful when manipulating vectors
const vec1Rot = vec1.clone().rotateByDeg(360)
// => {x: 100.00000000000001, y: 49.99999999999998}
assert.true(vec1.isCloseTo(vec1Rot));
assert.false(vec1.isEqualTo(vec1Rot));
Returns true if this vector axes values are exactly the same as another.
Be cautious with floating point errors. If vectors are close but not exactly
the same this method returns false
, in most cases you probably want to use
isCloseTo instead.
The second vector
true if the vector axes are equal to the other one's
const vec1 = new Vector(100, 50);
const vec2 = new Vector(100, 50);
assert.true(vec1.isEqualTo(vec2);
const vec3 = new Vector(0, 0);
assert.false(vec1.isEqualTo(vec3);
// Beware when manipulating vectors
const vec1Rot = vec1.clone().rotateByDeg(360)
// => {x: 100.00000000000001, y: 49.99999999999998}
assert.false(vec1.isEqualTo(vec1Rot));
Returns true if this vector is parallel to another one.
This method has a small tolerance so that vectors 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 vectors which seems almost perpendicular
are considered perpendicular. This is to avoid rounding errors inherent to floating
point programming. (For example v.isParallelTo(v.isPerpendicularTo(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
Creates a clone of this vector with the same properties. This is particularly useful when you need to apply the method which modifies the vector but keep the original vector untouched
The instance of the newly created vector
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 length 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 resembling 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
Static
randomCalculates the euclidean distance between this vector and another one
The second vector
The euclidean distance between the vectors
Calculates the Chebyshev distance between this vector and another. This is the greatest absolute difference between the respective coordinates of the vectors.
The second vector
The Chebyshev distance between the vectors
Calculates the Manhattan distance between this vector and another. This is the sum of the absolute difference between the respective coordinates of the vectors.
The second vector
The Manhattan distance between the vectors
Calculates the distance from the X axis of another vector to the X axis of this one
The second vector
The distance between the X axes
Calculates the distance from the Y axis of another vector to the Y axis of this one
The second vector
The distance between the Y axes
Performs a linear blend / interpolation towards another vector
The mixFactor
parameter is the amount to interpolate between this vector and
the other vector. 0.0 keeps the axes equal to this vector's, 0.5 is
halfway between, and 1.0 sets the axes equal to the other vector's.
The other vector
The blend amount (optional, default: 0.5)
this
for chaining capabilities
Performs a linear blend / interpolation of the X axis towards another vector.
The mixFactor
parameter is the amount to interpolate between this vector and
the other vector. 0.0 keeps the X axis equal to this vector's, 0.5 is
halfway between, and 1.0 sets the X axis equal to the other vector's.
The other vector
The blend amount [0, 1] (optional, default: 0.5)
this
for chaining capabilities
Performs a linear blend / interpolation of the Y axis towards another vector
The mixFactor
parameter is the amount to interpolate between this vector and
the other vector. 0.0 keeps the Y axis equal to this vector's, 0.5 is
halfway between, and 1.0 sets the Y axis equal to the other vector's.
The other vector
The blend amount (optional, default: 0.5)
this
for chaining capabilities
Projects a vector onto the direction of another vector
The second vector
this
for chaining capabilities
DivisionByZeroError If the vector to project onto is zero. This prevents NaN
results.
const vec1 = new Vector(100, 0);
const vec2 = new Vector(100, 100);
vec1.projectOnto(vec2);
assert.equal(vec1.x, 50)
assert.equal(vec1.y, 50)
Multiplies both axes by -1
this
for chaining capabilities
Multiplies the X axis by -1
this
for chaining capabilities
Multiplies the Y axis by -1
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
Alias for mag
Alias for Vector.normalize
Normalize the vector (Keep the direction but change the magnitude to 1)
this
for chaining capabilities
DivisionByZeroError If the vector is zero
Calculates the squared length (or squared magnitude) of the vector
This is faster than mag because we don't use Math.sqrt
.
The squared magnitude of the vector
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. See stackoverflow.com
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 axes of the vector with a value between 2 vectors
this
for chaining capabilities
Randomly choses one axis and randomizes it with a value between the corresponding axis 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
Alias for Vector.normalize
Normalize the vector (Keep the direction but change the magnitude to 1)
this
for chaining capabilities
DivisionByZeroError If the vector is zero
Normalize the vector (Keep the direction but change the magnitude to 1)
this
for chaining capabilities
DivisionByZeroError If the vector is zero
Resizes the vector so that its direction is not changed but its
magnitude is set to the new value. If the magnitude
argument is
negative, the angle of the resulting vector is rotated by 180 degrees.
The new value of the vector's magnitude
this
for chaining capabilities
DivisionByZeroError If the vector is zero
const vec1 = new Vector(0, 1);
vec1.resize(10);
assert.equal(vec1.horizontalAngle(), 90);
assert.equal(vec1.magnitude(), 10);
vec1.resize(-2);
assert.equal(vec1.horizontalAngle(), -90);
assert.equal(vec1.magnitude(), 2);
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 positive X axis as origin, move counter-clockwise
The angle in radians to rotate the vector to
this
for chaining capabilities
Rotate the vector to an angle in degrees using the positive X axis as origin, move counter-clockwise
The angle in degrees to rotate the vector to
this
for chaining capabilities
A simple 2D vector class