-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (56 loc) · 1.57 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
const ratio = 2;
const scrWidth = 2000;
const scrHeight = scrWidth / ratio;
let x = -2;
let y = -0.5;
let width = 6;
let height = 2;
function setup() {
createCanvas(scrWidth, scrHeight);
background(50);
redraw__();
}
const approximatelyEquals = (a, b, epsilon) => Math.abs(a - b) < epsilon;
const getNextGenPopulation = (r, population) => {
return r * population * (1 - population);
};
const getStablePoints = (r) => {
const populations = [0.5];
while (true) {
const nextGenPopulation = getNextGenPopulation(r, populations[populations.length - 1]);
const periodStartIndex = populations.findIndex((value) =>
approximatelyEquals(value, nextGenPopulation, Number.EPSILON)
);
if (periodStartIndex !== -1) {
const period = populations.slice(periodStartIndex);
return period;
} else if (populations.length > 1000) {
const period = populations.slice(900);
return period;
}
populations.push(nextGenPopulation);
}
};
const redraw__ = () => {
background(50);
stroke(255);
for (let pixelX = 0; pixelX < scrWidth; pixelX++) {
const r = map(pixelX, 0, scrWidth, x, x + width);
const stablePoints = getStablePoints(r);
for (const stablePoint of stablePoints) {
const pixelY = map(stablePoint, y, y + height, scrHeight, 0);
point(pixelX, pixelY);
}
}
};
function mousePressed() {
const zoomRatio = 2;
const mappedX = map(mouseX, 0, scrWidth, x, x + width);
const mappedY = map(mouseY, 0, scrHeight, y + height, y);
console.log(mappedX, mappedY);
width /= zoomRatio;
height /= zoomRatio;
x = mappedX - width / 2;
y = mappedY - height / 2;
redraw__();
}