-
Notifications
You must be signed in to change notification settings - Fork 11
/
genGrid.ts
57 lines (48 loc) · 1.24 KB
/
genGrid.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function closeToZero( a: number ): boolean {
return Math.abs( a ) < 1E-9;
}
function mod( x: number, d: number ): number {
return x - Math.floor( x / d ) * d;
}
interface GridEntry {
value: number;
importance: number;
}
export function genGrid(
t0: number,
t1: number,
options: {
base?: number;
details?: number;
} = {}
): GridEntry[] {
const base = options.base ?? 10;
const details = options.details ?? 0;
const delta = t1 - t0;
const logDelta = Math.log( delta ) / Math.log( base );
const div = Math.pow( base, Math.floor( logDelta ) - details );
const begin = Math.ceil( t0 / div ) * div;
const result: GridEntry[] = [];
let value = begin;
let i = 0;
while ( value <= t1 ) {
let importanceRaw = div;
if ( closeToZero( value ) ) {
importanceRaw = Infinity;
} else {
let d = ( mod( value + 0.5 * importanceRaw, importanceRaw ) ) - 0.5 * importanceRaw;
while ( closeToZero( d ) ) {
importanceRaw *= base;
d = ( mod( value + 0.5 * importanceRaw, importanceRaw ) ) - 0.5 * importanceRaw;
}
importanceRaw /= base;
}
result.push( {
value,
importance: importanceRaw / delta
} );
i ++;
value = begin + i * div;
}
return result;
}