simplevectorsjs
v1.3.2
Published
SimpleVectorsJS is a library to make working with vectors in Node JS easy, including all standard vector operations and properties for n-dimension vectors
Downloads
5
Maintainers
Readme
SimpleVectorsJS
SimpleVectorsJS is a library to make working with vectors in Node JS easy, including all standard vector operations and properties for n-dimension vectors
Installation
npm install simplevectorsjs
Usage
Creating a vector
let { Vector, VectorConstants } = require("simplevectorsjs");
let a = new Vector(2, 0.5); //Create a new vector with components <-1, -1>
let b = new Vector();
b.fromMagnitudeAngle2D(10, Math.PI); //Set this vector from a magnitude and a direction (2D only)
let c = new Vector();
c.fromTwoPoints([0,0,0],[-2,2,1]); //Set this vector from two points
Basic vector operations
let d = a.add(b); //Add b to a
console.log("a+b: ", d.toString());
d = a.subtract(b); //Subtract b from a
console.log("a-b: ", d.toString());
d = b.multiply(5); //Multiply a by a scalar
console.log("5*b: ", d.toString());
Getting the direction/unit vector and changing the vector's magnitude
d = a.unit(); //Get the unit vector
d = d.multiply(10); //And scale to 5
//OR, set the magnitude of the vector:
//d = a.getCopy(); //Get a copy of the vector
//d.magnitude = 5;
console.log("5*(a/|a|): ", d.toString());
More vector operations
d = c.cross(new Vector(-1,2,1));
console.log("c x <-1, 2, 1>: ", d.toString());
let e = a.dot(b);
console.log("a•b: ", e);
e = c.tripleScalarProduct(new Vector(1,2,0), new Vector(-2,0,1));
console.log("c•(<1,2,0> x <-2,0,1>): ", e);
e = a.scal(b); //Scalar projection of b onto a
console.log("Scal_a b: ", e);
d = a.proj(b); //Vector projection of b onto a
console.log("Vect_a b: ", d.toString());
e = b.angle(a); //Angle between a and b
console.log("Absolute angle between a and b: ", e);
e = a.angle(VectorConstants.D2.i); //Angle between b and positive x-axis
console.log("Absolute angle between a and positive x-axis (i): ", e);
Vector properties
console.log("Magnitude of a: ", a.magnitude);
console.log("Number of components in a: ", a.size);
console.log("Is unit vector? a/|a|: ", a.multiply(1/a.magnitude).isUnit());
console.log("Is equal vector? a/|a|==a: ", a.unit().isEqual(a));
License
Copyright 2020 Alex Mous
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Documentation
Simplevectors
- Simplevectors
- .Vector
- new Vector(...components)
- static
- .Vector#magnitude : number
- .Vector#size : number
- .Vector#magnitude : number
- inner
- ~fromMagnitudeAngle2D(magnitude, angle)
- ~fromMagnitudeAngle2D(point1, point2)
- ~unit() ⇒ Vector
- ~multiply(k) ⇒ Vector
- ~add(vect) ⇒ Vector
- ~subtract(vect) ⇒ Vector
- ~cross(vect) ⇒ Vector
- ~proj(vect)
- ~getCopy() ⇒ Vector
- ~dot(vect) ⇒ number
- ~tripleScalarProduct(vect1, vect2) ⇒ number
- ~angle(vect) ⇒ number
- ~scal(vect) ⇒ number
- ~toString() ⇒ string
- ~isUnit() ⇒ boolean
- ~isEqual(vect) ⇒ boolean
- .VectorConstants : VectorConstants
- .Vector
Simplevectors.Vector
Simplevectors is a library to make working with vectors in Node JS easy, including all standard vector operations and properties for n-dimension vectors
Kind: static class of Simplevectors
- .Vector
- new Vector(...components)
- static
- .Vector#magnitude : number
- .Vector#size : number
- .Vector#magnitude : number
- inner
- ~fromMagnitudeAngle2D(magnitude, angle)
- ~fromMagnitudeAngle2D(point1, point2)
- ~unit() ⇒ Vector
- ~multiply(k) ⇒ Vector
- ~add(vect) ⇒ Vector
- ~subtract(vect) ⇒ Vector
- ~cross(vect) ⇒ Vector
- ~proj(vect)
- ~getCopy() ⇒ Vector
- ~dot(vect) ⇒ number
- ~tripleScalarProduct(vect1, vect2) ⇒ number
- ~angle(vect) ⇒ number
- ~scal(vect) ⇒ number
- ~toString() ⇒ string
- ~isUnit() ⇒ boolean
- ~isEqual(vect) ⇒ boolean
new Vector(...components)
Vector class
| Param | Type | Description | | --- | --- | --- | | ...components | number | Components of the vector to construct |
Vector.Vector#magnitude : number
Get the magnitude of the vector
Kind: static property of Vector
Vector.Vector#size : number
Get the number of components of the vector
Kind: static property of Vector
Vector.Vector#magnitude : number
Set the new magnitude
Kind: static property of Vector
Vector~fromMagnitudeAngle2D(magnitude, angle)
Set this vector from magnitude and angle from positive x-axis in 2D
Kind: inner method of Vector
| Param | Type | Description | | --- | --- | --- | | magnitude | number | Magnitude | | angle | number | Angle (radians) |
Vector~fromMagnitudeAngle2D(point1, point2)
Set this vector from the difference between two points
Kind: inner method of Vector
| Param | Type | Description | | --- | --- | --- | | point1 | Array.<number> | Point 1 (array of distance in each dimension) | | point2 | Array.<number> | Point 2 (same number of dimensions as Point 1) |
Vector~unit() ⇒ Vector
Unit vector
Kind: inner method of Vector
Returns: Vector - The unit vector
Vector~multiply(k) ⇒ Vector
Scalar multiplication
Kind: inner method of Vector
Returns: Vector - Scaled vector
| Param | Type | Description | | --- | --- | --- | | k | number | Scalar k to multiply vector by |
Vector~add(vect) ⇒ Vector
Add vect to this vector
Kind: inner method of Vector
Returns: Vector - New vector
| Param | Type | Description | | --- | --- | --- | | vect | Vector | Vector to add to this |
Vector~subtract(vect) ⇒ Vector
Subtract vect from this vector
Kind: inner method of Vector
Returns: Vector - Difference between this and vect
| Param | Type | Description | | --- | --- | --- | | vect | Vector | Vector to subtract from this |
Vector~cross(vect) ⇒ Vector
Cross product (3D vectors) of vect with this vector (this x vect)
Kind: inner method of Vector
Returns: Vector - The cross product vector
| Param | Type | Description | | --- | --- | --- | | vect | Vector | Vector to do a cross product with |
Vector~proj(vect)
Vector projection of vect onto this vector
Kind: inner method of Vector
| Param | Type | | --- | --- | | vect | Vector |
Vector~getCopy() ⇒ Vector
Get a copy of this vector
Kind: inner method of Vector
Returns: Vector - A copy of this vector
Vector~dot(vect) ⇒ number
Dot product
Kind: inner method of Vector
Returns: number - The dot product of the two vectors
| Param | Type | Description | | --- | --- | --- | | vect | Vector | Vector to do a dot product with |
Vector~tripleScalarProduct(vect1, vect2) ⇒ number
Get the triple scalar product of this vector, vect1 and vect2 (this•(vect1 x vect2))
Kind: inner method of Vector
Returns: number - Triple scalar product
| Param | Type | Description | | --- | --- | --- | | vect1 | Vector | First vector | | vect2 | Vector | Second vector |
Vector~angle(vect) ⇒ number
Angle between this vector and vect (0 <= angle <= PI)
Kind: inner method of Vector
Returns: number - Angle (0 <= theta <= PI)
| Param | Type | Description | | --- | --- | --- | | vect | Vector | Vector to calculate angle between |
Vector~scal(vect) ⇒ number
Scalar projection of vect onto this vector
Kind: inner method of Vector
Returns: number - Projection
| Param | Type | | --- | --- | | vect | Vector |
Vector~toString() ⇒ string
Get the string representation of the vector
Kind: inner method of Vector
Returns: string - Vector as a string
Vector~isUnit() ⇒ boolean
Is this a unit vector?
Kind: inner method of Vector
Vector~isEqual(vect) ⇒ boolean
Is this the same as vect?
Kind: inner method of Vector
| Param | Type | | --- | --- | | vect | Vector |
Simplevectors.VectorConstants : VectorConstants
Kind: static constant of Simplevectors