3.0.8 • Published 2 months ago

vecti v3.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

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

3.0.8

2 months ago

3.0.7

3 months ago

3.0.6

3 months ago

3.0.5

4 months ago

3.0.4

4 months ago

3.0.3

4 months ago

3.0.2

5 months ago

2.2.1

10 months ago

2.2.0

10 months ago

3.0.1

9 months ago

3.0.0

9 months ago

2.1.40

1 year ago

2.1.27

2 years ago

2.1.28

2 years ago

2.1.25

2 years ago

2.1.26

2 years ago

2.1.23

2 years ago

2.1.24

2 years ago

2.1.21

2 years ago

2.1.22

2 years ago

2.1.29

2 years ago

2.1.38

1 year ago

2.1.39

1 year ago

2.1.36

2 years ago

2.1.37

1 year ago

2.1.34

2 years ago

2.1.35

2 years ago

2.1.32

2 years ago

2.1.33

2 years ago

2.1.30

2 years ago

2.1.31

2 years ago

2.1.9

2 years ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.14

2 years ago

2.1.15

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

2.1.18

2 years ago

2.1.19

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.8

2 years ago

2.1.7

2 years ago

2.1.20

2 years ago

2.0.28

2 years ago

2.0.29

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.0.31

2 years ago

2.0.32

2 years ago

2.0.30

2 years ago

2.1.0

2 years ago

2.0.19

2 years ago

2.0.18

2 years ago

2.0.26

2 years ago

2.0.27

2 years ago

2.0.24

2 years ago

2.0.25

2 years ago

2.0.22

2 years ago

2.0.23

2 years ago

2.0.20

2 years ago

2.0.21

2 years ago

2.0.17

2 years ago

2.0.15

2 years ago

2.0.16

2 years ago

2.0.13

2 years ago

2.0.14

2 years ago

2.0.11

2 years ago

2.0.12

2 years ago

2.0.9

2 years ago

2.0.10

2 years ago

2.0.8

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago