refactor: math export + range

This commit is contained in:
cha0s 2019-11-03 10:40:08 -06:00
parent 060177f1e8
commit 8701cadbe8
4 changed files with 156 additions and 53 deletions

View File

@ -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';

52
packages/math/math.js Normal file
View File

@ -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;

33
packages/math/range.js Normal file
View File

@ -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);
}
}
}

View File

@ -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),
];
}
}
}