-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
172 lines (142 loc) · 4.77 KB
/
util.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { Point, Rect } from "./types";
import { vadd, vm, vm2, vm3, vmul, vscale, vsub } from "./vutil";
export function mapval<T, U>(m: { [k: string]: T }, f: (x: T, k: string) => U): { [k: string]: U } {
return Object.fromEntries(Object.entries(m).map(([k, v]) => [k, f(v, k)]));
}
export class Rand {
n: number;
constructor(n?: number) { this.n = n || 42; for (let i = 0; i < 3; i++) this.f(); }
f(): number {
this.n = (2147483629 * this.n + 2147483587) % 2147483647;
return (this.n & 0xffff) / (1 << 16);
}
i(n: number): number {
return Math.floor(this.f() * n);
}
}
export function zeropad(str: string, length: number): string {
return repeat('0', Math.max(0, length - str.length)) + str;
}
export function repeat(x: string, n: number) {
if (n < 0)
throw 'negative repeat';
let s = '';
for (; ;) {
if (n & 1) s += x;
n >>= 1;
if (n) x += x;
else break;
}
return s;
}
export const int = Math.floor;
export function lerp(a: number, b: number, t: number) {
return a + (b - a) * t;
}
// multiplicative interpolation
export function mlerp(a: number, b: number, t: number) {
return Math.exp(lerp(Math.log(a), Math.log(b), t));
}
export function unreachable<T>(_x: never): T {
throw new Error('unreachable');
}
export function filterKeys<T>(rec: Record<string, T>, pred: (x: string) => boolean): Record<string, T> {
return Object.fromEntries(
Object.entries(rec)
.filter(([k, _v]) => pred(k))
);
}
// RtlUniform from Native API[13] from
// https://en.wikipedia.org/wiki/Linear_congruential_generator
export function next_rand(seed: number): { seed: number, float: number } {
const next = (2147483629 * seed + 2147483587) % 2147483647;
return { seed: next, float: (next & 0xffff) / (1 << 16) };
}
export function boundRect(points: Point[]): Rect {
const xs = points.map(p => p.x);
const ys = points.map(p => p.y);
const mins = { x: Math.min(...xs), y: Math.min(...ys) };
const maxs = { x: Math.max(...xs), y: Math.max(...ys) };
return { p: mins, sz: vsub(maxs, mins) };
}
export function pointInRect(p: Point, r: Rect): boolean {
return p.x >= r.p.x && p.y >= r.p.y && p.x <= r.p.x + r.sz.x && p.y <= r.p.y + r.sz.y;
}
export function point_hash(p: Point, seed: number): number {
let c = seed + 1000 * p.x + 3758 * p.y;
for (let i = 0; i < 20; i++) {
const { seed } = next_rand(c);
c = seed;
};
return next_rand(c).float;
}
export function midpointOfRect(rect: Rect): Point {
return vm2(rect.p, rect.sz, (p, s) => p + s / 2);
}
export function scaleRectToCenter(rect: Rect, s: number): Rect {
return {
p: vm2(rect.p, rect.sz, (p, sz) => (s * (p - (p + sz / 2)) + (p + sz / 2))),
sz: vscale(rect.sz, s),
}
}
export function scaleRectToCenterPoint(rect: Rect, sf: Point): Rect {
return {
p: vm3(rect.p, rect.sz, sf, (p, sz, s) => (s * (p - (p + sz / 2)) + (p + sz / 2))),
sz: vmul(rect.sz, sf),
}
}
export function insetRect(rect: Rect, i: number): Rect {
return {
p: vm(rect.p, p => p + i),
sz: vm(rect.sz, sz => sz - 2 * i),
}
}
// Returns a random permutation of [0,...,length-1].
export function getRandomOrder(length: number): number[] {
const rv: [number, number][] = [];
for (let i = 0; i < length; i++) {
rv.push([i, Math.random()]);
}
rv.sort((a, b) => a[1] - b[1]);
return rv.map(a => a[0]);
}
export function range(n: number): number[] {
return [...new Array(n)].map((_a, b) => b);
}
export function randPointInRect(rect: Rect): Point {
return vm2(rect.p, rect.sz, (p, sz) => Math.random() * sz + p);
}
export async function grab(path: string): Promise<string> {
const resp = await fetch(path, { cache: "no-cache" });
if (resp.status != 200)
throw new Error(`Couldn't load ${path}, status code ${resp.status}`);
return await resp.text();
}
export function rectPts(rect: Rect): [Point, Point] {
return [rect.p, vadd(rect.p, rect.sz)];
}
export function allRectPts(rect: Rect): Point[] {
return [rect.p, { x: rect.p.x + rect.sz.x, y: rect.p.y }, vadd(rect.p, rect.sz), { x: rect.p.x, y: rect.p.y + rect.sz.y }];
}
// Gives a rect covering the same area but "negatively", so that
// allRectPts(invertRect(r)) = allRectPts(r).reverse()
export function invertRect(rect: Rect): Rect {
return { p: { x: rect.p.x + rect.sz.x, y: rect.p.y }, sz: { x: -rect.sz.x, y: rect.sz.y } }
}
export function pixelSnap(p: Point): Point {
return vm(p, x => Math.floor(x * devicePixelRatio) / devicePixelRatio);
}
export function pixelSnapRect(r: Rect): Rect {
return { p: pixelSnap(r.p), sz: pixelSnap(r.sz) };
}
export function fpart(x: number): number {
return x - Math.floor(x);
}
export function flatUndef<A>(xs: (undefined | A)[]): undefined | A[] {
const rv: A[] = [];
for (const x of xs) {
if (x == undefined) return undefined;
rv.push(x);
}
return rv;
}