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

@coderosh/vector

v2.0.0

Published

2D and 3D Vector classes for game development and physics simulations

Downloads

107

Readme

vector

2D and 3D Vector classes for game development and physics simulations.

Installation

Install via npm

npm install @coderosh/vector

Or via yarn

yarn add @coderosh/vector

Usage

import { Vector2D, Vector3D } from '@coderosh/vector'

/* 3D Vector */
new Vector3D(1, 4, 1)

/* 2D Vector */
new Vector2D(1, 3)

Available methods

Documentation

add

Adds two vectors

const vec3d = new Vector3D(1, 2, 3)
vec3d.add(4, 5, 6)
vec3d.add(new Vector3D(4, 5, 6))

const vec2d = new Vector2D(1, 2)
vec2d.add(4, 5)
vec2d.add(new Vector2D(4, 5))

sub

Substracts two vectors

const vec3d = new Vector3D(1, 2, 3)
vec3d.sub(4, 5, 6)
vec3d.sub(new Vector3D(4, 5, 6))

const vec2d = new Vector2D(1, 2)
vec2d.sub(4, 5)
vec2d.sub(new Vector2D(4, 5))

scale

Multiply a vector by a scalar factor

const vec3d = new Vector3D(1, 2, 3)
vec3d.scale(2)

const vec2d = new Vector2D(1, 2)
vec2d.scale(2)

lenSqr

Square of length of vector

const vec3d = new Vector3D(1, 2, 3)
vec3d.lenSqr()

const vec2d = new Vector2D(1, 2)
vec2d.lenSqr()

len

Length of vector

const vec3d = new Vector3D(1, 2, 3)
vec3d.len()

const vec2d = new Vector2D(1, 2)
vec2d.len()

normalize

Normalize the vector

const vec3d = new Vector3D(1, 2, 3)
vec3d.normalize()

const vec2d = new Vector2D(1, 2)
vec2d.normalize()

dist

Calculate distance between two vectors

const vec3d = new Vector3D(1, 2, 3)
vec3d.dist(5, 2, 3)
vec3d.dist(new Vector3D(5, 2, 3))

const vec2d = new Vector2D(1, 2)
vec2d.dist(5, 2)
vec2d.dist(new Vector2D(5, 2))

equals

Check if two vectors have same coordinates

const vec3d = new Vector3D(1, 2, 3)
vec3d.equals(1, 2, 3)
vec3d.equals(new Vector3D(1, 2, 3))

const vec2d = new Vector2D(1, 2)
vec2d.equals(1, 2)
vec2d.equals(new Vector2D(1, 2))

dot

Returns dot product of two vectors

const vec3d = new Vector3D(1, 2, 3)
vec3d.dot(1, 1, 2)
vec3d.dot(new Vector3D(1, 1, 2))

const vec2d = new Vector2D(1, 2)
vec2d.dot(4, 2)
vec2d.dot(new Vector2D(4, 2))

cross

Returns cross product of two vectors

const vec = new Vector3D(1, 2, 3)
vec.cross(1, 2, 4)
vec.cross(new Vector3D(1, 2, 4))

lerp

Linear interpolate vectors

const vec3d = new Vector3D(1, 2, 3)
vec3d.lerp(1, 1, 2, 0.1)
vec3d.lerp(new Vector3D(1, 1, 2), 0.1)

const vec2d = new Vector2D(1, 2)
vec2d.lerp(4, 2, 0.1)
vec2d.lerp(new Vector2D(4, 2), 0.1)

slerp

Spherical linear interpolate vectors

const vec = new Vector3D(1, 0, 0)
vec.slerp(new Vector3D(0, 1, 0), 0.5)
vec.slerp(0, 1, 0, 0.5)

nlerp

Linear interpolate vectors and normalize

const vec3d = new Vector3D(1, 2, 3)
vec3d.nlerp(1, 1, 2, 0.1)
vec3d.nlerp(new Vector3D(1, 1, 2), 0.1)

const vec2d = new Vector2D(1, 2)
vec2d.nlerp(4, 2, 0.1)
vec2d.nlerp(new Vector2D(4, 2), 0.1)

copy

const vec3d = new Vector3D(1, 2, 3)
const newVec3d = vec3d.copy()
const newVec3d_ = new Vector(vec3d)

const vec2d = new Vector3D(1, 2, 3)
const newVec2d = vec2d.copy()
const newVec2d_ = new Vector(vec2d)

LICENSE

MIT