-
Notifications
You must be signed in to change notification settings - Fork 6
/
mandelbrot.c
101 lines (86 loc) · 1.91 KB
/
mandelbrot.c
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
/*
Computes and displays the Mandelbrot set in the terminal using
ANSI codes.
Bruno Levy, 2020
*/
#include <stdio.h>
#ifdef __linux__
#include <unistd.h>
#else
#include "io.h"
#endif
#define W 46
#define H 46
#define mandel_shift 10
#define mandel_mul (1 << mandel_shift)
#define xmin -2*mandel_mul
#define ymax 2*mandel_mul
#define ymin -2*mandel_mul
#define xmax 2*mandel_mul
#define dx (xmax-xmin)/H
#define dy (ymax-ymin)/H
#define norm_max (4 << mandel_shift)
#define ANSIRGB(R,G,B) "\033[48;2;" #R ";" #G ";" #B "m "
const char* colormap[21] = {
ANSIRGB( 0, 0, 0),
ANSIRGB( 0, 0, 40),
ANSIRGB( 0, 0, 80),
ANSIRGB( 0, 0,120),
ANSIRGB( 0, 0,160),
ANSIRGB( 0, 0,200),
ANSIRGB( 0, 0,240),
ANSIRGB( 0, 0, 0),
ANSIRGB( 0, 40, 0),
ANSIRGB( 0, 80, 0),
ANSIRGB( 0,120, 0),
ANSIRGB( 0,160, 0),
ANSIRGB( 0,200, 0),
ANSIRGB( 0,240, 0),
ANSIRGB( 0, 0, 0),
ANSIRGB( 40, 0, 0),
ANSIRGB( 80, 0, 0),
ANSIRGB( 120, 0, 0),
ANSIRGB( 160, 0, 0),
ANSIRGB( 200, 0, 0),
ANSIRGB( 240, 0, 0)
};
int main() {
int frame=0;
for(;;) {
// IO_OUT(IO_LEDS,frame);
int last_color = -1;
printf("\033[H");
int Ci = ymin;
for(int Y=0; Y<H; ++Y) {
int Cr = xmin;
for(int X=0; X<W; ++X) {
int Zr = Cr;
int Zi = Ci;
int iter = 20;
while(iter > 0) {
int Zrr = (Zr * Zr) >> mandel_shift;
int Zii = (Zi * Zi) >> mandel_shift;
int Zri = (Zr * Zi) >> (mandel_shift - 1);
Zr = Zrr - Zii + Cr;
Zi = Zri + Ci;
if(Zrr + Zii > norm_max) {
break;
}
--iter;
}
int color = (iter+frame)%21;
printf("%s", color == last_color ? " " : colormap[color]);
last_color = color;
Cr += dx;
}
Ci += dy;
printf("\033[49m\n");
last_color = -1;
}
++frame;
#ifdef __linux__
usleep(100000);
#endif
// if(frame>4) break;
}
}