1.1.2 • Published 1 year ago

vecjs v1.1.2

Weekly downloads
5
License
MIT
Repository
github
Last release
1 year ago

VecJS

Immutable functional vector library. Currently it only has Vec2 the 2D vector type.

Installation

npm i vecjs

Usage

Usage is simple and readable:

import { Vec2 } from 'vecjs'

let a = new Vec2(1,2)
let b = new Vec2({x: 3, y:4})
let c = {x: 4, y:5}

let res = (a) .add (b) .sub (c)  // x:0, y: 1

console.log(res)

Functions:

nameexampledescription
add(a) .add (b)(a.x+b.x, a.y+b.y)
sub(a) .sub (b)(a.x-b.x, a.y-b.y)
mul(a) .mul (b)(a.xb.x, a.yb.y)
div(a) .div (b){ res: (a.x/b.x, a.y/b.y), err: boolean }
avg(a) .avg (b)( (a.x+b.x)/2 , (a.y+b.y)/2 )
dot(a) .dot (b)a.xb.x + a.yb.y
max(a) .max (b)(max(a.x, b.x), max(a.y, b.y))
min(a) .min (b)(min(a.x, b.x), min(a.y, b.y))
map(a) .map (x => x*2)(f(a.x), f(b.x))
furrier(a) .furrier (b){ res: the furrier coefficient of a and b (number), err: boolean }
proj(a) .proj (b){ res: projection of a to the vector b (Vec2), err: boolean }
randomVec2.random()(random between 0 and 1, random between 0 and 1) with borders
nullVec2.null()(0,0) null element
idVec2.id()(1,1) identity element

Computed properties:

nameexampledescription
lenSquare(a) .lenSquarea.x^2 + b.y^2
len(a) .lensqrt(a.x^2 + b.y^2)
norm(a) .norm{ res: (a.x/a.len , a.y/a.len), err: boolean }
normal1(a) .normal1(- a.y, a.x)
normal2(a) .normal2( a.y, - a.x)
inverse(a) .inverse(- a.x, - a.y)
sign(a) .sign(sgn(a.x), sgn(a.y))
sum(a) .suma.x + a.y
diff(a) .diffa.x - a.y

Properties:

nameexampledescription
x(a).xThe x coordinate
y(a).yThe y coordinate