-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·318 lines (277 loc) · 8.11 KB
/
index.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { createCanvas } from "canvas";
import path from "path";
import sharp from "sharp";
import {
createProgressUpdater,
floorToFirstDecimal,
floorToMultiple,
opacityToHex,
randFromSeed,
randomString,
} from "./utlis.js";
const modifiers = {
noop: (v) => v,
sin: Math.sin,
cos: Math.cos,
sqrt: Math.sqrt,
cbrt: Math.cbrt,
log: Math.log,
asinh: Math.asinh,
atan: Math.atan,
};
/**
* Computes the next coordinate in a system
* as defined by a 2D quadratic function
* @param {*} x Current x coordinate
* @param {*} y Current y coordinate
* @param {*} ax a parameters of the system
* @param {*} ay b parameters of the system
* @param {*} xFn function to apply to x coordinate
* @param {*} yFn function to apply to y coordinate
* @returns The next coordinate [x, y]
*/
function attractor(x, y, ax, ay, xFn = (v) => v, yFn = (v) => v) {
return [
ax[0] +
ax[1] * xFn(x) +
ax[2] * xFn(x) * xFn(x) +
ax[3] * xFn(x) * yFn(y) +
ax[4] * yFn(y) +
ax[5] * yFn(y) * yFn(y),
ay[0] +
ay[1] * xFn(x) +
ay[2] * xFn(x) * xFn(x) +
ay[3] * xFn(x) * yFn(y) +
ay[4] * yFn(y) +
ay[5] * yFn(y) * yFn(y),
];
}
function createParams(rand) {
const ax = [];
const ay = [];
for (let i = 0; i < 6; i++) {
ax[i] = 4 * (rand() - 0.5);
ay[i] = 4 * (rand() - 0.5);
}
return { ax, ay };
}
function generateAttractor(params, n, mods, report = true) {
const { ax, ay, x0, y0 } = params;
const points = [[x0, y0]];
const updateProgress = createProgressUpdater("generating", n);
for (let i = 1; i < n; i++) {
points[i] = attractor(
points[i - 1][0],
points[i - 1][1],
ax,
ay,
mods[0],
mods[1]
);
if (report) updateProgress(i);
}
return points;
}
function computeLyapunov(params, mods) {
const lyapunovStart = 1000;
const lyapunovEnd = 50000;
const points = generateAttractor(params, lyapunovEnd, mods, false);
const { xMin, xMax, yMin, yMax } = computeBounds(points);
let lyapunov = 0;
let dRand = randFromSeed("disturbance");
let d0, xe, ye;
do {
xe = points[0][0] + (dRand() - 0.5) / 1000.0;
ye = points[0][1] + (dRand() - 0.5) / 1000.0;
const dxe = points[0][0] - xe;
const dye = points[0][1] - ye;
d0 = Math.sqrt(dxe * dxe + dye * dye);
} while (d0 <= 0);
if (
xMin < -1e10 ||
yMin < -1e10 ||
xMax > 1e10 ||
yMax > 1e10 ||
Number.isNaN(xMin) ||
Number.isNaN(xMax) ||
Number.isNaN(yMin) ||
Number.isNaN(yMax)
) {
// attracted towards infinity
return false;
}
for (let i = 1; i < lyapunovEnd; i++) {
const dx = points[i][0] - points[i - 1][0];
const dy = points[i][1] - points[i - 1][1];
if (Math.abs(dx) < 1e-10 && Math.abs(dy) < 1e-10) {
// attracted towards a single point
return false;
}
if (i > lyapunovStart) {
const [newXe, newYe] = attractor(
xe,
ye,
params.ax,
params.ay,
mods[0],
mods[1]
);
const dxe = points[i][0] - newXe;
const dye = points[i][1] - newYe;
const dd = Math.sqrt(dxe * dxe + dye * dye);
lyapunov += Math.log(Math.abs(dd / d0));
xe = points[i][0] + (d0 * dxe) / dd;
ye = points[i][1] + (d0 * dye) / dd;
}
}
if (Math.abs(lyapunov) < 10) {
// neutral stable attractor
return false;
} else if (lyapunov < 0) {
// periodic attractor
return false;
}
return true;
}
function computeBounds(points) {
let xMin = Number.MAX_VALUE;
let xMax = Number.MIN_VALUE;
let yMin = Number.MAX_VALUE;
let yMax = Number.MIN_VALUE;
for (let i = 0; i < points.length; i++) {
xMin = Math.min(xMin, points[i][0]);
yMin = Math.min(yMin, points[i][1]);
xMax = Math.max(xMax, points[i][0]);
yMax = Math.max(yMax, points[i][1]);
}
return { xMin, xMax, yMin, yMax };
}
function computeSpread(points, bounds, report = true) {
const { xMin, xMax, yMin, yMax } = bounds;
const cells = {};
// divide the smallest side in at least 100 cells
const minSubdivision = 100;
const minLength = Math.min(Math.abs(xMax - xMin), Math.abs(yMax - yMin));
const cellSize = floorToFirstDecimal(minLength / minSubdivision);
// find the number of columns/rows
const startX = floorToMultiple(xMin, cellSize);
const endX = floorToMultiple(xMax, cellSize);
const startY = floorToMultiple(yMin, cellSize);
const endY = floorToMultiple(yMax, cellSize);
const gridWidth = Math.abs(endX - startX);
const gridHeight = Math.abs(endY - startY);
const cols = Math.round(gridWidth / cellSize);
const rows = Math.round(gridHeight / cellSize);
// analyze maximum 1M first points
// spread does not increase much beyond that
const n = Math.min(points.length, 1000000);
const updateProgress = createProgressUpdater("analyzing", n);
for (let i = 0; i < n; i++) {
const cellX = floorToMultiple(points[i][0], cellSize);
const cellY = floorToMultiple(points[i][1], cellSize);
const cellKey = `${cellX}_${cellY}`;
if (!cells[cellKey]) {
cells[cellKey] = 1;
}
if (report) updateProgress(i);
}
const spread = Object.keys(cells).length / (cols * rows);
return spread;
}
function draw(context, points, bounds, settings, report = true) {
const { xMin, xMax, yMin, yMax } = bounds;
const { color, background, width, height, marginRatio, opacity } = settings;
context.fillStyle = background;
context.fillRect(0, 0, width, height);
const margin = width * marginRatio;
const attractorWidth = xMax - xMin;
const attractorHeight = yMax - yMin;
const scale = Math.min(
(width - margin) / attractorWidth,
(height - margin) / attractorHeight
);
const centerX = (width - attractorWidth * scale) / 2;
const centerY = (height - attractorHeight * scale) / 2;
context.fillStyle = `${color}${opacityToHex(opacity)}`;
const updateProgress = createProgressUpdater("drawing", points.length);
for (let i = 0; i < points.length; i++) {
let ix = centerX + (points[i][0] - xMin) * scale;
let iy = centerY + (points[i][1] - yMin) * scale;
context.fillRect(ix, iy, 1, 1);
if (report) updateProgress(i);
}
}
/**
* Renders an attractor as a PNG file on the disk given that provided
* attractor parameters exhibits chaotic behavior.
*/
export function render(seed, settings, report = true) {
const {
pointCount,
xMod,
yMod,
width,
height,
output,
quality,
spreadFilter,
} = settings;
const rand = randFromSeed(seed);
const params = createParams(rand);
const mods = [modifiers[xMod], modifiers[yMod]];
const x0 = rand() - 0.5;
const y0 = rand() - 0.5;
if (!computeLyapunov({ ...params, x0, y0 }, mods)) {
return;
}
console.log(`seed: ${seed}\tmods: ${xMod}/${yMod}`);
const points = generateAttractor(
{ ...params, x0, y0 },
pointCount,
mods,
report
);
const bounds = computeBounds(points);
// sometimes non-chaotic properties only show after generation
if (
isNaN(bounds.xMin) ||
isNaN(bounds.xMax) ||
isNaN(bounds.yMin) ||
isNaN(bounds.yMax)
) {
return;
}
const spread = computeSpread(points, bounds, report);
// filter out attractor below spread factor
if (spread < spreadFilter) {
return;
}
const canvas = createCanvas(width, height);
const context = canvas.getContext("2d");
draw(context, points, bounds, settings, report);
const outputDir = path.resolve(process.cwd(), output);
const outputFile = path.join(outputDir, `${seed}_${xMod}_${yMod}.png`);
const buffer = canvas.toBuffer("image/png");
sharp(buffer).removeAlpha().png({ quality }).toFile(outputFile);
}
/**
* Function to "mine" the attractor algorithm by first generating
* a seed and then trying multiple combinations of x/y modifiers to
* maximize the exploration of possible outputs.
*/
export function mine(settings) {
const modNames = Object.keys(modifiers);
const modCombos = [];
modNames.forEach((i1) => {
modNames.forEach((i2) => {
modCombos.push([i1, i2]);
});
});
while (true) {
const seed = randomString(8);
modCombos.forEach(([xMod, yMod]) => {
const sx = { ...settings, xMod, yMod };
render(seed, sx, false);
});
}
}