Arrow2D library
Arrow2D is a library specific to 2D vector operations.
Instalation
Arrow2D is a Node.js module, to install it first download and install Node.js.
Having Node.js and npm installed, simply run:
npm i arrow2d
Quick start
const CreateVector StaticArrow2D = ; //Create a VectorForm vector.const vector1 = ;const vector2 = ;const vector3 = StaticArrow2D; //Create an ArrayForm vector.const arrayVector = const arrayVector2 = StaticArrow2D;
Operations
normalize()
Normalizes the vector, turning it into a unit vector.
Example
const vector = ;vector; // Normalize the vector.
;
add(vectorToAdd)
Adds another vector components into the current vector.
Example
const vector1 = ;const vector2 = ;vector1; // Adds vector2 to vector1
;
sub(vectorToSubtract)
Subtracts another vector components from the current vector.
Example
const vector1 = ;const vector2 = ;vector1; // Subtracts vector2 from vector1
;
scalarMult(scalar)
Multiplies the current vector components with a scalar.
Example
const vector = ;const scalar = 2;vector; // Multiply vector by scalar.
scalarDiv(scalar)
Divides the current vector components with a scalar.
Example
const vector = ;const scalar = 2;vector; // Divide vector by scalar.
;
add(firstVector, secondVector) - STATIC
Static version of add.
Add the components of two vectors and returns a new one.
Example
const vector1 = ;const vector2 = ;const vector3 = StaticArrow2D; // Add vectors and return a new one.
;
sub(firstVector, secondVector) - STATIC
Static version of sub.
Subtracts the components of two vectors and return a new one.
Example
const vector1 = ;const vector2 = ;const vector3 = StaticArrow2D; // Subtract vectors and return a new one.
;
dotProduct(firstVector, secondVector) - STATIC
Returns a scalar value from the dot product of two vectors.
Example
const vector1 = ;const vector2 = ;const result = StaticArrow2D; // Returns a scalar.
scalarDiv(vector, scalar) - STATIC
Static version of scalarDiv.
Divides a vector by a scalar and returns a new one.
Example
const vector1 = ;const scalar = 2;const vector3 = StaticArrow2D; // Returns a new vector divided by the scalar.
Utils
constrainMagnitude()
Constrain the magnitude of the vector to a value.
Example
const vector = ;vector; // Constrains the magnitude of the vector to 3.
heading()
Returns the direction the vector is pointing.
Example
const vector1 = ;const direction = vector1;
rotate(angle)
Rotate the vector by an angle.
Example
const vector = ;vector; // Rotate vector by 90 degrees.
vectorToArray(vector) - STATIC
Returns the array form of a vector. The operations that take a vector as an input will receive an array instead.
Example
const vector = ;const array = StaticArrow2D;
arrayToVector(array) - STATIC
Returns the vector form of an array.
Example
const array = 5 2;const vector = StaticArrow2D;
angleBetween(firstVector, secondVector, degrees) - STATIC
Returns the angle, in radians, between two vectors. If you want the angle in degress, set "degress" to true.
Example
const vector1 = ;const vector2 = ;const angle = StaticArrow2D;
orthogonalVector(vector) - STATIC
Returns an orthogonal vector to the input vector.
Example
const vector = ;const orthogonal = StaticArrow2D;