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

arrow2d

v1.0.1

Published

Working with 2D vectors made easy!

Downloads

4

Readme

MIT License Made by Node NPM

Instalation

Arrow2D is a Node.js module, to install it first download and install Node.js.

Having Node.js and npm installed, simply run:

npm i arrow2d

Quick start

const { CreateVector, StaticArrow2D } = require('Arrow2D');

//Create a VectorForm vector.
const vector1 = CreateVector(5, 2);
const vector2 = CreateVector(2, 5);
const vector3 = StaticArrow2D.add(vector1, vector2);

//Create an ArrayForm vector.
const arrayVector = CreateVector(5, 2, true)
const arrayVector2 = StaticArrow2D.add(vector1, [2, 5]);

Operations

normalize()

Normalizes the vector, turning it into a unit vector.

Example

const vector = CreateVector(5, 2);
vector.normalize(); // Normalize the vector. 

Normalize operation;

add(vectorToAdd)

Adds another vector components into the current vector.

Example

const vector1 = CreateVector(5, 2);
const vector2 = CreateVector(2, 5);
vector1.add(vector2); // Adds vector2 to vector1

Addition operation;

sub(vectorToSubtract)

Subtracts another vector components from the current vector.

Example

const vector1 = CreateVector(5, 2);
const vector2 = CreateVector(2, 5);
vector1.sub(vector2); // Subtracts vector2 from vector1

Subtract operation;

scalarMult(scalar)

Multiplies the current vector components with a scalar.

Example

const vector = CreateVector(5, 2);
const scalar = 2;
vector.scalarMult(scalar); // Multiply vector by scalar.

scalarDiv(scalar)

Divides the current vector components with a scalar.

Example

const vector = CreateVector(5, 2);
const scalar = 2;
vector.scalarDiv(scalar); // Divide vector by scalar.

Scalar division operation;

add(firstVector, secondVector) - STATIC

Static version of add.

Add the components of two vectors and returns a new one.

Example

const vector1 = CreateVector(5, 2);
const vector2 = CreateVectorr(2, 5);
const vector3 = StaticArrow2D.add(vector1, vector2); // Add vectors and return a new one.

Static addition operation;

sub(firstVector, secondVector) - STATIC

Static version of sub.

Subtracts the components of two vectors and return a new one.

Example

const vector1 = CreateVector(5, 2);
const vector2 = CreateVector(2, 5);
const vector3 = StaticArrow2D.sub(vector1, vector2); // Subtract vectors and return a new one.

Static subtraction operation;

dotProduct(firstVector, secondVector) - STATIC

Returns a scalar value from the dot product of two vectors.

Example

const vector1 = CreateVector(5, 2);
const vector2 = CreateVector(2, 5);
const result = StaticArrow2D.dotProduct(vector1, vector2); // Returns a scalar.

scalarDiv(vector, scalar) - STATIC

Static version of scalarDiv.

Divides a vector by a scalar and returns a new one.

Example

const vector1 = CreateVector(5, 2);
const scalar = 2;
const vector3 = StaticArrow2D.scalarDiv(vector1, scalar); // Returns a new vector divided by the scalar.

Utils

constrainMagnitude()

Constrain the magnitude of the vector to a value.

Example

const vector = CreateVector(5, 2);
vector.constrainMagnitude(3); // Constrains the magnitude of the vector to 3.

heading()

Returns the direction the vector is pointing.

Example

const vector1 = CreateVector(5, 2);
const direction = vector1.heading();

rotate(angle)

Rotate the vector by an angle.

Example

const vector = CreateVector(5, 2);
vector.rotate(PI/2); // Rotate vector by 90 degrees.

vectorToArray(vector) - STATIC

Returns the array form of a vector. The operations that take a vector as an input will receive an array instead.

Example

const vector = CreateVector(5, 2);
const array = StaticArrow2D.vectorToArray(vector);

arrayToVector(array) - STATIC

Returns the vector form of an array.

Example

const array = [5, 2];
const vector = StaticArrow2D.arrayToVector(array);

angleBetween(firstVector, secondVector, degrees) - STATIC

Returns the angle, in radians, between two vectors. If you want the angle in degress, set "degress" to true.

Example

const vector1 = CreateVector(5, 2);
const vector2 = CreateVector(2, 5);
const angle = StaticArrow2D.angleBetween(vector1, vector2);

orthogonalVector(vector) - STATIC

Returns an orthogonal vector to the input vector.

Example

const vector = CreateVector(5, 2);
const orthogonal = StaticArrow2D.orthogonalVector(vector);