This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

@bedard/math
TypeScript icon, indicating that this package has built-in type declarations

0.8.0 • Public • Published

@bedard/math

Build status Codecov Dependencies Dev dependencies NPM Bundle size License

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) // [0, 1] (approximate)

bilerp

Bi-linear interpolation between vectors.

import { bilerp } from '@bedard/math'

bilerp([0, 0], [10, 10], 0.5) // [5, 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,
]) // [[0, 3, 6], [1, 4, 7], [2, 5, 8]]

degreesToRadians

Convert degrees to radians.

import { degreesToRadians } from '@bedard/math'

degreesToRadians(180) // 3.141592653589793

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],
]) // [0, 1, 2, 3, 4, 5, 6, 7, 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],
]) // [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],
]) // [[0, 3, 6], [1, 4, 7], [2, 5, 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]]) // [0, 0]

isEven

Test if a number is even.

import { isEven } from '@bedard/math'

isEven(2) // true

lerp

Linear interpolation between numbers.

import { lerp } from '@bedard/math'

lerp(0, 10, 0.5) // 5

measure

Measure the distance between two vectors.

import { measure } from '@bedard/math'

// arguments can be provided as a pair of vectors
measure([0, 0], [3, 4]) // 5

// or as a single line argument
measure([[0, 0], [3, 4]]) // 5

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) // [[0, 1], [-1, 0], [0, -1], [1, 0]] (approximate)

radiansToDegrees

Convert radians to degrees.

import { radiansToDegrees } from '@bedard/math'

radiansToDegrees(Math.PI) // 180

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) // [6, 3, 0, 7, 4, 1, 8, 5, 2]

rotateVector

Rotate a vector counter-clockwise around the origin.

import { rotateVector } from '@bedard/math'

rotateVector([0, 1], 90) // [-1, 0] (approximate)

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,
]) // [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

slope

Calculate the slope of a line.

import { slope } from '@bedard/math'

// arguments can be provided as a pair of vectors
slope([0, 0], [1, 1]) // 1

// or as a single line argument
slope([[0, 0], [1, 1]]) // 1

License

MIT

Copyright (c) 2021-present, Scott Bedard

Readme

Keywords

none

Package Sidebar

Install

npm i @bedard/math

Weekly Downloads

2

Version

0.8.0

License

MIT

Unpacked Size

59.5 kB

Total Files

56

Last publish

Collaborators

  • scottbedard