forked from rhysd/wain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot.c
53 lines (47 loc) · 1.29 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
// C translation of https://github.com/crystal-lang/crystal/blob/master/samples/mandelbrot.cr
int putchar(int);
void print_density(double d)
{
if (d > 8) {
putchar(' ');
} else if (d > 4) {
putchar('.');
} else if (d > 2) {
putchar('*');
} else {
putchar('+');
}
}
unsigned mandelconverge(double real, double imag)
{
double const creal = real;
double const cimag = imag;
unsigned iters = 0;
while (iters <= 255 && real * real + imag * imag < 4) {
double r = real * real - imag * imag + creal;
double i = 2 * real * imag + cimag;
real = r;
imag = i;
iters++;
}
return iters;
}
void mandel(double realstart, double imagstart, double realmag, double imagmag)
{
double const xmin = realstart;
double const xmax = realstart + realmag * 78;
double const xstep = realmag;
double const ymin = imagstart;
double const ymax = imagstart + imagmag * 40;
double const ystep = imagmag;
for (double y = ymin; y < ymax; y += ystep) {
for (double x = xmin; x < xmax; x += xstep) {
print_density(mandelconverge(x, y));
}
putchar('\n');
}
}
void _start()
{
mandel(-2.3, -1.3, 0.05, 0.07);
}