@graph-ts/vector2
v1.3.0
Published
A TypeScript 2D vector math library
Downloads
10
Readme
Vector2
A TypeScript 2D vector math library.
Mutable and Immutable Data
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.
Functional Methods
For working with immutable data, all of the top-level functions provided by the library
are purely functional and return new Vector2
objects.
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 }
Method chaining
To apply operations to a target vector, the Vector2
library requires you to be explicit
about your intentions. The update()
method returns an object that contains chainable
vector updating methods.
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 }