-
Notifications
You must be signed in to change notification settings - Fork 0
/
generation-005.js
114 lines (99 loc) · 3.14 KB
/
generation-005.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
/**
* CHANGELOG:
* Generation 1: implement Mandelbrot algorithm
* Generation 2: add color to the Mandelbrot set
* Generation 3: add zoom functionality to the Mandelbrot set
* Generation 4: add ability to move the Mandelbrot set
* Generation 5: add ability to change the number of iterations
*/
import { generations, evolve } from "./base.js";
import readline from "readline";
const generation = 5;
async function output() {
const width = 90;
const height = 30;
let xMin = -2;
let xMax = 1;
let yMin = -1;
let yMax = 1;
let maxIterations = 100;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter zoom level (1-10): ", (zoomLevel) => {
if (zoomLevel >= 1 && zoomLevel <= 10) {
const zoomFactor = Math.pow(2, zoomLevel - 5);
xMin /= zoomFactor;
xMax /= zoomFactor;
yMin /= zoomFactor;
yMax /= zoomFactor;
rl.question("Enter x offset (-1 to 1): ", (xOffset) => {
if (xOffset >= -1 && xOffset <= 1) {
const xRange = xMax - xMin;
xMin += xRange * xOffset;
xMax += xRange * xOffset;
rl.question("Enter y offset (-1 to 1): ", (yOffset) => {
if (yOffset >= -1 && yOffset <= 1) {
const yRange = yMax - yMin;
yMin += yRange * yOffset;
yMax += yRange * yOffset;
rl.question("Enter number of iterations (1-1000): ", (iterations) => {
if (iterations >= 1 && iterations <= 1000) {
maxIterations = iterations;
let outputString = "";
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const a = xMin + (xMax - xMin) * x / (width - 1);
const b = yMin + (yMax - yMin) * y / (height - 1);
let real = 0;
let imaginary = 0;
let iteration = 0;
while (real * real + imaginary * imaginary <= 4 && iteration < maxIterations) {
const tempReal = real * real - imaginary * imaginary + a;
const tempImaginary = 2 * real * imaginary + b;
real = tempReal;
imaginary = tempImaginary;
iteration++;
}
if (iteration === maxIterations) {
outputString += " ";
} else {
const color = iteration % 16;
const asciiIndex = Math.floor(iteration / (maxIterations / 10));
outputString += `\x1b[38;5;${color}m${String.fromCharCode(48 + asciiIndex)}\x1b[0m`;
}
}
outputString += "\n";
}
console.log(outputString);
} else {
console.log("Invalid number of iterations. Please enter a number between 1 and 1000.");
}
rl.close();
});
} else {
console.log("Invalid y offset. Please enter a number between -1 and 1.");
rl.close();
}
});
} else {
console.log("Invalid x offset. Please enter a number between -1 and 1.");
rl.close();
}
});
} else {
console.log("Invalid zoom level. Please enter a number between 1 and 10.");
rl.close();
}
});
}
if (generation < generations) {
try {
await evolve(generation);
} catch (error) {
console.error(error);
}
} else {
await output();
}