e-vector
v1.0.1
Published
A 2D or 3D Euclidean vector library for Node.js and the browser.
Downloads
1
Readme
E-Vector
This is a library to work with two or three dimensional Euclidean vectors.
Installation
Install using npm
,
npm install --save e-vector
Use as ES Module
import Vector from "e-vector";
API Docs
Table of Contents
- Vector
- copy
- fromAngle
- equals
- mag
- magSq
- add
- sub
- mul
- div
- dist
- dot
- cross
- normalize
- setMag
- heading
- project
- round
- ceil
- floor
- abs
Vector
packages/e-vector/src/e-vector.js:22-26
Create a vector by specifying its components.
Parameters
x
number Value of the x componenty
number Value of the y componentz
number Value of the z component. If it is not provided, it is assumed to be zero (optional, default0
)
Examples
const v = Vector(3, 4);
const u = Vector(3, 4, 5);
Returns Vector
copy
packages/e-vector/src/e-vector.js:41-41
Create a copy of the given vector.
Parameters
v
Vector The vector to copy
Examples
var v = Vector(3, 4);
var u = Vector.copy(v);
u.toString(); // Vector(3, 4, 0)
Returns Vector A vector with the same components as the given vector
fromAngle
packages/e-vector/src/e-vector.js:54-54
Create a new 2D unit vector from the given angle (in radians).
Parameters
angle
number Angle in radians
Examples
var v = Vector.fromAngle(Math.atan2(4, 3));
v.toString(); // Vector(0.6, 0.8, 0)
Returns Vector A new vector with heading equal to given angle and a magnitude of 1
equals
packages/e-vector/src/e-vector.js:73-73
Check the equality of two vectors.
Examples
const u = Vector(3, 4);
const v = Vector(4, 5);
const w = Vector(3, 4);
Vector.equals(u, v); // false
Vector.equals(u, w); // true
u === w; // false
Returns boolean true if the vectors are equal
mag
packages/e-vector/src/e-vector.js:81-81
Compute the magnitude of the vector.
Parameters
v
Vector
Returns number Magnitude of the given vector.
magSq
packages/e-vector/src/e-vector.js:89-89
Compute the square of magnitude of the vector.
Parameters
v
Vector
Returns number Square of magnitude of the given vector.
add
packages/e-vector/src/e-vector.js:98-98
Add two vectors.
Parameters
Returns Vector Resultant vector.
sub
packages/e-vector/src/e-vector.js:107-109
Subtract the second vector from the first.
Parameters
Returns Vector Vector result of v
-
u
.
mul
packages/e-vector/src/e-vector.js:118-118
Multiply the vector with a scalar.
Parameters
Returns Vector The resultant vector.
div
packages/e-vector/src/e-vector.js:127-130
Divide the vector with a scalar.
Parameters
Returns Vector If the scalar is 0
, return Vector(0, 0)
, else compute the vector.
dist
packages/e-vector/src/e-vector.js:139-141
Compute euclidean distance between two vectors.
Parameters
Returns number The distance between v
and u
.
dot
packages/e-vector/src/e-vector.js:151-151
Compute the dot product of two vectors.
Parameters
Returns number The result of u
⋅
v
.
cross
packages/e-vector/src/e-vector.js:161-166
Compute the cross product of two vectors. Only defined for three dimensional vectors.
Parameters
Returns Vector The result of u
×
v
.
normalize
packages/e-vector/src/e-vector.js:174-174
Change the magnitude/length of the vector to 1 without changing its angle/direction.
Parameters
u
Vector
Returns Vector Vector with magnitude of 1 and direction same as u
.
setMag
packages/e-vector/src/e-vector.js:183-183
Set the magnitude/length of the vector without changing its angle/direction.
Parameters
Returns Vector A new vector with magnitude of m and direction same as u
.
heading
packages/e-vector/src/e-vector.js:191-191
Compute the direction/angle of the vector in radians.
Parameters
u
Vector
Returns number Angle in radians
project
packages/e-vector/src/e-vector.js:200-202
Returns the projection of the second vector onto the first.
Parameters
on
Vector The vector on which the second vector will be projected.
Returns Vector The projected vector.
round
packages/e-vector/src/e-vector.js:216-216
Returns new vector with it's components rounded.
Parameters
u
Vector
Examples
var u = Vector(10.2, 10.9);
var roundedVector = Vector.round(u);
console.log(roundedVector); // Vector(10, 11)
Returns Vector v
ceil
packages/e-vector/src/e-vector.js:230-230
Returns new vector with the nearest non-fractional values for the components.
Parameters
u
Vector
Examples
var u = Vector(10.2, 10.9);
var ceilVector = Vector.ceil(u);
console.log(ceilVector); // Vector(11, 11)
Returns Vector v
floor
packages/e-vector/src/e-vector.js:244-244
Returns new vector with the nearest smaller non-fractional values for the components.
Parameters
u
Vector
Examples
var u = Vector(10.2, 10.9);
var floorVector = Vector.floor(u);
console.log(floorVector); // Vector(10, 10)
Returns Vector v
abs
packages/e-vector/src/e-vector.js:258-258
Returns new vector with the absolute values for the components.
Parameters
u
Vector
Examples
var u = Vector(-5, 10);
var absVector = Vector.abs(u);
console.log(absVector); // Vector(5, 10)
Returns Vector v
License
See LICENSE.md