1.1.0 • Published 6 years ago

vktr v1.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

vktr

Package version Build status Coverage status Dependencies status Development dependencies status

Tiny, immutable and fast n-dimensional vector with pure functional calculations

Install

  • NPM: npm install vktr
  • Yarn: yarn add vktr

Usage

Browser

<script src="vktr.js"></script>
<script>
  const { multiply, Vector } = vktr;

  const a = new Vector(1, 2, 3);
  const b = 2;

  multiply(a, b); // [2, 4, 6]
</script>

NodeJS

const { add, Vector } = require('vktr');

const a = new Vector(1, 2, 3);
const b = new Vector(3, 2, 1);

add(a, b); // [4, 4, 4]

ES6+

import { substract, Vector } from 'vktr';

const a = new Vector(1, 2, 3);
const b = new Vector(3, 2, 1);

substract(a, b); // [-2, 0, 2]

ES6+ with individual imports

import map from 'vktr/src/map';
import Vector from 'vktr/src/vector';

const a = new Vector(4, 5, 6);
const fn = (component) => component * 2;

map(a, fn); // [8, 10, 12]

Overview

new Vector(...number)

Creates a n-dimensional Vector object which can be used for calculations. A Vector has an arbitrary number of components, which has to be greater than zero.

Once a vector is created, it's components cannot be modified without producing a new Vector. This effect is call immutability, which causes predictable and efficient code.

However, there are some computed properties that are not calculated until you try to access them. Appart from this properties, the only way of making calculations is producing new Vectors.

Computed properties

  • angle: number: Vector's polar form angle. Only valid for 2-dimensional vectors.
  • components: Array<number>: Array of components of the vector.
  • dimensions: number: number of dimensions of the vector.
  • magnitude: number: Euclidean length of the vector. Alias for length property.
  • length: number: Euclidean length of the the vector.

  • x: number: Returns the first component of the vector, if any.
  • y: number: Returns the second component of the vector, if any.
  • z: number: Returns the third component of the vector, if any.
  • t: number: Returns the fourth component of the vector, if any.

Math functions

When using notation like n-Vector inside this documentation, it does not refer to different Vector classes, just the number of dimensions allowed for each vector.

As Vectors are not mutable, every math function applied to them produces a new Vector instead of modifying the existing. This results in an predictable easy-to-test environment. Each function has complete documentation about the expected data and result: