-
Notifications
You must be signed in to change notification settings - Fork 0
/
generation-004.js
117 lines (102 loc) · 4.04 KB
/
generation-004.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
/**
* CHANGELOG:
* Generation 0: implement base script
* Generation 1: add Mandelbrot algorithm
* Generation 2: add user-defined coordinates range and max iterations
* Generation 3: add optional Color Scheme
* Generation 4: add zooming feature
*/
import { generations, evolve } from "./base.js";
const generation = 4;
console.log(`Generation ${generation} spawned`);
// Define a function to get user input
async function getInput(prompt) {
const { stdin, stdout } = process;
return new Promise(resolve => {
stdin.resume();
stdout.write(prompt);
stdin.on('data', data => resolve(data.toString().trim()));
});
}
async function output() {
// Define constants based on the output specs
const WIDTH = 90;
const HEIGHT = 30;
// Get user-defined coordinates range and max iterations, or use default values
let xmin = parseFloat(await getInput('Please enter the minimum x coordinate value: ')) || -2;
let xmax = parseFloat(await getInput('Please enter the maximum x coordinate value: ')) || 1;
let ymin = parseFloat(await getInput('Please enter the minimum y coordinate value: ')) || -1;
let ymax = parseFloat(await getInput('Please enter the maximum y coordinate value: ')) || 1;
let MAX_ITERATIONS = parseInt(await getInput('Please enter the maximum number of iterations (integer): ')) || 100;
const USE_COLOR_SCHEME = (await getInput('Do you want to use a color scheme? (y/n): ')).toLowerCase() === 'y';
// Create an empty matrix of the required dimensions (90x30) (initial matrix)
let matrix = Array(HEIGHT).fill().map(() => Array(WIDTH).fill(' '));
while(true) {
// Split the window into grid
const gridXsize = (xmax - xmin) / WIDTH;
const gridYsize = (ymax - ymin) / HEIGHT;
// Iterate over each point in the matrix
for (let y = 0; y < HEIGHT; y++) {
for (let x = 0; x < WIDTH; x++) {
// Convert matrix-coordinates to real numbers
const a = xmin + (x / (WIDTH-1)) * (xmax - xmin);
const b = ymax - (y / (HEIGHT-1)) * (ymax - ymin);
let i = 0;
let za = 0;
let zb = 0;
// Iterate calculations until it exits the "bounding circle"
while (i < MAX_ITERATIONS && (za * za + zb * zb) < 4) {
const temp = za * za - zb * zb + a;
zb = 2 * za * zb + b;
za = temp;
i++;
}
let char = '';
if (USE_COLOR_SCHEME) {
// Get the red, green and blue components of the color based on the number of iterations MAX_ITERATIONS
const red = Math.floor(i % 16 * 16);
const green = Math.floor(i % 32 * 8);
const blue = Math.floor(i % 64 * 4);
// Assign the corresponding hexadecimal color value to the char variable
char = `\x1b[48;2;${red};${green};${blue}m \x1b[0m`;
} else {
// Assign an ascii character to the corresponding matrix-coordinate according to the number of iterations MAX_ITERATIONS if no color scheme is chosen
char = i === MAX_ITERATIONS ? '*' : String.fromCharCode(32 + (i % 15));
}
matrix[y][x] = char;
}
}
// Log matrix to the console using nested loops
for (let y = 0; y < matrix.length; y++) {
let row = '';
for (let x = 0; x < matrix[y].length; x++) {
row += matrix[y][x];
}
console.log(row);
}
const zoomIn = await getInput('Do you want to zoom in? (y/n): ');
if (zoomIn.toLowerCase() !== 'y') {
break;
} else {
// Get user-defined coordinates range and max iterations, or use default values
const xmid = xmin + (xmax - xmin) / 2;
const ymid = ymin + (ymax - ymin) / 2;
xmin = xmid - gridXsize * WIDTH / 4;
xmax = xmid + gridXsize * WIDTH / 4;
ymin = ymid - gridYsize * HEIGHT / 4;
ymax = ymid + gridYsize * HEIGHT / 4;
MAX_ITERATIONS *= 2;
matrix = Array(HEIGHT).fill().map(() => Array(WIDTH).fill(' '));
}
}
}
await output();
if (generation < generations) {
try {
await evolve(generation);
} catch (error) {
console.error(error);
}
} else {
console.log("GOAL achieved!");
}