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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
#include <complex.h>
#include <math.h>
#include <ncurses.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define INITIAL_WIDTH 3.2L
#define INITIAL_ITERATIONS 100
#define MAX_ITERATIONS 100000
#define FRAME_DELAY 30000 // in microseconds
#define ZOOM_RATE 0.965L
static const char gradient[] = " .,:;irsXA253hMHGS#9B&@";
struct viewer {
long double center_x;
long double center_y;
long double width;
long double target_x;
long double target_y;
long double zoom_rate;
size_t max_iterations;
bool paused;
bool show_status;
};
static int mandelbrot(long double cr, long double ci, size_t max_iterations);
static char iteration_character(size_t iteration, size_t max_iterations);
static void reset_viewer(struct viewer *viewer);
static void initialize_viewer(struct viewer *viewer);
static void update_zoom(struct viewer *viewer);
static void draw_status(const struct viewer *viewer, int rows, int columns);
static void draw_mandelbrot(const struct viewer *viewer);
static void move_view(struct viewer *viewer, long double dx, long double dy);
static bool handle_input(struct viewer *viewer);
int main(void)
{
struct viewer viewer;
bool running = true;
initialize_viewer(&viewer);
if (initscr() == NULL) {
fprintf(stderr, "Failed to init ncurses");
return -1;
}
if (has_colors()) {
start_color();
use_default_colors(); // keeps terminal background color
// gradient pairs
init_pair(1, COLOR_BLUE, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK);
init_pair(3, COLOR_WHITE, COLOR_BLACK);
}
cbreak();
noecho();
keypad(stdscr, true);
nodelay(stdscr, true);
curs_set(0);
while (running) {
draw_mandelbrot(&viewer);
running = handle_input(&viewer);
update_zoom(&viewer);
usleep(FRAME_DELAY);
}
endwin();
return 0;
}
static int mandelbrot(long double cr, long double ci, size_t max_iterations)
{
long double zr = 0.0L;
long double zi = 0.0L;
size_t iteration;
for (iteration = 0; iteration < max_iterations; iteration++) {
long double zr2 = zr * zr;
long double zi2 = zi * zi;
if (zr2 + zi2 > 4.0) {
break;
}
zi = 2.0 * zr * zi + ci;
zr = zr2 - zi2 + cr;
}
return iteration;
}
static char iteration_character(size_t iteration, size_t max_iterations)
{
size_t gradient_length = sizeof(gradient) - 2;
size_t index;
if (iteration >= max_iterations) {
return ' ';
}
// selecting the character
index = iteration * gradient_length / max_iterations;
if (index > gradient_length) {
index = gradient_length;
}
return gradient[index];
}
static void reset_viewer(struct viewer *viewer)
{
viewer->center_x = 0.0L;
viewer->center_y = 0.0L;
viewer->width = INITIAL_WIDTH;
viewer->max_iterations = INITIAL_ITERATIONS;
}
static void initialize_viewer(struct viewer *viewer)
{
reset_viewer(viewer);
viewer->target_x = -0.743643887037151L;
viewer->target_y = 0.131825904205330L;
viewer->zoom_rate = ZOOM_RATE;
viewer->paused = false;
viewer->show_status = true;
}
static void update_zoom(struct viewer *viewer)
{
if (viewer->paused) {
return;
}
// scales movement magnitude based on zoom rate so camera is always in
// real center,
// i.e. the more zoomed in the smaller movements become
long double movement = 1.0L - viewer->zoom_rate;
viewer->center_x += (viewer->target_x - viewer->center_x) * movement;
viewer->center_y += (viewer->target_y - viewer->center_y) * movement;
viewer->width *= viewer->zoom_rate;
// increases number of iterations as zoom increases to see finer detail
viewer->max_iterations =
INITIAL_ITERATIONS +
(int)(40.0L * logl(INITIAL_WIDTH / viewer->width));
if (viewer->max_iterations > MAX_ITERATIONS)
viewer->max_iterations = MAX_ITERATIONS;
if (viewer->width < 1.0e-16L)
reset_viewer(viewer);
}
static void draw_status(const struct viewer *viewer, int rows, int columns)
{
if (!viewer->show_status || rows < 2) {
return;
}
char status[256];
snprintf(status, sizeof(status),
"center %.18Lf %+.18Lfi width %.3Le iter %zu %s",
viewer->center_x, viewer->center_y, viewer->width,
viewer->max_iterations,
viewer->paused ? "PAUSED" : "AUTO ZOOM");
attron(A_REVERSE);
mvaddnstr(0, 0, status, columns);
// fill rest of row with spaces
for (int column = (int)strlen(status); column < columns; column++) {
mvaddch(0, column, ' ');
}
attroff(A_REVERSE);
}
static void draw_mandelbrot(const struct viewer *viewer)
{
size_t rows;
size_t columns;
getmaxyx(stdscr, rows, columns);
// if status bar is on -> start row is 1 not 0
int start_row = viewer->show_status ? 1 : 0;
if (rows - start_row <= 0 || columns <= 0) {
return;
}
long double height = viewer->width * (long double)(rows - start_row) /
(long double)columns * 2.0L;
erase();
for (int screen_y = start_row; screen_y < rows; screen_y++) {
long double normalized_y = (long double)(screen_y - start_row) /
(long double)(rows - start_row - 1);
long double ci =
viewer->center_y + (normalized_y - 0.5L) * height;
for (int screen_x = 0; screen_x < columns; screen_x++) {
long double normalized_x =
(long double)screen_x / (long double)(columns - 1);
long double cr = viewer->center_x +
(normalized_x - 0.5L) * viewer->width;
int iteration =
mandelbrot(cr, ci, viewer->max_iterations);
char pixel = iteration_character(
iteration, viewer->max_iterations);
int color_pair = 0;
if (iteration < viewer->max_iterations) {
// Group iterations into bands of 8 for a smooth
// ripple effect
int band = (iteration / 8) % 3;
if (band == 0)
color_pair = 1; // Deep Blue
else if (band == 1)
color_pair = 2; // Bright Cyan
else
color_pair = 3; // White highlight
}
// Apply color, draw pixel, remove color
if (color_pair > 0)
attron(COLOR_PAIR(color_pair));
mvaddch(screen_y, screen_x, pixel);
if (color_pair > 0)
attroff(COLOR_PAIR(color_pair));
}
}
draw_status(viewer, rows, columns);
refresh();
}
static void move_view(struct viewer *viewer, long double dx, long double dy)
{
long double height = viewer->width * 0.5L;
viewer->center_x += viewer->width * dx;
viewer->center_y += height * dy;
viewer->target_x += viewer->width * dx;
viewer->target_y += height * dy;
}
static bool handle_input(struct viewer *viewer)
{
int key = getch();
switch (key) {
case 'q':
case 'Q':
return false;
case ' ':
case 'p':
case 'P':
viewer->paused = !viewer->paused;
break;
case 'r':
case 'R':
reset_viewer(viewer);
break;
case 's':
case 'S':
viewer->show_status = !viewer->show_status;
break;
case '+':
case '=':
viewer->width *= 0.8L;
break;
case '-':
case '_':
viewer->width *= 1.25L;
break;
case KEY_LEFT:
case 'h':
move_view(viewer, -0.1L, 0.0L);
break;
case KEY_RIGHT:
case 'l':
move_view(viewer, 0.1L, 0.0L);
break;
case KEY_UP:
case 'k':
move_view(viewer, 0.0L, -0.1L);
break;
case KEY_DOWN:
case 'j':
move_view(viewer, 0.0L, 0.1L);
break;
case '[':
viewer->zoom_rate -= 0.005L;
if (viewer->zoom_rate < 0.8L)
viewer->zoom_rate = 0.8L;
break;
case ']':
viewer->zoom_rate += 0.005L;
if (viewer->zoom_rate > 0.999L)
viewer->zoom_rate = 0.999L;
break;
default:
break;
}
return true;
}
|