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

rg.vec2

v0.2.0

Published

A 2d vector class + vector math utilities.

Downloads

12

Readme

vec2

npm version Travis CI Badge (master branch)

A 2d vector class + vector math utilities.

How to include

Node.js / Browserify

In your shell:

$ npm install --save rg.vec2

In your javascript:

var Vec2 = require('rg.vec2')

Browser ("classic" way)

Just add a script tag with either vec2.js or vec2.min.js from this repos root directory. This makes the Vec2 variable globally available.

typeof window.Vec2
// --> 'function'

API

new Vec2(x, y)

Creates a new Vec2 object with the properties x and y.

Warning: this does not provide any error handling, so be sure that x and y are numbers!

var a = new Vec2(3, 4)

console.log(a)
// --> { x: 3, y: 4 }

Vec2#magnitude()

returns number

Returns the magnitude of this, i.e. Math.sqrt(this.x * this.x + this.y * this.y).

var a = new Vec2(3, 4)

console.log(a.magnitude())
// --> 5

Vec2#angle()

returns Number

Returns the angle of the vector in Radians. For (1, 0), this returns zero, for (0, 1) half Pi and so on.

var a = new Vec2(1, 0)
var b = new Vec2(0, 1)
var c = new Vec2(-1, 1)

console.log(a.angle())
// --> 0

console.log(b.angle())
// --> 1.5707963267948966

console.log(c.angle())
// --> 2.356194490192345

Vec2#normal()

return Vec2

Returns a (new Vec2) Normal, i.e. a vector orthogonal to this. The returned vector points to left of the original vector.

The same result is returned by Vec2#leftNormal(). If you need a Normal pointing to the right, use Vec2#rightNormal()

var a = new Vec2(4, 5)

var n = a.normal()

console.log(n)
// --> { x: -5, y: 4 }

console.log(a.leftNormal())
// --> { x: -5, y: 4 }

console.log(a.rightNormal())
// --> { x: 5, y: -4 }

// if the dot product of two vectors is zero, they're orthogonal.
console.log(n.dot(a))
// --> 0

Vec2#add(vector)

takes Vec2

returns Vec2

Adds vector to this. Returns this (for chaining).

var a = new Vec2(10, 12)
var b = new Vec2(4, 5)

a.add(b)

console.log(a)
// --> { x: 14, y: 17 }

Vec2#subtract(vector)

takes Vec2

returns Vec2

Subtracts vector from this. Returns this (for chaining).

var a = new Vec2(5, 3)
var b = new Vec2(3, 2)

a.subtract(b)

console.log(a)
// --> { x: 2, y: 1 }

Vec2#scale(number)

takes Number

returns Vec2

Multiplies x and y with number. Returns this (for chaining).

var a = new Vec2(7, 6)

a.scale(6)

console.log(a)
// --> { a: 42, b: 36 }

Vec2#dot(vector)

takes Vec2

returns Number

Calculates the dot product of this and vector.

var a = new Vector(5, 3)
var b = new Vector(-2, 4)

// a.x * b.x + a.y * b.y
console.log(a.dot(b))
// --> 2

Vec2#parallel(vector)

takes Vec2

returns Boolean

Returns true if this and vector are parallel, else false.

var a = new Vec2(5, 2)
var b = new Vec2(10, 4)
var c = new Vec2(6, 7)

console.log(a.parallel(b))
// --> true

console.log(a.parallel(c))
// --> false

Vec2#normalize()

returns Vec2

Normalizes this. This means that the direction will stay the same, but the magnitude will be 1.

var a = new Vec2(4, 5)
var b = a.copy()

b.normalize()

console.log(b.parallel(a))
// --> true

console.log(b.getMagnitude())
// --> 1

Vec2#asArray()

returns Array

Returns an array in the form [x, y].

var a = new Vec2(3, 4)

console.log(a.asArray())
// --> [ 3, 4 ]

Vec2#copy()

returns Vec2

Returns a new instance of Vec2 with the same x and y as this.

var a = new Vec2(3, 4)
var b = a.copy()

console.log(b.x === a.x, b.y === a.y)
// --> true true

console.log(b === a)
// --> false

Functions

All math-performing methods of Vec2 that return this are also available as functions in the "namespace" Vec2.

var a = new Vec2(1, 2)
var b = new Vec2(3, 4)

var c = a.copy().add(b)

// equals

var c = Vec2.add(a, b)

As you can see from the example, the main difference between these two ways is that calling Vec2.<function> returns a copy of the vector instead of doing the operation in-place.

Function available as method and namespaced function

  • add
  • subtract
  • scale
  • normalize

Vec2.fromAngle(<angle>, <length>)

takes Number, Number

returns Vec2

Returns a new vector with the angle angle (in Radians) and the length length.

var a = Vec2.fromAngle(Math.PI, 2)
var b = Vec2.fromAngle(Math.PI * (3 / 2), 42)

console.log(a.angle(), a.magnitude())
// --> 3.141592653589793, 2

console.log(b.angle(), b.magnitude())
// --> 4.71238898038469, 42

Vec2.fromArray(<array>)

takes Array

returns Vec2

Creates a new instance of Vec2 with array[0] as x and array[1] as y.

var b = Vec2.fromArray([1, 2])

console.log(b)
// --> { x: 1, y: 2 }

Testing/Building

Installing development dependencies

$ npm install

Running tests

$ npm test

Building for the browser

$ npm run build
$ # for building on file change
$ npm run watch

License

MIT license, see LICENSE.