Skip to content

A lightweight vector utility library that provides commonly used vector operations for JavaScript and TypeScript applications.

License

Notifications You must be signed in to change notification settings

feldhaus/vector

Repository files navigation

@feldhaus/vector

A lightweight vector 2D utility library that provides commonly used vector operations for JavaScript and TypeScript applications.

Installation

You can install the package via npm:

npm install @feldhaus/vector

Available Functions

  • add: Adds two vectors. Source
  • sub: Subtracts the second vector from the first. Source
  • mult: Multiplies a vector by a scalar. Source
  • div: Divides a vector by a scalar. Source
  • mag: Calculates the magnitude of a vector. Source
  • magSqr: Calculates the squared magnitude of a vector. Source
  • angleBetween: Calculates the angle between two vectors. Source
  • distanceBetween: Calculates the distance between two vectors. Source
  • normalize: Normalizes a vector to have a magnitude of 1. Source
  • dot: Calculates the dot product of two vectors. Source
  • cross: Calculates the cross product of two vectors. Source
  • lerp: Linearly interpolates between two vectors. Source

Available Constants

  • VECTOR_ZERO: A constant representing the zero vector { x: 0, y: 0 }.
  • VECTOR_UP: A constant representing the unit vector pointing up { x: 0, y: 1 }.
  • VECTOR_DOWN: A constant representing the unit vector pointing down { x: 0, y: -1 }.
  • VECTOR_LEFT: A constant representing the unit vector pointing left { x: -1, y: 0 }.
  • VECTOR_RIGHT: A constant representing the unit vector pointing right { x: 1, y: 0 }.

Examples

import { add, sub, mult, div } from '@feldhaus/vector';

const vectorA = { x: 1, y: 2 };
const vectorB = { x: 3, y: 4 };

const addedVector = add(vectorA, vectorB); // Output: { x: 4, y: 6 }
const subtractedVector = sub(vectorA, vectorB); // Output: { x: -2, y: -2 }
const multipliedVector = mult(vectorA, 2); // Output: { x: 2, y: 4 }
const dividedVector = div(vectorA, 2); // Output: { x: 0.5, y: 1 }
import { mag, magSqr } from '@feldhaus/vector';

const vectorA = { x: 1, y: 2 };
const vectorB = { x: 3, y: 4 };

const magnitude = mag(vectorA); // Output: 2.23606797749979
const magnitudeSquared = magSqr(vectorA); // Output: 5
import { angleBetween, distanceBetween, fromAngle } from '@feldhaus/vector';

const vectorA = { x: 1, y: 0 };
const vectorB = { x: 0, y: 1 };

const angle = angleBetween(vectorA, vectorB); // Output: 1.5707963267948966 (which is π/2 radians or 90 degrees)
const distance = distanceBetween(vectorA, vectorB); // Output: : 1.41
const vector = fromAngle(Math.PI); // Output: { x: -1, y: 0 }
import { dot, cross } from '@feldhaus/vector';

const vectorA = { x: 1, y: 2 };
const vectorB = { x: 3, y: 4 };

const dotProduct = dot(vectorA, vectorB); // Output: 11
const crossProduct = dot(vectorA, vectorB); // Output: -2
import { lerp } from '@feldhaus/vector';

const vectorA = { x: 0, y: 0 };
const vectorB = { x: 10, y: 10 };

const interpolateVector = lerp(vectorA, vectorB, 0.5); // Output: { x: 5, y: 5 }

Usage in Browser

You can also include the library directly in your HTML file using a UMD build:

<script src="https://unpkg.com/@feldhaus/vector/dist/index.umd.js"></script>

This will expose the library as FVector on the global window object, allowing you to use the functions like this:

<script>
  const vectorA = { x: 1, y: 2 };
  const vectorB = { x: 3, y: 4 };
  const addedVector = FVector.add(vectorA, vectorB);
  console.log(addedVector); // Output: { x: 4, y: 6 }
</script>

License

MIT

About

A lightweight vector utility library that provides commonly used vector operations for JavaScript and TypeScript applications.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published