refactor: math export + range
This commit is contained in:
parent
060177f1e8
commit
8701cadbe8
|
@ -1,6 +1,7 @@
|
||||||
// import * as Matrix from './matrix';
|
// import * as Matrix from './matrix';
|
||||||
// export {Matrix}
|
// export {Matrix}
|
||||||
export {QuadTree} from './quadtree';
|
export {QuadTree} from './quadtree';
|
||||||
|
export {Range} from './range';
|
||||||
import * as Rectangle from './rectangle';
|
import * as Rectangle from './rectangle';
|
||||||
export {Rectangle}
|
export {Rectangle}
|
||||||
export {SimpleMovingAverage} from './sma';
|
export {SimpleMovingAverage} from './sma';
|
||||||
|
@ -9,56 +10,54 @@ export {Vector}
|
||||||
import * as Vertice from './vertice';
|
import * as Vertice from './vertice';
|
||||||
export {Vertice}
|
export {Vertice}
|
||||||
|
|
||||||
export const abs = Math.abs;
|
export {
|
||||||
export const acos = Math.acos;
|
abs,
|
||||||
export const acosh = Math.acosh;
|
acos,
|
||||||
export const asin = Math.asin;
|
acosh,
|
||||||
export const asinh = Math.asinh;
|
asin,
|
||||||
export const atan = Math.atan;
|
asinh,
|
||||||
export const atanh = Math.atanh;
|
atan,
|
||||||
export const atan2 = Math.atan2;
|
atanh,
|
||||||
export const ceil = Math.ceil;
|
atan2,
|
||||||
export const cbrt = Math.cbrt;
|
ceil,
|
||||||
export const expm1 = Math.expm1;
|
cbrt,
|
||||||
export const clz32 = Math.clz32;
|
expm1,
|
||||||
export const cos = Math.cos;
|
clz32,
|
||||||
export const cosh = Math.cosh;
|
cos,
|
||||||
export const exp = Math.exp;
|
cosh,
|
||||||
export const floor = Math.floor;
|
exp,
|
||||||
export const fround = Math.fround;
|
floor,
|
||||||
export const hypot = Math.hypot;
|
fround,
|
||||||
export const imul = Math.imul;
|
hypot,
|
||||||
export const log = Math.log;
|
imul,
|
||||||
export const log1p = Math.log1p;
|
log,
|
||||||
export const log2 = Math.log2;
|
log1p,
|
||||||
export const log10 = Math.log10;
|
log2,
|
||||||
export const max = Math.max;
|
log10,
|
||||||
export const min = Math.min;
|
max,
|
||||||
export const pow = Math.pow;
|
min,
|
||||||
export const random = Math.random;
|
pow,
|
||||||
export const round = Math.round;
|
random,
|
||||||
export const sign = Math.sign;
|
round,
|
||||||
export const sin = Math.sin;
|
sign,
|
||||||
export const sinh = Math.sinh;
|
sin,
|
||||||
export const sqrt = Math.sqrt;
|
sinh,
|
||||||
export const tan = Math.tan;
|
sqrt,
|
||||||
export const tanh = Math.tanh;
|
tan,
|
||||||
export const trunc = Math.trunc;
|
tanh,
|
||||||
export const E = Math.E;
|
trunc,
|
||||||
export const LN10 = Math.LN10;
|
E,
|
||||||
export const LN2 = Math.LN2;
|
LN10,
|
||||||
export const LOG10E = Math.LOG10E;
|
LN2,
|
||||||
export const LOG2E = Math.LOG2E;
|
LOG10E,
|
||||||
export const PI = Math.PI;
|
LOG2E,
|
||||||
export const SQRT1_2 = Math.SQRT1_2;
|
PI,
|
||||||
export const SQRT2 = Math.SQRT2;
|
SQRT1_2,
|
||||||
|
SQRT2,
|
||||||
export const add = (l, r) => l + r;
|
add,
|
||||||
export const div = (l, r) => l / r;
|
div,
|
||||||
export const frac = (number) => number % 1;
|
frac,
|
||||||
export const mul = (l, r) => l * r;
|
mul,
|
||||||
export const randomNumber = (min, max, floor = true) => {
|
randomNumber,
|
||||||
const mag = Math.random() * ((max - min) + 1);
|
sub,
|
||||||
return min + (floor ? Math.floor(mag) : mag);
|
} from './math';
|
||||||
};
|
|
||||||
export const sub = (l, r) => l - r;
|
|
||||||
|
|
52
packages/math/math.js
Normal file
52
packages/math/math.js
Normal 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
33
packages/math/range.js
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,3 +1,6 @@
|
||||||
|
import {Range as MathRange} from '../range';
|
||||||
|
import {randomNumber} from '../math';
|
||||||
|
|
||||||
export {VectorMixin as Mixin} from './mixin';
|
export {VectorMixin as Mixin} from './mixin';
|
||||||
|
|
||||||
export const SQRT_2_2 = Math.sqrt(2) / 2;
|
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]));
|
const y = Math.min(65535, Math.max(0, v[1]));
|
||||||
return (y << 16) | x;
|
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),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user