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

vecti

v3.0.8

Published

A tiny TypeScript library for 2D vector math.

Downloads

9,536

Readme

Features

  • 🧮 Addition, subtraction, multiplication and division
  • ✨ Dot, cross and Hadamard product
  • 📏 Length and normalization
  • 📐 Rotation by radians and degrees
  • 🪨 Immutable data structure encourages chaining
  • 💾 Tiny and typed

Projects using Vecti

Installation

# yarn
$ yarn add vecti

# npm
$ npm install vecti

Usage

Vectors have two properties, x and y, representing their components. Since vectors are entirely immutable, they are read-only.

To use Vecti, add the following import to your TypeScript file. Instances of the Vector class can be created either by using its constructor or the static method of the class. The latter accepts a number array of length 2, with the first element being the x-axis component and the second element being the y-axis component.

import { Vector } from 'vecti'

// eslint-disable-next-line no-new
new Vector(42, 7) // == Vector { x: 42, y: 7 }

Vector.of([42, 7]) // == Vector { x: 42, y: 7 }

Addition

Two vectors can be added using the add method.

const a = new Vector(0, 1)
const b = new Vector(1, 0)

a.add(b) // == Vector { x: 1, y: 1 }

Subtraction

Two vectors can be subtracted using the subtract method. The parameter is the subtrahend and the instance is the minuend.

const a = new Vector(2, 1)
const b = new Vector(1, 0)

a.subtract(b) // == Vector { x: 1, y: 0 }

Multiplication

Vectors can be multiplied by scalars using the multiply method.

const a = new Vector(1, 0)

a.multiply(2) // == Vector { x: 2, y: 0 }

Division

Vectors can be divided by scalars using the divide method. The parameter is the divisor and the instance is the dividend.

const a = new Vector(4, 2)

a.divide(2) // == Vector { x: 2, y: 1 }

Dot product

The dot product of two vectors can be calculated using the dot method.

const a = new Vector(2, 3)
const b = new Vector(1, 3)

a.dot(b) // == 11

Cross product

The cross product of two vectors can be calculated using the cross method. The cross product of two vectors a and b is defined as a.x * b.y - a.y * b.x.

const a = new Vector(2, 1)
const b = new Vector(1, 3)

a.cross(b) // == 5

Hadamard product

The Hadamard product of two vectors can be calculated using the hadamard method.

const a = new Vector(2, 1)
const b = new Vector(1, 3)

a.hadamard(b) // == Vector { x: 2, y: 3 }

Length

The length of a vector can be calculated using the length method.

Length is defined as the L2 norm.

const a = new Vector(1, 0)

a.length() // == 1

const b = new Vector(-3, 4)

b.length() // == 5

Normalization

A normalized version of a vector can be calculated using the normalize method. The resulting vector will have a length of 1.

const a = new Vector(2, 0)

a.length() // == 2

const b = a.normalize() // == Vector { x: 1, y: 0 }

b.length() // == 1

Rotation

Vectors can be rotated by radians or degrees using the methods rotateByRadians and rotateByDegrees respectively.

Due to the rotation using Math.sin and Math.cos, rounding errors can occur. Notice that in the example below, the resulting x-component is 6.123233995736766e-17 and not 0 as expected.

const a = new Vector(1, 0)

a.rotateByDegrees(90) // == Vector { x: 6.123233995736766e-17, y: 1 }

a.rotateByRadians(Math.PI / 2) // == Vector { x: 6.123233995736766e-17, y: 1 }

Chaining

Vecti encourages chaining methods to achieve readable and concise calculations.

import { Vector } from 'vecti'

new Vector(-5, 0)
  .normalize()
  .rotateByDegrees(180)
  .add(Vector.of([0, 1]))
  .multiply(42) // == Vector { x: 42, y: 41.99999999999999 }

Development

# install dependencies
$ pnpm install

# build for production
$ pnpm build

# lint project files
$ pnpm lint

# run tests
$ pnpm test

License

MIT - Copyright © Jan Müller