A TypeScript 2D vector math library
npm install @graph-ts/vector2A TypeScript 2D vector math library.
For operations that change vector components, the Vector2 library
provides functional methods for working with immutable data as well as an interface for
working with mutable data.
For working with immutable data, all of the top-level functions provided by the library
are purely functional and return new Vector2 objects.
``typescript
import { Vector2, add } from '@graph-ts/vector2';
const a: Vector2 = { x: 0, y: 1 };
const b: Vector2 = { x: 1, y: 0 };
const c: Vector2 = add(a, b);
console.log(c); // { x: 1, y: 1 }
`
To apply operations to a target vector, the Vector2 library requires you to be explicitupdate()
about your intentions. The method returns an object that contains chainable
vector updating methods.
`typescript
import { Vector2, update } from '@graph-ts/vector2';
const a: Vector2 = { x: 0, y: 1 };
const b: Vector2 = { x: 1, y: 0 };
update(a) // update vector a by...
.add(b) // adding vector b and...
.translate(0, 1); // translating a distance of 1 along the x-axis
console.log(a); // { x: 2, y: 1 }
``