-
Notifications
You must be signed in to change notification settings - Fork 0
/
generation-002.js
70 lines (63 loc) · 1.65 KB
/
generation-002.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
/**
* CHANGELOG:
* Generation 0: implement base script
* Generation 1: implement mandelbrot algorithm and output to console
* Generation 2: add ability to zoom in and out of the mandelbrot set using arrow keys
*/
import { generations, evolve } from "./base.js";
import readline from "readline";
const generation = 2;
console.log(`Generation ${generation} spawned`);
const WIDTH = 90;
const HEIGHT = 30;
const MAX_ITERATIONS = 100;
let zoom = 1;
let offsetX = 0;
let offsetY = 0;
async function output() {
let output = "";
for (let y = 0; y < HEIGHT; y++) {
for (let x = 0; x < WIDTH; x++) {
const cx = (x - WIDTH / 2) * 4 / (WIDTH * zoom) - offsetX;
const cy = (y - HEIGHT / 2) * 4 / (WIDTH * zoom) - offsetY;
let zx = 0;
let zy = 0;
let i = 0;
while (zx * zx + zy * zy < 4 && i < MAX_ITERATIONS) {
const xt = zx * zx - zy * zy + cx;
const yt = 2 * zx * zy + cy;
zx = xt;
zy = yt;
i++;
}
const char = i === MAX_ITERATIONS ? " " : String.fromCharCode(32 + i % 16);
output += char;
}
output += "\n";
}
console.log(output);
}
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on("keypress", (str, key) => {
if (key.name === "up") {
zoom *= 1.1;
} else if (key.name === "down") {
zoom /= 1.1;
} else if (key.name === "left") {
offsetX -= 0.1 / zoom;
} else if (key.name === "right") {
offsetX += 0.1 / zoom;
}
output();
});
await output();
if (generation < generations) {
try {
await evolve(generation);
} catch (error) {
console.error(error);
}
} else {
console.log("GOAL achieved!");
}