@daeinc/math
Advanced tools
Comparing version 0.1.0 to 0.2.0
/** | ||
* linear interpolation (=lerp) | ||
* @param a start value | ||
* @param b stop value | ||
* @param t amount 0..1 | ||
* @returns | ||
*/ | ||
export declare const mix: (a: number, b: number, t: number) => number; | ||
/** | ||
* alias for mix() | ||
*/ | ||
export declare const lerp: (a: number, b: number, t: number) => number; | ||
/** | ||
* | ||
* @param val | ||
* @param s | ||
* @param e | ||
* @param ns | ||
* @param ne | ||
* @returns | ||
*/ | ||
export declare const map: (val: number, s: number, e: number, ns: number, ne: number) => number; | ||
/** | ||
* clamp values between min and max (=constrain) | ||
@@ -37,2 +15,20 @@ * @param val | ||
* | ||
* @param x1 | ||
* @param y1 | ||
* @param x2 | ||
* @param y2 | ||
* @returns | ||
*/ | ||
export declare const dist: (x1: number, y1: number, x2: number, y2: number) => number; | ||
/** | ||
* returns squared distance | ||
* @param x1 | ||
* @param y1 | ||
* @param x2 | ||
* @param y2 | ||
* @returns | ||
*/ | ||
export declare const distSq: (x1: number, y1: number, x2: number, y2: number) => number; | ||
/** | ||
* | ||
* @param n number | ||
@@ -50,31 +46,24 @@ * @param amp how far it can be away from input | ||
/** | ||
* good for drawing shapes to include the maximum value (round up) | ||
* @param n float number | ||
* @param digit how many float digits to keep | ||
* | ||
* @param val | ||
* @param s | ||
* @param e | ||
* @param ns | ||
* @param ne | ||
* @returns | ||
*/ | ||
export declare const roundF: (n: number, digit: number) => number; | ||
export declare const map: (val: number, s: number, e: number, ns: number, ne: number) => number; | ||
/** | ||
* snap value to increment | ||
* @param n original number | ||
* @param inc increment to snap to. | ||
* linear interpolation (=lerp) | ||
* @param a start value | ||
* @param b stop value | ||
* @param t amount 0..1 | ||
* @returns | ||
* | ||
*/ | ||
export declare const snapBy: (n: number, inc: number) => number; | ||
export declare const mix: (a: number, b: number, t: number) => number; | ||
/** | ||
* snap to a value in array. whatever is closest to. | ||
* @param n original number | ||
* @param snapArr values to snap to | ||
* @returns {number | undefined} | ||
* alias for mix() | ||
*/ | ||
export declare const snapToArray: (n: number, snapArr: number[]) => number; | ||
export declare const lerp: (a: number, b: number, t: number) => number; | ||
/** | ||
* reflect a scalar value along axis. good for creating reflected version. | ||
* @param num number to flip | ||
* @param axis value to reflect against | ||
* @returns | ||
*/ | ||
export declare const reflect: (num: number, axis: number) => number; | ||
/** | ||
* NOTE: it may not be accurate with non-integer numbers. | ||
@@ -93,3 +82,3 @@ * @param n | ||
*/ | ||
export declare const modf: (n: number, max: number, precision?: number) => number; | ||
export declare const modF: (n: number, max: number, precision?: number) => number; | ||
/** | ||
@@ -104,19 +93,30 @@ * inclusive modulo. modIncl(1, 3) will include 3. | ||
/** | ||
* returns squared distance | ||
* @param x1 | ||
* @param y1 | ||
* @param x2 | ||
* @param y2 | ||
* reflect a scalar value along axis. good for creating reflected version. | ||
* @param num number to flip | ||
* @param axis value to reflect against | ||
* @returns | ||
*/ | ||
export declare const distSq: (x1: number, y1: number, x2: number, y2: number) => number; | ||
export declare const reflect: (num: number, axis: number) => number; | ||
/** | ||
* | ||
* @param x1 | ||
* @param y1 | ||
* @param x2 | ||
* @param y2 | ||
* good for drawing shapes to include the maximum value (round up) | ||
* @param n float number | ||
* @param digit how many float digits to keep | ||
* @returns | ||
*/ | ||
export declare const dist: (x1: number, y1: number, x2: number, y2: number) => number; | ||
export declare const roundF: (n: number, digit: number) => number; | ||
/** | ||
* snap value to increment | ||
* @param n original number | ||
* @param inc increment to snap to. | ||
* @returns | ||
* | ||
*/ | ||
export declare const snapBy: (n: number, inc: number) => number; | ||
/** | ||
* snap to a value in array. whatever is closest to. | ||
* @param n original number | ||
* @param snapArr values to snap to | ||
* @returns {number | undefined} | ||
*/ | ||
export declare const snapToArray: (n: number, snapArr: number[]) => number; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.dist = exports.distSq = exports.modIncl = exports.modf = exports.mod = exports.reflect = exports.snapToArray = exports.snapBy = exports.roundF = exports.floorF = exports.constrain = exports.clamp = exports.map = exports.lerp = exports.mix = void 0; | ||
exports.snapToArray = exports.snapBy = exports.roundF = exports.reflect = exports.modIncl = exports.modF = exports.mod = exports.lerp = exports.mix = exports.map = exports.floorF = exports.distSq = exports.dist = exports.constrain = exports.clamp = void 0; | ||
/** | ||
* linear interpolation (=lerp) | ||
* @param a start value | ||
* @param b stop value | ||
* @param t amount 0..1 | ||
* @returns | ||
*/ | ||
const mix = (a, b, t) => a * (1 - t) + b * t; | ||
exports.mix = mix; | ||
/** | ||
* alias for mix() | ||
*/ | ||
exports.lerp = exports.mix; | ||
/** | ||
* | ||
* @param val | ||
* @param s | ||
* @param e | ||
* @param ns | ||
* @param ne | ||
* @returns | ||
*/ | ||
const map = (val, s, e, ns, ne) => ns + ((val - s) / (e - s)) * (ne - ns); | ||
exports.map = map; | ||
/** | ||
* clamp values between min and max (=constrain) | ||
@@ -43,2 +19,26 @@ * @param val | ||
* | ||
* @param x1 | ||
* @param y1 | ||
* @param x2 | ||
* @param y2 | ||
* @returns | ||
*/ | ||
const dist = (x1, y1, x2, y2) => { | ||
return Math.sqrt((0, exports.distSq)(x1, y1, x2, y2)); | ||
}; | ||
exports.dist = dist; | ||
/** | ||
* returns squared distance | ||
* @param x1 | ||
* @param y1 | ||
* @param x2 | ||
* @param y2 | ||
* @returns | ||
*/ | ||
const distSq = (x1, y1, x2, y2) => { | ||
return (x2 - x1) ** 2 + (y2 - y1) ** 2; | ||
}; | ||
exports.distSq = distSq; | ||
/** | ||
* | ||
* @param n number | ||
@@ -63,58 +63,26 @@ * @param amp how far it can be away from input | ||
/** | ||
* good for drawing shapes to include the maximum value (round up) | ||
* @param n float number | ||
* @param digit how many float digits to keep | ||
* | ||
* @param val | ||
* @param s | ||
* @param e | ||
* @param ns | ||
* @param ne | ||
* @returns | ||
*/ | ||
const roundF = (n, digit) => { | ||
const factor = Math.pow(10, digit); | ||
return Math.round(n * factor) / factor; | ||
}; | ||
exports.roundF = roundF; | ||
const map = (val, s, e, ns, ne) => ns + ((val - s) / (e - s)) * (ne - ns); | ||
exports.map = map; | ||
/** | ||
* snap value to increment | ||
* @param n original number | ||
* @param inc increment to snap to. | ||
* linear interpolation (=lerp) | ||
* @param a start value | ||
* @param b stop value | ||
* @param t amount 0..1 | ||
* @returns | ||
* | ||
*/ | ||
const snapBy = (n, inc) => { | ||
return Math.round(n / inc) * inc; | ||
}; | ||
exports.snapBy = snapBy; | ||
const mix = (a, b, t) => a * (1 - t) + b * t; | ||
exports.mix = mix; | ||
/** | ||
* snap to a value in array. whatever is closest to. | ||
* @param n original number | ||
* @param snapArr values to snap to | ||
* @returns {number | undefined} | ||
* alias for mix() | ||
*/ | ||
const snapToArray = (n, snapArr) => { | ||
snapArr.sort((a, b) => a - b); // sort numbers in order | ||
if (n <= snapArr[0]) | ||
return snapArr[0]; // if less than the smallest one | ||
else if (n >= snapArr[snapArr.length - 1]) | ||
return snapArr[snapArr.length - 1]; // if greater than the largest num | ||
else { | ||
for (let i = 0; i < snapArr.length - 1; i++) { | ||
const prev = snapArr[i]; | ||
const next = snapArr[i + 1]; | ||
if (n > prev && n < next) { | ||
return Math.abs(n - next) <= Math.abs(n - prev) ? next : prev; | ||
} | ||
} | ||
} | ||
throw new Error("snapToArray(): did not meet any condition"); | ||
}; | ||
exports.snapToArray = snapToArray; | ||
exports.lerp = exports.mix; | ||
/** | ||
* reflect a scalar value along axis. good for creating reflected version. | ||
* @param num number to flip | ||
* @param axis value to reflect against | ||
* @returns | ||
*/ | ||
const reflect = (num, axis) => { | ||
return axis - (num - axis); | ||
}; | ||
exports.reflect = reflect; | ||
/** | ||
* NOTE: it may not be accurate with non-integer numbers. | ||
@@ -136,3 +104,3 @@ * @param n | ||
*/ | ||
const modf = (n, max, precision = 6) => { | ||
const modF = (n, max, precision = 6) => { | ||
const mlt = Math.pow(10, precision); // multiplier to make it integer | ||
@@ -143,3 +111,3 @@ const ni = Math.floor(n * mlt); | ||
}; | ||
exports.modf = modf; | ||
exports.modF = modF; | ||
/** | ||
@@ -159,25 +127,57 @@ * inclusive modulo. modIncl(1, 3) will include 3. | ||
/** | ||
* returns squared distance | ||
* @param x1 | ||
* @param y1 | ||
* @param x2 | ||
* @param y2 | ||
* reflect a scalar value along axis. good for creating reflected version. | ||
* @param num number to flip | ||
* @param axis value to reflect against | ||
* @returns | ||
*/ | ||
const distSq = (x1, y1, x2, y2) => { | ||
return (x2 - x1) ** 2 + (y2 - y1) ** 2; | ||
const reflect = (num, axis) => { | ||
return axis - (num - axis); | ||
}; | ||
exports.distSq = distSq; | ||
exports.reflect = reflect; | ||
/** | ||
* | ||
* @param x1 | ||
* @param y1 | ||
* @param x2 | ||
* @param y2 | ||
* good for drawing shapes to include the maximum value (round up) | ||
* @param n float number | ||
* @param digit how many float digits to keep | ||
* @returns | ||
*/ | ||
const dist = (x1, y1, x2, y2) => { | ||
return Math.sqrt((0, exports.distSq)(x1, y1, x2, y2)); | ||
const roundF = (n, digit) => { | ||
const factor = Math.pow(10, digit); | ||
return Math.round(n * factor) / factor; | ||
}; | ||
exports.dist = dist; | ||
exports.roundF = roundF; | ||
/** | ||
* snap value to increment | ||
* @param n original number | ||
* @param inc increment to snap to. | ||
* @returns | ||
* | ||
*/ | ||
const snapBy = (n, inc) => { | ||
return Math.round(n / inc) * inc; | ||
}; | ||
exports.snapBy = snapBy; | ||
/** | ||
* snap to a value in array. whatever is closest to. | ||
* @param n original number | ||
* @param snapArr values to snap to | ||
* @returns {number | undefined} | ||
*/ | ||
const snapToArray = (n, snapArr) => { | ||
snapArr.sort((a, b) => a - b); // sort numbers in order | ||
if (n <= snapArr[0]) | ||
return snapArr[0]; // if less than the smallest one | ||
else if (n >= snapArr[snapArr.length - 1]) | ||
return snapArr[snapArr.length - 1]; // if greater than the largest num | ||
else { | ||
for (let i = 0; i < snapArr.length - 1; i++) { | ||
const prev = snapArr[i]; | ||
const next = snapArr[i + 1]; | ||
if (n > prev && n < next) { | ||
return Math.abs(n - next) <= Math.abs(n - prev) ? next : prev; | ||
} | ||
} | ||
} | ||
throw new Error("snapToArray(): did not meet any condition"); | ||
}; | ||
exports.snapToArray = snapToArray; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@daeinc/math", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Math utilities", | ||
@@ -5,0 +5,0 @@ "types": "./dist/index.d.ts", |
# math utilities | ||
For personal use. Breakcing changes expected. | ||
For personal consumption. Breaking changes expected. | ||
@@ -5,0 +5,0 @@ ## License |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14852
1