@internet/maths v0.1.2
Maths utilities
:books: Documentation | :globe_with_meridians: Internet modules
Requirements
- ES6 Modules support
- Using a module bundler like Webpack, Rollup or Parcel
- Native support from browser
- From NodeJS with something like esm
Module Installation
# using npm
$ npm install --save @internet/maths
# or using yarn
$ yarn add @internet/mathsAPI
Example
import { mod, map, lerp, ... } from '@internet/maths'API
- maths
- .mod(dividend, divisor) ⇒ number
- .map(value, start1, stop1, start2, stop2) ⇒ number
- .clamp(value, min, max) ⇒ number
- .norm(value, min, max) ⇒ number
- .lerp(start, end, amount) ⇒ number
- .damp(source, target, smoothing, dt) ⇒ number
- .dist(x1, y1, x2, y2) ⇒ number
- .sqdist(x1, y1, x2, y2) ⇒ number
- .ang(x1, y1, x2, y2) ⇒ number
- .polarToCart(radius, angle) ⇒ array
- .radToDeg(angle) ⇒ number
- .degToRad(angle) ⇒ number
- .mean(values) ⇒ number
- .median(values) ⇒ number
maths.mod(dividend, divisor) ⇒ number
Perform a modulo operation.
Kind: static method of maths
Returns: number - Result of the modulo operation
| Param | Type | Description |
|---|---|---|
| dividend | number | A dividend |
| divisor | number | A divisor |
Example
const resut = mod(-1, 5) // return 4maths.map(value, start1, stop1, start2, stop2) ⇒ number
Re-maps a number from one range to another.
Kind: static method of maths
| Param | Type | Description |
|---|---|---|
| value | number | The incoming value to be converted |
| start1 | number | Lower bound of the value's current range |
| stop1 | number | Upper bound of the value's current range |
| start2 | number | Lower bound of the value`'s target range |
| stop2 | number | Upper bound of the value's target range |
maths.clamp(value, min, max) ⇒ number
Constrains a value to not exceed a maximum and minimum value.
Kind: static method of maths
| Param | Type | Description |
|---|---|---|
| value | number | The value to constrain |
| min | number | Minimum limit |
| max | number | Maximum limit |
maths.norm(value, min, max) ⇒ number
Normalizes a number from another range into a value between 0 and 1.
Kind: static method of maths
Returns: number - Normalized value
| Param | Type | Description |
|---|---|---|
| value | number | The incoming value to be converted |
| min | number | Lower bound of the value's current range |
| max | number | Upper bound of the value's current range |
maths.lerp(start, end, amount) ⇒ number
Perform a linear interpolation between two values. Equivalent of mix in GLSL.
Kind: static method of maths
Returns: number - Lerped value
| Param | Type | Description |
|---|---|---|
| start | number | Start of the range in which to interpolate |
| end | number | End of the range in which to interpolate |
| amount | number | Amount to lerp between the two number (from 0 to 1) |
maths.damp(source, target, smoothing, dt) ⇒ number
Frame-rate aware damping function
Kind: static method of maths
Returns: number - Damped value
| Param | Type | Description |
|---|---|---|
| source | number | Initial value |
| target | number | Target value |
| smoothing | number | Smoothing rate |
| dt | number | Delta-time (in milliseconds) |
maths.dist(x1, y1, x2, y2) ⇒ number
Calculates the distance between two points (2D)
Kind: static method of maths
Returns: number - Distance
| Param | Type | Description |
|---|---|---|
| x1 | number | x-coordinate of the first point |
| y1 | number | y-coordinate of the first point |
| x2 | number | x-coordinate of the second point |
| y2 | number | y-coordinate of the second point |
maths.sqdist(x1, y1, x2, y2) ⇒ number
Calculates the squared distance between two points (2D)
Kind: static method of maths
Returns: number - Distance
| Param | Type | Description |
|---|---|---|
| x1 | number | x-coordinate of the first point |
| y1 | number | y-coordinate of the first point |
| x2 | number | x-coordinate of the second point |
| y2 | number | y-coordinate of the second point |
maths.ang(x1, y1, x2, y2) ⇒ number
Calculates the angle between two points (2D)
Kind: static method of maths
Returns: number - Angle (in Radians)
| Param | Type | Description |
|---|---|---|
| x1 | number | x-coordinate of the first point |
| y1 | number | y-coordinate of the first point |
| x2 | number | x-coordinate of the second point |
| y2 | number | y-coordinate of the second point |
maths.polarToCart(radius, angle) ⇒ array
Calculates the angle between two points (2D)
Kind: static method of maths
Returns: array - Array containing the cartesian coordinates x, y
| Param | Type | Description |
|---|---|---|
| radius | number | Radius / Distance |
| angle | number | Angle |
maths.radToDeg(angle) ⇒ number
Convert angle from radians to degrees
Kind: static method of maths
Returns: number - Angle in degree
| Param | Type | Description |
|---|---|---|
| angle | number | Angle in radian |
maths.degToRad(angle) ⇒ number
Convert angle from degrees to radians
Kind: static method of maths
Returns: number - Angle in radian
| Param | Type | Description |
|---|---|---|
| angle | number | Angle in degree |
maths.mean(values) ⇒ number
Get the mean average of values
Kind: static method of maths
Returns: number - mean value
| Param | Type |
|---|---|
| values | array |
maths.median(values) ⇒ number
Get the median average of values
Kind: static method of maths
Returns: number - median value
| Param | Type |
|---|---|
| values | array |