All Projects → DerYeger → vecti

DerYeger / vecti

Licence: MIT License
A tiny TypeScript library for 2D vector math.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to vecti

vector2d
2D Vector Library. Operates using Objects, Array or Float32Array types to allow flexible performance.
Stars: ✭ 28 (+100%)
Mutual labels:  math, vector, 2d
Mathnet Spatial
Math.NET Spatial
Stars: ✭ 246 (+1657.14%)
Mutual labels:  math, 2d
Black
World's fastest HTML5 2D game engine   🛸
Stars: ✭ 174 (+1142.86%)
Mutual labels:  math, 2d
SCNMathExtensions
Math extensions for SCNVector3, SCNQuaternion, SCNMatrix4
Stars: ✭ 32 (+128.57%)
Mutual labels:  math, vector
Hlslpp
Math library using hlsl syntax with SSE/NEON support
Stars: ✭ 153 (+992.86%)
Mutual labels:  math, vector
Math Php
Powerful modern math library for PHP: Features descriptive statistics and regressions; Continuous and discrete probability distributions; Linear algebra with matrices and vectors, Numerical analysis; special mathematical functions; Algebra
Stars: ✭ 2,009 (+14250%)
Mutual labels:  math, vector
matrixgl
Yet another matrix library for WebGL
Stars: ✭ 25 (+78.57%)
Mutual labels:  math, vector
Sophus
C++ implementation of Lie Groups using Eigen.
Stars: ✭ 1,048 (+7385.71%)
Mutual labels:  math, 2d
Mathematics for Machine Learning
Learn mathematics behind machine learning and explore different mathematics in machine learning.
Stars: ✭ 28 (+100%)
Mutual labels:  math, vector
Tensor
A library and extension that provides objects for scientific computing in PHP.
Stars: ✭ 146 (+942.86%)
Mutual labels:  math, vector
vec-la-fp
↗️ A tiny (functional) 2d linear algebra library
Stars: ✭ 21 (+50%)
Mutual labels:  math, vector
Numphp
Mathematical PHP library for scientific computing
Stars: ✭ 120 (+757.14%)
Mutual labels:  math, vector
Algebra
means completeness and balancing, from the Arabic word الجبر
Stars: ✭ 92 (+557.14%)
Mutual labels:  math, vector
Unitymathreference
Math reference for games and more. All visualized in Unity3D.
Stars: ✭ 166 (+1085.71%)
Mutual labels:  math, 2d
Spatialmath Python
Create, manipulate and convert representations of position and orientation in 2D or 3D using Python
Stars: ✭ 78 (+457.14%)
Mutual labels:  math, 2d
footile
A 2D vector graphics library written in Rust
Stars: ✭ 32 (+128.57%)
Mutual labels:  vector, 2d
Coord Rs
[deprecated] A simple, ergonomic vector mathematics crate for Rust
Stars: ✭ 18 (+28.57%)
Mutual labels:  math, vector
Sharpmath
A small .NET math library.
Stars: ✭ 36 (+157.14%)
Mutual labels:  math, vector
vector-math
Shader-math in haxe: library for GLSL vector operations, complete with swizzles and all
Stars: ✭ 30 (+114.29%)
Mutual labels:  math, vector
FixedPoint-Sharp
Fixed point math with 48.16 precision (based on lib by https://github.com/fholm)
Stars: ✭ 114 (+714.29%)
Mutual labels:  math, 2d

Logo

Vecti

A tiny TypeScript library for 2D vector math.

Documentation

CI NPM Coverage LGTM Grade MIT npm bundle size

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.

import { Vector } from 'vecti'

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.

const a = new Vector(42, 7)
console.log(a) // == Vector { x: 42, y: 7 }

const b = Vector.of([42, 7])
console.log(b) // == 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)

const c = a.add(b)

console.log(c) // == 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)

const c = a.subtract(b)

console.log(c) // == Vector { x: 1, y: 0 }

Multiplication

Vectors can be multiplied by scalars using the multiply method.

const a = new Vector(1, 0)

const b = a.multiply(2)

console.log(b) // == 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)

const b = a.divide(2)

console.log(b) // == 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)

const c = a.dot(b)

console.log(c) // == 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)

const c = a.cross(b)

console.log(c) // == 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)

const c = a.hadamard(b)

console.log(c) // == 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)

console.log(a.length()) // == 1

const b = new Vector(-3, 4)

console.log(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)

console.log(a.length()) // == 2

const b = a.normalize()

console.log(b) // == Vector { x: 1, y: 0 }
console.log(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)

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


console.log(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'

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

Development

# install dependencies
$ yarn install

# build for production
$ yarn build

# lint project files
$ yarn lint

# run tests
$ yarn test

License

MIT - Copyright © Jan Müller

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].