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

@minogin/vector

v1.0.2

Published

2D-vector algebra. Vectors are immutable - every operation returns new instance.

Downloads

331

Readme

// Calculate
let a = new Vector(10, 20)  
let b = new Vector(30, 40)
let c = a.add(b).rotate(Vector.rad(30)) 

// Use
console.log('x: ' + c.x)    
console.log('y: ' + c.y)
console.log('length: ' + c.norm())
console.log('angle in degrees: ' + Vector.deg(c.angle()))

Description

2D-vector operations: addition, subtraction, normalization, rotation etc.

The vectors are immutable and therefore safe to use. Every operation returns new instance.

Installation

Install with npm

npm i @minogin/vector

Add the following line to your javascript code

import Vector from '@minogin/vector'

Documentation

Constructors

Vector(x, y)

  • x, y {Number} - initial vector coordinates
let v = new Vector(10, -20)

Vector(v)

  • v {Vector} - vector to clone
let a = new Vector(10, 20)
let b = new Vector(a)       // b = { 10; 20 }

Operators and functions

add(v)

  • v {Vector} - vector to add
  • returns: Vector

Returns sum of current vector and v

let a = new Vector(10, 20)
let b = a.add(new Vector(30, 40))   // b = { 40; 60 }

sub(v)

  • v {Vector} - vector to subtract
  • returns: Vector

Returns difference of current vector and v

let a = new Vector(30, 40)
let b = a.sub(new Vector(10, 30))   // b = { 20; 10 }

neg(v)

  • returns: Vector

Returns vector negative (inverse) to current

let a = new Vector(10, -20)
let b = a.neg()     // a = {-10; 20}

mul(v)

  • v {Vector} - vector to multiply by
  • returns: Vector

Returns dot product of current vector and another

let a = new Vector(2, 3)
let p = a.mul(new Vector(4, 5))     // p = 2*4 + 3*5 = 23  

norm()

  • returns: Vector

Returns norm (length) of current vector

let n = new Vector(3, 4).norm()     // n = 5  

unit()

  • returns: Vector

Returns unit vector (length = 1) directed same as current

let u = new Vector(5, -5).unit()     // u = { √2/2; -√2/2 }

rotate(angle)

  • angle {Number} - angle in radians
  • returns: Vector

Returns current vector rotated by angle (radians)

let b = new Vector(1, 0).rotate(Math.PI / 6)     // b = { √3/2; 1/2 }

See also: Vector.rad(degrees)

angle()

  • returns: Number

Computes current vector's angle (to x axis) in radians

let phi = new Vector(Math.sqrt(3)/2, 1/2).angle()     // phi = Math.PI / 6

See also: Vector.deg(radians)

equals(v[, precision])

  • v {Vector} - vector to compare to
  • precision {Number} - precision of comparison, defaults to Number.EPSILON
  • returns: Boolean

Checks whether this vector is equal (with given precision) to another.

Be careful! Number.EPSILON may be too small for most floating point operations.

let v = new Vector(Math.sqrt(3)/2, 1/2).rotate(-Math.PI / 6) 
if (v.equals(new Vector(1, 0))) { /*...*/ }     // May be false!
if (v.equals(new Vector(1, 0), 1e-10)) { /*...*/ }     // It is true

clone()

  • returns: Vector

v.clone() is identical to new Vector(v)

Utilities

Vector.rad(degrees)

  • degrees {Number} - angle in degrees
  • returns: Number

Converts degrees to radians.

new Vector(10, 20).rotate(Vector.rad(30)) 

Vector.deg(radians)

  • radians {Number} - angle in radians
  • returns: Number

Converts radians to degrees.

let v = new Vector(Math.sqrt(3)/2, 1/2)
console.log('Vector is rotated by ' + Math.deg(v.angle()) + ' degrees')

License

ISC