@graph-ts/vector2
TypeScript icon, indicating that this package has built-in type declarations

1.3.0 • Public • Published

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 }

Readme

Keywords

none

Package Sidebar

Install

npm i @graph-ts/vector2

Weekly Downloads

1

Version

1.3.0

License

MIT

Unpacked Size

19 kB

Total Files

13

Last publish

Collaborators

  • atdyer