@fimbul-works/vec
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

@fimbul-works/vec

A high-performance TypeScript vector math library providing 2D, 3D, and 4D vector operations with a focus on performance and type safety.

npm version TypeScript

Features

  • 🚀 Optimized Performance: Uses Float64Array with magnitude caching and private fields
  • 🛡️ Type Safety: Full TypeScript support with strict typing and readonly options
  • 📏 Multiple Distance Metrics: Euclidean, Manhattan, Chebyshev, and Minkowski
  • 🔒 Immutability Support: Both mutable and immutable operation modes
  • 🎮 Graphics Ready: Homogeneous coordinates and transformation support
  • 🧮 Math Features: Comprehensive geometric and arithmetic operations
  • Memory Efficient: Zero-allocation options for performance-critical code

Installation

npm install @fimbul-works/vec

Quick Start

import { Vec2, Vec3, Vec4 } from "@fimbul-works/vec";

// Create and manipulate vectors
const position = new Vec2(100, 200);
const direction = new Vec2(1, 0).rotate(Math.PI / 4);
const movement = Vec2.scale(direction, 5);

position.add(movement);

// 3D graphics with homogeneous coordinates
const point = new Vec4(x, y, z, 1);    // Point in 3D space
const vector = new Vec4(dx, dy, dz, 0); // Direction in 3D space

Zero-Allocation Usage

// Reuse vectors to avoid garbage collection
const result = new Vec2();
const temp = new Vec2();

// Chain operations without creating intermediates
position
  .add(velocity.scale(deltaTime, temp))
  .clamp(0, 100);

Core Operations

// Static operations (immutable)
const sum = Vec2.add(v1, v2);
const dot = Vec3.dot(v1, v2);
const cross = Vec3.cross(v1, v2);

// Method chaining (mutable)
const result = new Vec3(1, 0, 0)
  .rotate(angle)
  .scale(2)
  .normalize();

// Distance calculations
const dist = v1.distance(v2);
const manhattan = v1.distanceManhattan(v2);

Complete API Documentation

License

MIT License - See LICENSE file for details.


Built with ⚡ by FimbulWorks

Package Sidebar

Install

npm i @fimbul-works/vec

Weekly Downloads

6

Version

1.0.3

License

MIT

Unpacked Size

174 kB

Total Files

14

Last publish

Collaborators

  • fimbul.works