-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmandelbrot.c
55 lines (42 loc) · 1.31 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
#include <complex.h>
#include <stdlib.h>
#include "graph.h"
#include "mandelbrot.h"
size_t
choose_escape_color(const complex double c, const size_t maximum_iterations)
{
size_t escape = 0;
complex double z = 0.0 + I * 0.0;
for (complex double temp; escape < maximum_iterations; escape++) {
temp = cpow(z, 2);
if (cabs(temp) > 2)
break;
z = temp + c;
}
if (escape == maximum_iterations)
escape = 0;
if (escape == 0)
return 0;
else if (escape <= (maximum_iterations / 7))
return 1;
else if (escape <= (maximum_iterations / 5))
return 2;
else
return 3;
return escape;
}
void mandelbrot(const char *backend, const char *outputfile,
const size_t width, const size_t height, const size_t iterations,
const complex double center, const double range)
{
graph_t image = graph_create(backend, width, height, center, range);
for (size_t i = 0; i < image.width; i++) {
for (size_t j = 0; j < image.height; j++) {
complex double c = graph_get_coordinates(image, i, j);
size_t colormap_entry = choose_escape_color(c, iterations);
graph_set_pixel(image, i, j, colormap_entry);
}
}
graph_write(image, outputfile);
graph_destroy(image);
}