-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot.c
91 lines (80 loc) · 1.84 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
/*
miniFB Mandelbrot set example by 0kalekale
*/
#include <MiniFB.h>
#include <stdio.h>
#include <stdint.h>
#include <complex.h>
static uint32_t g_width = 600;
static uint32_t g_height = 600;
static uint32_t *g_buffer = 0x0;
void
resize(struct mfb_window *window, int width, int height) {
(void) window;
g_width = width;
g_height = height;
g_buffer = realloc(g_buffer, g_width * g_height * 4);
}
int
iter(double complex z) {
int it = 0;
double complex c = z;
while (cabs(z)<=4&&(it<30)) {
z = (z*z)+c;
it++;
}
return 30-it;
}
int
main()
{
struct mfb_window *window = mfb_open_ex("Mandelbrot Set", g_width, g_height, NULL);
if (!window)
return 0;
g_buffer = (uint32_t *) malloc(g_width * g_height * 4);
mfb_set_viewport(window, 50, 50, g_width - 50 - 50, g_height - 50 - 50);
resize(window, g_width - 100, g_height - 100); // to resize buffer
mfb_update_state state;
do {
int i = 0;
for (int x = 0; x< g_width; x++) {
for (int y = 0; y< g_height; y++) {
double re = (double)(2*(y-300))/(double)300;
double im = (double)(2*(x-300))/(double)300;
double complex z = CMPLX(re, im);
int it = iter(z);
if (it == 0) {
g_buffer[i] = MFB_RGB(0x00, 0x00, 0x00);
}
else {
switch(it%6) {
case 0:
g_buffer[i] = MFB_RGB(0xFF, 0x00, 0x00);
break;
case 1:
g_buffer[i] = MFB_RGB(0x80, 0x80, 0x00);
break;
case 2:
g_buffer[i] = MFB_RGB(0x00, 0xFF, 0x00);
break;
case 3:
g_buffer[i] = MFB_RGB(0, 0x80, 0x80);
break;
case 4:
g_buffer[i] = MFB_RGB(0, 0, 0xFF);
break;
case 5:
g_buffer[i] = MFB_RGB(0, 0, 0xFF);
}
}
++i;
}
}
state = mfb_update_ex(window, g_buffer, g_width, g_height);
if (state != STATE_OK) {
window = 0x0;
break;
}
} while(mfb_wait_sync(window));
return 0;
}