@utilityjs/vector
v2.0.0
Published
An implementation of a 2D/3D Vector.
Downloads
14
Maintainers
Readme
UtilityJS | Vector
A 2D/3D vector implementation with mathematical operations.
Features
- 2D and 3D Support: Works with both 2D and 3D vectors
- Mathematical Operations: Add, subtract, multiply, dot product, cross product
- Vector Utilities: Normalize, reflect, lerp, distance, angle calculation
- Method Chaining: Fluent API for composing operations
- Static Methods: Class-level operations for functional style
Installation
npm install @utilityjs/vectoror
pnpm add @utilityjs/vectorUsage
Creating Vectors
import { Vector } from "@utilityjs/vector";
const v2d = new Vector(3, 4); // 2D vector
const v3d = new Vector(1, 2, 3); // 3D vectorBasic Operations
const v1 = new Vector(1, 2);
const v2 = new Vector(3, 4);
v1.add(v2); // v1 is now (4, 6)
v1.subtract(v2); // v1 is now (1, 2)
v1.multiply(2); // v1 is now (2, 4)Vector Math
const v1 = new Vector(3, 4);
v1.magnitude(); // 5
v1.normalize(); // unit vector
v1.dotProduct(new Vector(1, 0)); // dot product
const v2 = new Vector(1, 0, 0);
const v3 = new Vector(0, 1, 0);
v2.crossProduct(v3); // (0, 0, 1)Static Methods
const v1 = new Vector(1, 2);
const v2 = new Vector(3, 4);
Vector.distance(v1, v2);
Vector.lerp(v1, v2, 0.5);
Vector.fromAngle(Math.PI / 4);
Vector.fromArray([1, 2, 3]);API
Vector
Constructor
new Vector(x: number, y: number)- Creates a 2D vectornew Vector(x: number, y: number, z: number)- Creates a 3D vector
Instance Methods
getX(): number/setX(x): Vector- X component accessorgetY(): number/setY(y): Vector- Y component accessorgetZ(): number/setZ(z): Vector- Z component accessorsetAxes(x, y, z?): Vector- Set all componentsadd(vector): Vector- Add another vectorsubtract(vector): Vector- Subtract another vectormultiply(vectorOrScalar): Vector- Multiply by vector or scalardotProduct(vector): number- Calculate dot productcrossProduct(vector): Vector- Calculate cross productdistance(vector): number- Calculate distance to another vectorangleBetween(vector): number- Calculate angle in radianslerp(vector, t): Vector- Linear interpolationnormalize(): Vector- Normalize to unit lengthreflect(surfaceNormal): Vector- Reflect across surfacereverse(): Vector- Reverse directionsetMagnitude(magnitude): Vector- Set magnitude preserving directionmagnitude(): number- Get vector lengthsquaredMagnitude(): number- Get squared length (efficient)equalsTo(vector): boolean- Check equalityclone(): Vector- Create a copytoString(): string- String representationtoArray(): [number, number, number]- Convert to arraytoObject(): { x, y, z }- Convert to object
Static Methods
Vector.add(v1, v2): Vector- Add two vectorsVector.subtract(v1, v2): Vector- Subtract vectorsVector.multiply(v1, v2OrScalar): Vector- Multiply vectorsVector.dotProduce(v1, v2): number- Dot productVector.crossProduct(v1, v2): Vector- Cross productVector.distance(v1, v2): number- Distance between vectorsVector.lerp(v1, v2, t): Vector- Linear interpolationVector.fromAngle(angle, magnitude?): Vector- Create from angleVector.fromArray(array): Vector- Create from array
Contributing
Read the contributing guide to learn about our development process, how to propose bug fixes and improvements, and how to build and test your changes.
License
This project is licensed under the terms of the MIT license.
