vector-fun
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Vector-Fun

A simple functional vector mathematics library written in Typescript.

Vector object shape

2d:

// { x: 0, y: 0 };

3d:

// { x: 0, y: 0, z: 0 };

Creating a vector object

Null vector

const vector2d = vector2();
// {x: 0, y: 0}

const vector3d = vector3();
// {x: 0, y: 0, z: 0}

Vector with equal components

const vector2d = vector2(1);
// {x: 1, y: 1}

const vector3d = vector3(1);
// {x: 1, y: 1, z: 1}

Vector from individual components

const vector2d = vector2(1, 2.6);
// {x: 1, y: 2.6}

const vector3d = vector3(1, 2.6, 3);
// {x: 1, y: 2.6, z: 3}

Vector from an array

const vector2d = vector2([1, 2]);
// {x: 1, y: 2}

const vector3d = vector3([1, 2, 3]);
// {x: 1, y: 2, z: 3}

Copied vector

const source2d = vector2(1, 2);
const vector2d = vector2(source2d);
// {x: 1, y: 2}

vector2d !== source2d;
// true

const source3d = vector3(1, 2, 3);
const vector3d = vector2(source3d);
// {x: 1, y: 2, z: 3}

vector3d !== source3d;
// true

Single vector operations

2d only

  • Direction in radians
  • Normalization
  • Rotation by radians
  • Scaling to a new magnitude

Both 2d and 3d

  • Convert to array
  • Division by a scalar
  • Magnitude
  • Multiplication by a scalar
  • Type guard (isVector2 and isVector3)
  • Vector inversion

Two vector operations

  • Addition
  • Equality
  • Subtraction

Readme

Keywords

Package Sidebar

Install

npm i vector-fun

Weekly Downloads

6

Version

2.0.0

License

MIT

Unpacked Size

20.7 kB

Total Files

45

Last publish

Collaborators

  • yngwi