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

@arekrado/vector-2d

v0.0.12

Published

Pure functions to manipulate 2d vectors

Downloads

6

Readme

vector-2d - Demo

Pure functions to manipulate 2d vectors

cover

npm i @arekrado/vector-2d
type Vector2D = [number, number]
  • vector
type Vector = (x: number, y: number) => Vector2D
vector(5, 10) // [5, 10]
  • vectorZero
type VectorZero = () => Vector2D
vectorZero() // [0, 0]
  • vectorUp
type VectorZero = () => Vector2D
vectorUp() // [0, 1]
  • vectorRight
type VectorZero = () => Vector2D
vectorRight() // [1, 0]
  • vectorDown
type VectorZero = () => Vector2D
vectorDown() // [0, -1]
  • vectorLeft
type VectorZero = () => Vector2D
vectorLeft() // [-1, 0]
  • vectorOne
type VectorZero = () => Vector2D
vectorOne() // [1, 1]
  • add
type Add = (v1: Vector2D, v2: Vector2D) => Vector2D
add(vector(2, 2), vector(2, 2)) // [4, 4]
add(vector(-5, 10), vector(3, -3)) // [2, 7]
  • sub
type Sub = (v1: Vector2D, v2: Vector2D) => Vector2D
sub(vector(2, 2), vector(2, 2)) // [0, 0]
sub(vector(-5, 10), vector(3, -3)) // [8, 13]
  • divide
type Sub = (v1: Vector2D, v2: Vector2D) => Vector2D
divide(vector(2, 2), vector(2, 2)) // [1, 1]
divide(vector(10, 12), vector(2, 2)) // [5, 6]
  • multiply
type Sub = (v1: Vector2D, v2: Vector2D) => Vector2D
multiply(vector(2, 2), vector(2, 2)) // [4, 4]
multiply(vector(5, 10), vector(3, -3)) // [15, -30]
  • scale - multiple vector by scalar (number)
type Scale = (scalar: number, v: Vector2D) => Vector2D
scale(0, vector(1, 1)) // [0, 0]
scale(1, vector(1, 1)) // [1, 1]
scale(10, vector(0.3, 0.4)) //  [3, 4]
  • magnitude - returns vector length
type Magnitude = (v: Vector2D) => number
magnitude(vectorZero()) // 0
magnitude(vector(3, 4)) // 5
magnitude(vector(-3, -4)) // 5
magnitude(vector(1, 1)) // 1.4142135623730951
magnitude(vector(2, 2)) // 2.8284271247461903
  • distance - returns distance between two vectors
type Distance = (v1: Vector2D, v2: Vector2D) => number
distance(vectorZero(), vectorZero()) // 0
distance(vector(3, 4), vector(3, 4)) // 0
distance(vector(1, 1), vector(2, 1)) // 1
distance(vector(-1, -1), vector(-2, -1)) // 1
  • clamp - set vector length
type Clamp = (v: Vector2D, vMagnitude: number) => Vector2D
clamp(vector(3, 4), 5) // [3, 4]
clamp(vector(3, 4), 3) // [1.7999999999999998, 2.4]
  • isEqual - compares two vectors
type IsEqual = (v1: Vector2D, v2: Vector2D) => boolean
isEqual(vector(1, 1), vector(1, 1)) // true
isEqual(vector(1, 1), vector(2, 1)) // false
  • dot - https://en.wikipedia.org/wiki/Dot_product - "how parallel the vectors are to each other"
type Dot = (v1: Vector2D, v2: Vector2D) => number
dot(vectorZero(), vectorZero()) // 0
dot(vector(1, 1), vector(1, 1)) // 2
dot(vectorUp(), vectorDown()) // -1
dot(vectorUp(), vectorLeft()) // 0
dot(vectorDown(), vectorRight()) // 0
  • angle - calculates angle between two vectors and returns result in radians
type Angle = (v1: Vector2D, v2: Vector2D) => number
angle(vectorZero(), vectorZero()) // 1.5707963267948966
angle(vector(1, 1), vector(1, 1)) // 2.1073424255447017e-8
angle(vectorUp(), vectorDown()) // 3.141592653589793
angle(vectorUp(), vectorLeft()) // 1.5707963267948966
angle(vectorDown(), vectorRight()) // 1.5707963267948966
  • angleDeg - calculates angle between two vectors and returns result in degrees (360)
type AngleDeg = (v1: Vector2D, v2: Vector2D) => number
angleDeg(vectorZero(), vectorZero()) // 90
angleDeg(vector(1, 1), vector(1, 1)) // 0.0000012074182697257333
angleDeg(vectorUp(), vectorDown()) // 180
angleDeg(vectorUp(), vectorLeft()) // 90
angleDeg(vectorDown(), vectorRight()) // 90
  • limit - clamp vector length between two values if exceeds them
type Limit = (v: Vector2D, min: number, max: number) => Vector2D
limit(vectorZero(), 0, 0) // [3, 4]
limit(vector(3, 4), 0, 2) /// [0, 0]
limit(vector(3, 4), 0, 5) // [1.2, 1.6]
limit(vector(-3, -4), 0, 2) // [-1.2, -1.6]
  • normalize - returns the same vector but with length equals 1
type Normalize = (v: Vector2D) => Vector2D
normalize(vectorZero()) // [0, 0]
normalize(vector(1, 1))) // [0.7071067811865475, 0.7071067811865475]
normalize(vector(1, 0)) // [1, 0]