Necrotizing Pineapple Music

# npm

## @ishoa/math-helpers

1.1.1 • Public • Published

# Math-Helper-Functions

Some useful Math functions not built into the Javascript Math Object. Mainly useful for Web Games.

## Functions

map(x, istart, istop, ostart, ostop)`Number`

Map a value from a range of numbers to another range of numbers.

x is the intial value between istart and istop and return the value between ostart and ostop

limit(x, min, max)`Number`

Limit a value between a min/max value

toRad(x)`Number`

Convert the angle, provided in degrees (0-360), into radians (0-2*PI)

toDeg(x)`Number`

Convert the angle, provided in radians (0-2*PI), into degrees (0-360)

range(min, max)`Number`

Get random Float value between range of numbers

distance(a, b)`Number`

Distance between two points

lerp(current, target, time)`Number`

Lerp between current position to target position over a period of time

average(a, b)`Number`

Returns the average or the midpoint between two numbers

midpoint(v1, v2)`Object`

Get the midpoint between two points

chance(percent)

Return a true or false for a random chance of a percentage

## map(x, istart, istop, ostart, ostop) ⇒ `Number`

Map a value from a range of numbers to another range of numbers x is to istart and istop segment.

Returns: `Number` - The number that is in the same relation to ostart and ostop segment

Param Type Description
x `Number` Value we are testing
istart `Number` Min value to test
istop `Number` Max value to test
ostart `Number` Min value to map to (based on x value)
ostop `Number` Max value to map to (based on x value)

Example

`map(Math.random(), 0, 1, 10, 20); will provided a number between 10 and 20`

## limit(x, min, max) ⇒ `Number`

Limit a value between a min/max value

Returns: `Number` - Either x, or the nearest limit if x is out of bounds.

Param Type Description
x `Number` Value we need to limit
min `Number` Min number to return
max `Number` Max number to return

## toRad(x) ⇒ `Number`

Convert the angle, provided in degrees (0-360), into radians (0-2*PI)

Returns: `Number` - Angle in Radians

Param Type Description
x `Number` Angle in Degrees

## toDeg(x) ⇒ `Number`

Convert the angle, provided in radians (0-2*PI), into degrees (0-360)

Returns: `Number` - Angle in Degrees

Param Type Description
x `Number` Angle in Radians

## range(min, max) ⇒ `Number`

Get random Float value between range of numbers

Returns: `Number` - Return random Float value between min and max range

Param Type Description
min `Number` Min range value
max `Number` Max range value

## distance(a, b) ⇒ `Number`

Distance between two points

Returns: `Number` - Distance between points

Param Type Description
a `Object` first point {x: 0, y: 0}
b `Object` second point {x: 0, y: 0}

Example

`- can supply 2D or 3D point {x: 0, y: 0, z: 0}`

## lerp(current, target, time) ⇒ `Number`

Lerp between current position to target position over a period of time

Returns: `Number` - Value between current/target over the time

Param Type Description
current `Number` Current position value
target `Number` Target position value
time `Number` Time value

## average(a, b) ⇒ `Number`

Returns the average or the midpoint between two numbers

Returns: `Number` - - the midpoint between two numbers

Param Type Description
a `Number` first number
b `Number` second number

## midpoint(v1, v2) ⇒ `Object`

Get the midpoint between two points

Returns: `Object` - - {x: x, y: y, z: z}

Param Type Description
v1 `Object` {x: 0, y: 0}
v2 `Object` {x: 0, y: 0}

Example

`- Also accepts 3D point {x: 0, y: 0, z: 0}`

## chance(percent)

Return a true or false for a random chance of a percentage

Param Type Description
percent `Number` float percentage 0 -> 1

Example

`if(chance(0.2)){ console.log('20% chance true') }`

## Keywords

### Install

`npm i @ishoa/math-helpers`

### Repository

github.com/ishoax/Math-Helper-Functions

0

1.1.1

MIT

20.5 kB

8