
Product
Introducing Rust Support in Socket
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
A tiny TypeScript library for 2D vector math.
# yarn
$ yarn add vecti
# npm
$ npm install vecti
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 }
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 }
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 }
Vectors can be multiplied by scalars using the multiply
method.
const a = new Vector(1, 0)
a.multiply(2) // == Vector { x: 2, y: 0 }
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 }
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
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
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 }
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
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
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 }
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 }
# install dependencies
$ pnpm install
# build for production
$ pnpm build
# lint project files
$ pnpm lint
# run tests
$ pnpm test
MIT - Copyright © Jan Müller
FAQs
A tiny TypeScript library for 2D vector math.
The npm package vecti receives a total of 3,260 weekly downloads. As such, vecti popularity was classified as popular.
We found that vecti demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.