2d vector { x, y }
for math / physics
npm install vector-es
import Vector from 'vector-es';
const vector = new Vector(1, 2);
vector.mult(2); // { x: 2, y 4}
constructor :: (Number, Number) -> Vector
a vector is shape of { x, y }
x
y
All static methods are immutable.
-
add :: (Vector, Vector) -> Vector
add two vectors
-
sub :: (Vector, Vector) -> Vector
subtract one vector from another.
-
mult :: (Vector, Number) -> Vector
multiply by scalar
-
div :: (Vector, Number) -> Vector
divide by scalar
-
dot :: (Vector, Vector) -> Number
dot product
-
cross :: (Vector, Vector) -> Number
cross product
-
mag :: Vector -> Number
the magnitude of a vector
-
magSq :: Vector -> Number
the square of the magnitude
-
dir :: Vector -> Number
the direction of a vector
-
dist :: (Vector, Vector) -> Number
the distance between two vectors
-
distSq :: (Vector, Vector) -> Number
the square of the distance between two vectors
-
angle :: (Vector, Vector) -> Number
the angle between two vectors, range [0, PI]
-
create :: (Number, Number) -> Vector
create a vector by rectangular form
-
fromAngle :: (Number, Number) -> Vector
create a vector by angle
-
normalize :: Vector -> Vector
normalize the vector to a length of 1
-
clone :: Vector -> Vector
create a new vector copy
All instance methods are very similar to the statics. there're two differences.
-
first argument
instance methods don't need first argument, it's itself.
-
mutable
most of methods are mutable.
-
add :: Vector -> Vector
adds a vector
-
sub :: Vector -> Vector
subtract from another vector
-
mult :: Number -> Vector
multiply a vector by a scalar
-
div :: Number -> Vector
divide a vector by a scalar
-
dot :: Vector -> Number
dot product
-
cross :: Vector -> Number
cross product
-
mag :: () -> Number
the magnitude of a vector
-
magSq :: () -> Number
the square of the magnitude
-
dir :: () -> Number
the direction of a vector
-
dist :: Vector -> Number
the distance between two vectors
-
distSq :: Vector -> Number
the square of the distance between two vectors
-
angle :: Vector -> Number
the angle between two vectors, range [0, PI]
-
clone :: () -> Vector
create a vector copy, this's an immutable method
-
normalize :: () -> Vector
create a unit vector
-
set :: (Number, Number) -> Vector
set x, y of this vector
-
setX :: Number -> Vector
set x of this vector
-
setY :: Number -> Vector
set y of this vector
-
setMag :: Number -> Vector
set the magnitude of this vector
-
limit :: Number -> Vector
limit the magnitude of this vector