@haydn/linear-fns
v0.0.4
Published
<div align="center"> <h1> <img src="https://unpkg.com/@haydn/linear-fns/logo.png" alt="linear-fns" width="160" /> </h1> <p>A JavaScript utility library for linear algebra.</p> <p> <img alt="npm bundle size" src="https://img.shields.io/bund
Downloads
13
Readme
Features
- Lightweight.
- Pure functions.
- TypeScript declarations included.
- ESM package.
Installation
bun add @haydn/linear-fns
deno add npm:@haydn/linear-fns
npm install @haydn/linear-fns
pnpm add @haydn/linear-fns
yarn add @haydn/linear-fnsUsage
import { addVec2, lengthVec2, normalizeVec2 } from "@haydn/linear-fns";
addVec2([1, 2], [3, 4]);
//=> [4, 6]
lengthVec2([3, 4]);
//=> 5
normalizeVec2([3, 4]);
//=> [0.6, 0.8]:toolbox: Functions
- absVec2
- addVec2
- clampVec2
- crossVec2
- dotVec2
- divideVec2
- isEqualVec2
- floorVec2
- lengthVec2
- maxVec2
- minVec2
- modVec2
- multiplyVec2
- normalizeVec2
- subtractVec2
- vec2
:gear: absVec2
Calculates the absolute value of a two-dimensional vector.
| Function | Type |
| ---------- | ---------- |
| absVec2 | (v: Vec2) => Vec2 |
Parameters:
v: The vector to take the absolute value of.
Returns:
The absolute value of the vector.
Examples:
absVec2([1, -2]);
//=> [1, 2]:gear: addVec2
Sums two two-dimensional vectors.
| Function | Type |
| ---------- | ---------- |
| addVec2 | (v1: Vec2, v2: Vec2) => Vec2 |
Parameters:
v1: The first vector.v2: The second vector.
Returns:
The sum of the two vectors.
Examples:
addVec2([1, 2], [3, 4]);
//=> [4, 6]:gear: clampVec2
Clamps a two-dimensional vector between a minimum and maximum vector.
| Function | Type |
| ---------- | ---------- |
| clampVec2 | (v: Vec2, min: Vec2, max: Vec2) => Vec2 |
Parameters:
v: The vector to clamp.min: The minimum vector.max: The maximum vector.
Returns:
A vector with the clamped x and y values.
Examples:
clampVec2([1, 2], [0, 0], [3, 3]);
//=> [1, 2]:gear: crossVec2
Calculates the cross product of two two-dimensional vectors.
| Function | Type |
| ---------- | ---------- |
| crossVec2 | (v1: Vec2, v2: Vec2) => number |
Parameters:
v1: The first vector.v2: The second vector.
Returns:
The cross product of the two vectors.
Examples:
crossVec2([1, 2], [3, 4]);
//=> -2:gear: dotVec2
Calculates the dot product of two two-dimensional vectors.
| Function | Type |
| ---------- | ---------- |
| dotVec2 | (v1: Vec2, v2: Vec2) => number |
Parameters:
v1: The first vector.v2: The second vector.
Returns:
The dot product of the two vectors.
Examples:
dotVec2([1, 2], [3, 4]);
//=> 11:gear: divideVec2
Divides two two-dimensional vectors.
| Function | Type |
| ---------- | ---------- |
| divideVec2 | (v1: Vec2, v2: Vec2) => Vec2 |
Parameters:
v1: The first vector.v2: The second vector.
Returns:
The division of the two vectors.
Examples:
divideVec2([2, 10], [2, 2]);
//=> [1, 5]:gear: isEqualVec2
Determines if two two-dimensional vectors are equal.
| Function | Type |
| ---------- | ---------- |
| isEqualVec2 | ([x1, y1]: Vec2, [x2, y2]: Vec2) => boolean |
Parameters:
v1: The first vector.v2: The second vector.
Returns:
True if the vectors are equal, false otherwise.
Examples:
isEqualVec2([1, 2], [1, 2]);
//=> true:gear: floorVec2
Rounds down the components of a two-dimensional vector.
| Function | Type |
| ---------- | ---------- |
| floorVec2 | (v: Vec2) => Vec2 |
Parameters:
v: The vector to round down.
Returns:
The rounded down vector.
Examples:
floorVec2([2.5, 3.7]);
//=> [2, 3]:gear: lengthVec2
Calculates the length (magnitude) of a two-dimensional vector.
| Function | Type |
| ---------- | ---------- |
| lengthVec2 | (v: Vec2) => number |
Parameters:
v: The vector.
Returns:
The length of the vector.
Examples:
lengthVec2([3, 4]);
//=> 5:gear: maxVec2
Finds the maximum two-dimensional vector from a list of vectors.
| Function | Type |
| ---------- | ---------- |
| maxVec2 | (...vectors: Vec2[]) => Vec2 |
Parameters:
...vectors: The vectors to compare.
Returns:
A vector with the maximum x and y values.
Examples:
maxVec2([1, 2], [5, 1], [3, 4]);
//=> [5, 4]:gear: minVec2
Finds the minimum two-dimensional vector from a list of vectors.
| Function | Type |
| ---------- | ---------- |
| minVec2 | (...vectors: Vec2[]) => Vec2 |
Parameters:
...vectors: The vectors to compare.
Returns:
A vector with the minimum x and y values.
Examples:
minVec2([1, 2], [5, 1], [3, 4]);
//=> [1, 1]:gear: modVec2
Finds the modulus of two-dimensional vectors.
| Function | Type |
| ---------- | ---------- |
| modVec2 | (v1: Vec2, v2: Vec2) => Vec2 |
Parameters:
v1: The first vector.v2: The second vector.
Returns:
The modulus of the vectors.
Examples:
modVec2([5, 7], [3, 4]);
//=> [2, 3]:gear: multiplyVec2
Multiplies two two-dimensional vectors.
| Function | Type |
| ---------- | ---------- |
| multiplyVec2 | (v1: Vec2, v2: Vec2) => Vec2 |
Parameters:
v1: The first vector.v2: The second vector.
Returns:
The product of the vectors.
Examples:
multiplyVec2([2, 3], [4, 5]);
//=> [8, 15]:gear: normalizeVec2
Normalizes a two-dimensional vector.
| Function | Type |
| ---------- | ---------- |
| normalizeVec2 | (v: Vec2) => Vec2 |
Parameters:
v: The vector.
Returns:
The normalized vector.
Examples:
normalizeVec2([3, 4]);
//=> [0.6, 0.8]:gear: subtractVec2
Subtracts two two-dimensional vectors.
| Function | Type |
| ---------- | ---------- |
| subtractVec2 | (v1: Vec2, v2: Vec2) => Vec2 |
Parameters:
v1: The first vector.v2: The second vector.
Returns:
The difference of the vectors.
Examples:
subtractVec2([5, 6], [3, 2]);
//=> [2, 4]:gear: vec2
Constructs a two-dimensional vector.
| Function | Type |
| ---------- | ---------- |
| vec2 | (x: number, y?: number or undefined) => Vec2 |
Parameters:
x: The x-coordinate of the vector.y: The y-coordinate of the vector. If not provided, defaults to the x-coordinate.
Returns:
The constructed vector.
Examples:
vec2(1, 2);
//=> [1, 2]
vec2(1);
//=> [1, 1]:cocktail: Types
:gear: Vec2
| Type | Type |
| ---------- | ---------- |
| Vec2 | [number, number] |
