1.0.0 • Published 5 years ago
js-vector-2d v1.0.0
js-vector-2d
A javascript library for 2D vectors
Installing
$ npm i js-vector-2d
Importing vector class
const Vector2D = require('js-vector-2d');
Creating vector object
const v1 = new Vector2D(3, 4);
const v2 = new Vector2D(1, 2);
Different vector operations
// copy a vector
const v3 = v1.copy(); // returns a vector object with values of vector v1
// vector addition
v1.add(v2);
// vector subtration
v1.sub(v2);
// vector scalar multiplication
v1.mul(2); // 2 is a scalar
// vector scalar division
v1.div(2); // 2 is a scalar
// calculate magnitude
v1.mag(); // returns magnitude of vector v1
// normalize a vector
v1.normalize(); // normalizes a vector i.e. sets the magnitude of vector to 1 without changing its direction
// set magnitude of vector
v1.set_mag(2); // normalizes a vector and sets its magnitude to a scalar value
// calculate dot product
v1.dot(v2); // returns dot product of vector v1 & v2
// calculate angle between 2 vectors ( in radians )
v1.angle(v2); // returns the angle ( in radians ) between vector v1 & v2
// calculate angle between 2 vectors ( in degree )
v1.angle_in_degree(v2); // returns the angle ( in degree ) between vector v1 & v2
// rotate a vector ( in radians )
v1.rotate(0.1); // rotates a vector ( in radians ), values ranges from -1 to 1
// where,
// -1 is ( PIE radian )
// 0 is ( PIE / 2 radian ) and
// 1 is ( 0 radian )
// rotate a vector ( in degree )
v1.rotate_in_degree(1); // rotates a vector ( in degree )
1.0.0
5 years ago