From 8701cadbe8b8954bb5b7fde47c28e98813bae7c8 Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 3 Nov 2019 10:40:08 -0600 Subject: [PATCH] refactor: math export + range --- packages/math/index.js | 105 +++++++++++++++++----------------- packages/math/math.js | 52 +++++++++++++++++ packages/math/range.js | 33 +++++++++++ packages/math/vector/index.js | 19 ++++++ 4 files changed, 156 insertions(+), 53 deletions(-) create mode 100644 packages/math/math.js create mode 100644 packages/math/range.js diff --git a/packages/math/index.js b/packages/math/index.js index 0d8c44c..007002e 100644 --- a/packages/math/index.js +++ b/packages/math/index.js @@ -1,6 +1,7 @@ // import * as Matrix from './matrix'; // export {Matrix} export {QuadTree} from './quadtree'; +export {Range} from './range'; import * as Rectangle from './rectangle'; export {Rectangle} export {SimpleMovingAverage} from './sma'; @@ -9,56 +10,54 @@ export {Vector} import * as Vertice from './vertice'; export {Vertice} -export const abs = Math.abs; -export const acos = Math.acos; -export const acosh = Math.acosh; -export const asin = Math.asin; -export const asinh = Math.asinh; -export const atan = Math.atan; -export const atanh = Math.atanh; -export const atan2 = Math.atan2; -export const ceil = Math.ceil; -export const cbrt = Math.cbrt; -export const expm1 = Math.expm1; -export const clz32 = Math.clz32; -export const cos = Math.cos; -export const cosh = Math.cosh; -export const exp = Math.exp; -export const floor = Math.floor; -export const fround = Math.fround; -export const hypot = Math.hypot; -export const imul = Math.imul; -export const log = Math.log; -export const log1p = Math.log1p; -export const log2 = Math.log2; -export const log10 = Math.log10; -export const max = Math.max; -export const min = Math.min; -export const pow = Math.pow; -export const random = Math.random; -export const round = Math.round; -export const sign = Math.sign; -export const sin = Math.sin; -export const sinh = Math.sinh; -export const sqrt = Math.sqrt; -export const tan = Math.tan; -export const tanh = Math.tanh; -export const trunc = Math.trunc; -export const E = Math.E; -export const LN10 = Math.LN10; -export const LN2 = Math.LN2; -export const LOG10E = Math.LOG10E; -export const LOG2E = Math.LOG2E; -export const PI = Math.PI; -export const SQRT1_2 = Math.SQRT1_2; -export const SQRT2 = Math.SQRT2; - -export const add = (l, r) => l + r; -export const div = (l, r) => l / r; -export const frac = (number) => number % 1; -export const mul = (l, r) => l * r; -export const randomNumber = (min, max, floor = true) => { - const mag = Math.random() * ((max - min) + 1); - return min + (floor ? Math.floor(mag) : mag); -}; -export const sub = (l, r) => l - r; +export { + abs, + acos, + acosh, + asin, + asinh, + atan, + atanh, + atan2, + ceil, + cbrt, + expm1, + clz32, + cos, + cosh, + exp, + floor, + fround, + hypot, + imul, + log, + log1p, + log2, + log10, + max, + min, + pow, + random, + round, + sign, + sin, + sinh, + sqrt, + tan, + tanh, + trunc, + E, + LN10, + LN2, + LOG10E, + LOG2E, + PI, + SQRT1_2, + SQRT2, + add, + div, + frac, + mul, + randomNumber, + sub, +} from './math'; diff --git a/packages/math/math.js b/packages/math/math.js new file mode 100644 index 0000000..21173b1 --- /dev/null +++ b/packages/math/math.js @@ -0,0 +1,52 @@ +export const abs = Math.abs; +export const acos = Math.acos; +export const acosh = Math.acosh; +export const asin = Math.asin; +export const asinh = Math.asinh; +export const atan = Math.atan; +export const atanh = Math.atanh; +export const atan2 = Math.atan2; +export const ceil = Math.ceil; +export const cbrt = Math.cbrt; +export const expm1 = Math.expm1; +export const clz32 = Math.clz32; +export const cos = Math.cos; +export const cosh = Math.cosh; +export const exp = Math.exp; +export const floor = Math.floor; +export const fround = Math.fround; +export const hypot = Math.hypot; +export const imul = Math.imul; +export const log = Math.log; +export const log1p = Math.log1p; +export const log2 = Math.log2; +export const log10 = Math.log10; +export const max = Math.max; +export const min = Math.min; +export const pow = Math.pow; +export const random = Math.random; +export const round = Math.round; +export const sign = Math.sign; +export const sin = Math.sin; +export const sinh = Math.sinh; +export const sqrt = Math.sqrt; +export const tan = Math.tan; +export const tanh = Math.tanh; +export const trunc = Math.trunc; +export const E = Math.E; +export const LN10 = Math.LN10; +export const LN2 = Math.LN2; +export const LOG10E = Math.LOG10E; +export const LOG2E = Math.LOG2E; +export const PI = Math.PI; +export const SQRT1_2 = Math.SQRT1_2; +export const SQRT2 = Math.SQRT2; + +export const add = (l, r) => l + r; +export const div = (l, r) => l / r; +export const frac = (number) => number % 1; +export const mul = (l, r) => l * r; +export const randomNumber = (min, max) => { + return min + Math.random() * (max - min); +}; +export const sub = (l, r) => l - r; diff --git a/packages/math/range.js b/packages/math/range.js new file mode 100644 index 0000000..0998175 --- /dev/null +++ b/packages/math/range.js @@ -0,0 +1,33 @@ +import {randomNumber} from './math'; + +export class Range { + + constructor(jsonOrValue) { + if ('undefined' === typeof jsonOrValue.min) { + this.min = jsonOrValue; + } + else { + this.min = jsonOrValue.min; + this.max = jsonOrValue.max; + } + } + + static value(rangeOrOther) { + if (rangeOrOther instanceof Range) { + return rangeOrOther.value(); + } + else { + return rangeOrOther; + } + } + + value() { + if ('undefined' === typeof this.max) { + return this.min; + } + else { + return randomNumber(this.min, this.max, false); + } + } + +} diff --git a/packages/math/vector/index.js b/packages/math/vector/index.js index 0d9d285..2950163 100644 --- a/packages/math/vector/index.js +++ b/packages/math/vector/index.js @@ -1,3 +1,6 @@ +import {Range as MathRange} from '../range'; +import {randomNumber} from '../math'; + export {VectorMixin as Mixin} from './mixin'; export const SQRT_2_2 = Math.sqrt(2) / 2; @@ -385,3 +388,19 @@ export function packToUint32(v) { const y = Math.min(65535, Math.max(0, v[1])); return (y << 16) | x; } + +export class Range extends MathRange { + + value() { + if ('undefined' === typeof this.max) { + return this.min; + } + else { + return [ + randomNumber(this.min[0], this.max[0], false), + randomNumber(this.min[1], this.max[1], false), + ]; + } + } + +}