@daeinc/math
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -1,7 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.snapToArray = exports.snapBy = exports.roundF = exports.reflect = exports.polarToCartesian = exports.modIncl = exports.modF = exports.mod = exports.lerp = exports.mix = exports.map = exports.floorF = exports.distSq = exports.dist = exports.constrain = exports.clamp = exports.TWO_PI = exports.PI = void 0; | ||
// frequently used constants | ||
exports.PI = Math.PI; | ||
exports.TWO_PI = Math.PI * 2; | ||
export const PI = Math.PI; | ||
export const TWO_PI = Math.PI * 2; | ||
/** | ||
@@ -14,8 +11,7 @@ * clamp values between min and max (=constrain) | ||
*/ | ||
const clamp = (val, min, max) => Math.min(Math.max(val, min), max); | ||
exports.clamp = clamp; | ||
export const clamp = (val, min, max) => Math.min(Math.max(val, min), max); | ||
/** | ||
* alias for clamp() | ||
*/ | ||
exports.constrain = exports.clamp; | ||
export const constrain = clamp; | ||
/** | ||
@@ -29,6 +25,5 @@ * | ||
*/ | ||
const dist = (x1, y1, x2, y2) => { | ||
return Math.sqrt((0, exports.distSq)(x1, y1, x2, y2)); | ||
export const dist = (x1, y1, x2, y2) => { | ||
return Math.sqrt(distSq(x1, y1, x2, y2)); | ||
}; | ||
exports.dist = dist; | ||
/** | ||
@@ -42,6 +37,5 @@ * returns squared distance | ||
*/ | ||
const distSq = (x1, y1, x2, y2) => { | ||
export const distSq = (x1, y1, x2, y2) => { | ||
return (x2 - x1) ** 2 + (y2 - y1) ** 2; | ||
}; | ||
exports.distSq = distSq; | ||
/** | ||
@@ -61,3 +55,3 @@ * | ||
*/ | ||
const floorF = (n, digit) => { | ||
export const floorF = (n, digit) => { | ||
n = parseFloat(n.toFixed(digit)); | ||
@@ -67,3 +61,2 @@ const factor = Math.pow(10, digit); | ||
}; | ||
exports.floorF = floorF; | ||
/** | ||
@@ -78,4 +71,3 @@ * | ||
*/ | ||
const map = (val, s, e, ns, ne) => ns + ((val - s) / (e - s)) * (ne - ns); | ||
exports.map = map; | ||
export const map = (val, s, e, ns, ne) => ns + ((val - s) / (e - s)) * (ne - ns); | ||
/** | ||
@@ -88,8 +80,7 @@ * linear interpolation (=lerp) | ||
*/ | ||
const mix = (a, b, t) => a * (1 - t) + b * t; | ||
exports.mix = mix; | ||
export const mix = (a, b, t) => a * (1 - t) + b * t; | ||
/** | ||
* alias for mix() | ||
*/ | ||
exports.lerp = exports.mix; | ||
export const lerp = mix; | ||
/** | ||
@@ -101,6 +92,5 @@ * NOTE: it may not be accurate with non-integer numbers. | ||
*/ | ||
const mod = (n, max) => { | ||
export const mod = (n, max) => { | ||
return ((n % max) + max) % max; | ||
}; | ||
exports.mod = mod; | ||
/** | ||
@@ -113,3 +103,3 @@ * modulo(%) for float numbers up to precision digit. | ||
*/ | ||
const modF = (n, max, precision = 6) => { | ||
export const modF = (n, max, precision = 6) => { | ||
const mlt = Math.pow(10, precision); // multiplier to make it integer | ||
@@ -120,3 +110,2 @@ const ni = Math.floor(n * mlt); | ||
}; | ||
exports.modF = modF; | ||
// export const floorF = (n: number, digit: number): number => { | ||
@@ -134,3 +123,3 @@ // n = parseFloat(n.toFixed(digit)); | ||
*/ | ||
const modIncl = (n, max) => { | ||
export const modIncl = (n, max) => { | ||
if (max < 0) | ||
@@ -140,3 +129,2 @@ throw new Error("modIncl(): 2nd arg must be >= 0"); | ||
}; | ||
exports.modIncl = modIncl; | ||
/** | ||
@@ -148,6 +136,5 @@ * converts polar coordinate to cartesian. to update center, result.map((v,i)=> v + center[i]) | ||
*/ | ||
const polarToCartesian = (radius, angle) => { | ||
export const polarToCartesian = (radius, angle) => { | ||
return [Math.cos(angle) * radius, Math.sin(angle) * radius]; | ||
}; | ||
exports.polarToCartesian = polarToCartesian; | ||
/** | ||
@@ -159,6 +146,5 @@ * reflect a scalar value along axis. good for creating reflected version. | ||
*/ | ||
const reflect = (num, axis) => { | ||
export const reflect = (num, axis) => { | ||
return axis - (num - axis); | ||
}; | ||
exports.reflect = reflect; | ||
/** | ||
@@ -170,7 +156,6 @@ * good for drawing shapes to include the maximum value (round up) | ||
*/ | ||
const roundF = (n, digit) => { | ||
export const roundF = (n, digit) => { | ||
const factor = Math.pow(10, digit); | ||
return Math.round(n * factor) / factor; | ||
}; | ||
exports.roundF = roundF; | ||
/** | ||
@@ -183,6 +168,5 @@ * snap value to increment | ||
*/ | ||
const snapBy = (n, inc) => { | ||
export const snapBy = (n, inc) => { | ||
return Math.round(n / inc) * inc; | ||
}; | ||
exports.snapBy = snapBy; | ||
/** | ||
@@ -194,3 +178,3 @@ * snap to a value in array. whatever is closest to. | ||
*/ | ||
const snapToArray = (n, snapArr) => { | ||
export const snapToArray = (n, snapArr) => { | ||
snapArr.sort((a, b) => a - b); // sort numbers in order | ||
@@ -212,3 +196,2 @@ if (n <= snapArr[0]) | ||
}; | ||
exports.snapToArray = snapToArray; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@daeinc/math", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Math utilities", | ||
@@ -5,0 +5,0 @@ "types": "./dist/index.d.ts", |
@@ -23,2 +23,4 @@ # math utilities | ||
Clamp values between min and max (=constrain) | ||
### constrain | ||
@@ -30,2 +32,4 @@ | ||
Alias for `clamp()` | ||
### dist | ||
@@ -37,2 +41,4 @@ | ||
Compute distance between two points | ||
### distSq | ||
@@ -44,3 +50,3 @@ | ||
There's another version in `@daeinc/geom` that takes `Pt: number[]` type as arguments. | ||
Compute squared distance between two points. There's another version in `@daeinc/geom` that takes `Pt: number[]` type as arguments. | ||
@@ -53,2 +59,4 @@ ### floorF | ||
Floor a float value to the specified digit. | ||
### map | ||
@@ -66,2 +74,4 @@ | ||
Map a value from one range to another. | ||
### mix | ||
@@ -73,2 +83,4 @@ | ||
Linear interpolation (=lerp) | ||
### lerp | ||
@@ -80,2 +92,4 @@ | ||
Alias for `mix()` | ||
### mod | ||
@@ -87,2 +101,4 @@ | ||
Modular operation | ||
### modF | ||
@@ -94,2 +110,4 @@ | ||
Modulo(%) operation for float numbers up to precision digit. | ||
### modIncl | ||
@@ -101,2 +119,4 @@ | ||
Inclusive modulo operation. `modIncl(1, 3)` will include `3`. It can handle negative number and returns positive value | ||
### polarToCartesian | ||
@@ -120,2 +140,4 @@ | ||
Rreflect a scalar value along axis. If you want to reflect a point in 2d space, you can use [`reflectPoint()`](https://github.com/cdaein/geom#reflectpoint) from [`@daeinc/geom`](https://github.com/cdaein/geom). | ||
### roundF | ||
@@ -127,2 +149,4 @@ | ||
Round a float value. Good for drawing shapes to include the maximum value (round up) | ||
### snapBy | ||
@@ -134,2 +158,4 @@ | ||
Snap value to increment using `Math.round()`. | ||
### snapToArray | ||
@@ -141,2 +167,4 @@ | ||
Snap to a closest value in an array. | ||
## To do | ||
@@ -143,0 +171,0 @@ |
Sorry, the diff of this file is not supported yet
18143
162
304