npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vector-math

v1.1.1

Published

Zero dependency 2D & 3D vector mathematical operations

Downloads

62

Readme

vector-math

Overview

Zero dependency JavaScript library for performing fommon vector calculations. The library can handle 2D/3D vectors.

[!NOTE] The library was originally written back in 2018, but has since been re-written to make use of an ES6 class instead of separate functions. The original functions from v1.0.1 have been included in the re-write to maintain backward compatibility, but they will be deprecated in a future release.

Getting started

The package is very simple to use and ll vector operations are done using the Vector class, which will be detailed here.

Installation

npm i vector-math

Usage

The packace provides the following vector properties and operations:

  • Modulus (class property)
  • Unit vector (class property)
  • Value (vector in object form {i, j, k} as a class property)
  • Addition (method)
  • Subtraction (method)
  • Dot product (method)
  • Cross product (method)

Creating vectors

import { Vector } from "vector-path";

// Create vectors
const u = new Vector(1, 2, -3);
const v = new Vector(-4, 1);
const w = new Vector(1);

The above code will create the following vectors: $\mathbf{\bar{u}}=\mathbf{i}\mathbf{+}\mathbf{2j}\mathbf{-}\mathbf{3k}$ $\mathbf{\bar{v}}=-\mathbf{4i}\mathbf{+}\mathbf{j}$ $\mathbf{\bar{w}}=\mathbf{i}$ Initializing a vector with no values will creat a null vector

const q = new Vector();

Will create a null vector: $\mathbf{\bar{q}}=\mathbf{0i}\mathbf{+}\mathbf{0j}\mathbf{+}\mathbf{0k}$

Vector properties

// VECTORS

// u = i + 2j -3k
// v = -4i + j
// w = i

// MODULUS
let u_modulus = u.modulus; // modulus of u = 3.741657386773941
let v_modulus = v.modulus; // modulus of v = 4.123105625617661
let w_modulus = w.modulus; // modulus of w = 1

// UNIT VECTOR
let u_unitVector = u.unitVector; // unit vector as obj {i, j, k}
let v_unitVector = v.unitVector; // unit vector as obj {i, j, k}
let w_unitVector = w.unitVector; // unit vector as obj {i, j, k}

// VALUE
let u_value = u.value; // vector as obj {i, j, k}
let v_value = v.value; // vector as obj {i, j, k}
let w_value = w.value; // vector as obj {i, j, k}

// COMPONENTS
let u_i = u.i; // i component of vector u
let v_j = v.j; // j component of vector v
let w_k = w.k; // k component of vector w

Vector operations

// VECTORS

// u = i + 2j -3k
// v = -4i + j
// w = i

// ADDITION
let sum_uv = u.Add(v); // u + v (result returned as Vector class)
let sum_uvw = u.Add(v, w); // u + v + w (result returned as Vector class)

// SUBTRACTION
let diff_uv = u.Subtract(v); // u - v (result returned as Vector class)
let diff_uvw = u.Subtract(v, w); // u - v - w (result returned as Vector class)

// DOT PRODUCT
let u_dot_v = u.Dot(v); // u (dot) v (result returned as number)

// CROSS PRODUCT
let u_cross_v = u.Cross(v); // u x v (result returned as Vector class)

Examples

Given vectors $\mathbf{\bar{u}}=\mathbf{i}\mathbf{+}\mathbf{2j}\mathbf{-}\mathbf{3k}$ $\mathbf{\bar{v}}=-\mathbf{4i}\mathbf{+}\mathbf{j}$ $\mathbf{\bar{w}}=\mathbf{i}$

Cross product

$\mathbf{\bar{a}}=\mathbf{\bar{u}}\times\mathbf{\bar{v}}$

const u = new Vector(1, 2, -3);
const v = new Vector(-4, 1);

const a = u.Cross(v);
// a = i + 12j + 9k
// a.value = { i: 3, j: 12, k: 9 }

$\mathbf{\bar{a}}=\mathbf{3i}\mathbf{+}\mathbf{12j}\mathbf{+}\mathbf{9k}$

Combining operations

The library supports chaining of vector operations, so complex calculations can easily be expressed.

$\mathbf{b}=[(\mathbf{\bar{v}}\times\mathbf{\bar{u}})\mathbf{+}(\mathbf{\bar{w}}\times\mathbf{\bar{u}})]\bullet\mathbf{\bar{v}}$

const u = new Vector(1, 2, -3);
const v = new Vector(-4, 1);
const w = new Vector(1);

const b = v.Cross(u).Add(w.Cross(u)).Dot(v);
// b = 0

$\mathbf{b}=\mathbf{0}$

Feature roadmap

The following vector operations will be added in future releases

  • [x] ~~Modulus~~
  • [x] ~~Unit vector~~
  • [x] ~~Addition~~
  • [x] ~~Subtraction~~
  • [x] ~~Dot product~~
  • [x] ~~Cross product~~
  • [x] ~~Scalar Triple product~~
  • [ ] Angle between vectors
  • [ ] Scalar projection
  • [ ] Vector projection
  • [ ] Scalar multiplication
  • [ ] Basis
  • [ ] Check linear independence