@bedard/math

This library exists to help me avoid duplicate code between projects. My goal is to expose small, well tested, and tree-shakeable utility functions. Given the simplicity of these, I do not anticipate breaking changes. But be warned, this project is not following semantic versioning.
Installation
The recommended installation method is via NPM.
npm install @bedard/math
Alternatively, these functions maybe be accessed via a CDN. When using a CDN, the library will be exposed globally as BedardMath
.
<script src="https://unpkg.com/@bedard/math"></script>
Functions
angleFrom
Calculate angled distance from a vector. An angle of 0
degrees will track along the X axis, with positive angles rotating counter-clockwise.
import { angleFrom } from '@bedard/math'
angleFrom([0, 0], 90, 1)
bilerp
Bi-linear interpolation between vectors.
import { bilerp } from '@bedard/math'
bilerp([0, 0], [10, 10], 0.5)
cols
Chunk a square matrix into columns. Note that the source matrix must be provided in row-major order.
import { cols } from '@bedard/math'
cols([
0, 1, 2,
3, 4, 5,
6, 7, 8,
])
degreesToRadians
Convert degrees to radians.
import { degreesToRadians } from '@bedard/math'
degreesToRadians(180)
flattenCols
Flatten an array of columns to a matrix in row-major order.
import { flattenCols } from '@bedard/math'
flattenCols([
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
])
flattenRows
Flatten an array of rows to a matrix in row-major order.
import { flattenRows } from '@bedard/math'
flattenRows([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
])
flip
Convert between rows and columns. A good way to visualize this operation is holding a card by the top-left and bottom-right corners and flipping it over.
import { flip } from '@bedard/math'
flip([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
])
intersect
Intersect two-dimensional lines. Returns undefined
if lines are parellel.
import { intersect } from '@bedard/math'
intersect([[-1, 0], [1, 0]], [[0, 1], [0, -1]])
isEven
Test if a number is even.
import { isEven } from '@bedard/math'
isEven(2)
lerp
Linear interpolation between numbers.
import { lerp } from '@bedard/math'
lerp(0, 10, 0.5)
measure
Measure the distance between two vectors.
import { measure } from '@bedard/math'
measure([0, 0], [3, 4])
measure([[0, 0], [3, 4]])
polygon
Create a regular polygon. The first argument defines the number of points, with the second defining the circumradius. Points start from the 12 o'clock position, and rotate counter-clockwise around the origin.
import { polygon } from '@bedard/math'
polygon(4, 1)
radiansToDegrees
Convert radians to degrees.
import { radiansToDegrees } from '@bedard/math'
radiansToDegrees(Math.PI)
rotateMatrix
Rotate a square matrix. Positive values apply clockwise rotations.
import { rotateMatrix } from '@bedard/math'
rotateMatrix([
0, 1, 2,
3, 4, 5,
6, 7, 8,
], 1)
rotateVector
Rotate a vector counter-clockwise around the origin.
import { rotateVector } from '@bedard/math'
rotateVector([0, 1], 90)
rows
Chunk a square matrix into rows. Note that the source matrix must be provided in row-major order.
import { rows } from '@bedard/math'
rows([
0, 1, 2,
3, 4, 5,
6, 7, 8,
])
slope
Calculate the slope of a line.
import { slope } from '@bedard/math'
slope([0, 0], [1, 1])
slope([[0, 0], [1, 1]])
License
MIT
Copyright (c) 2021-present, Scott Bedard