-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractal.c
286 lines (252 loc) · 6.96 KB
/
fractal.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
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
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <stdbool.h>
#include <assert.h>
#include <time.h>
#include "display.h"
struct screen{
display *d;
int wpx;
int hpx;
long double cx;
long double cy;
long double scale;
int iterations;
long double movementfactor;
int colourset;
int maxcoloursets;
};
typedef struct screen screen;
void freeScreen(screen *s){
end(s->d);
free(s->d);
free(s);
}
screen *newScreen(char *title, int width, int height){
screen *s = malloc(sizeof(screen));
s->d = newDisplay(title, width, height);
s->wpx = width;
s->hpx = height;
s->cx = 0;
s->cy = 0;
s->scale = 1;
s->iterations = 100;
s->movementfactor = 4;
s->colourset = 0;
s->maxcoloursets = 5;
return s;
}
void complexprint(long double a, long double b){
printf("%Lf + %Lfi\n", a, b);
}
long double squish(long double in,
long double inmin, long double inmax,
long double outmin, long double outmax){
long double floor = in - inmin;
long double inrange = inmax - inmin;
long double outrange = outmax - outmin;
long double out = ((floor * outrange) / inrange) + outmin;
return out;
}
long int getIterations(long double x, long double y, int maxi){
long double xi = x;
long double yi = y;
int i;
for(i = 0; i < maxi && (fabsl(xi) + fabsl(yi) < 5); i++){
long double z2x = (xi*xi - yi*yi);
long double z2y = (2*xi*yi);
xi = z2x + x;
yi = z2y + y;
}
/* uncomment for more accurate look
if(i > (maxi-10) || i < 10){
i = 0;
}
*/
return i;
}
int packRGBA(int r, int g, int b, int a){
int colour = 0;
colour = colour | (r & 0xFF);
colour = (colour << 8) | (g & 0xFF);
colour = (colour << 8) | (b & 0xFF);
colour = (colour << 8) | (a & 0xFF);
return colour;
}
int generatePackedRGBA(screen *s, int p){
int r;
int g;
int b;
int a;
switch(s->colourset){
case 0:
r = squish(p, 0, s->iterations, 100, 255);
g = squish(p, 0, s->iterations, 0, 127);
b = squish(sqrt(p), 0, 14, 0, 255);
a = 255;
break;
case 1:
r = squish(p*p, 0, s->iterations*s->iterations, 5, 70);
g = squish(p, 0, s->iterations, 0, 255);
b = squish(p, 0, s->iterations, 0, 150);
a = 255;
break;
case 2:
r = squish(p, 0, s->iterations, 0, 70);
g = squish(p*p, 0, s->iterations*s->iterations, 10, 70);
b = squish(p*p, 0, s->iterations*s->iterations, 70, 255);
a = 255;
break;
case 3:
r = squish(p, 0, s->iterations, 0, 255);
g = squish(p*p, 0, s->iterations*s->iterations, 50, 100);
b = squish(p*p, 0, s->iterations*s->iterations, 50, 255);
a = 255;
break;
case 4:
r = squish(p*p, 0, s->iterations*s->iterations, 50, 100);
g = squish(p, 0, s->iterations, 0, 70);
b = squish(p*p, 0, s->iterations*s->iterations, 50, 255);
a = 255;
break;
default:
r = 5;
g = 55;
b = 255;
a = 255;
break;
}
int packedColour = packRGBA(r, g, b, a);
return packedColour;
}
void mandelbrotDrawPixel(screen *s, int xp, int yp){
long double xcord = squish(xp, 0, s->wpx, s->cx - s->scale, s->cx + s->scale);
long double ycord = squish(yp, 0, s->hpx, s->cy - s->scale, s->cy + s->scale);
long int i = getIterations(xcord, ycord, s->iterations);
int mancolour = generatePackedRGBA(s, i);
colour(s->d, mancolour);
SDL_RenderDrawPoint(s->d->renderer, xp, yp);
}
void generateAndFillPixels(screen *s){
for(int xp = 0; xp < s->wpx; xp++){
for(int yp = 0; yp < s->hpx; yp++){
mandelbrotDrawPixel(s, xp, yp);
}
}
show(s->d);
}
void saveCurrentImage(screen *s){
char *filepath = malloc(100);
time_t t = time(NULL);
struct tm tm = *localtime(&t);
sprintf(filepath, "fractal %d-%d-%d %d:%d:%d.bmp",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
SDL_SaveBMP(P(SDL_GetWindowSurface(s->d->window)), filepath);
free(filepath);
}
// start drawing the mandelbrot set
void mandelbrot(){
int width = 1000;
int height = 1000;
screen *ms = newScreen("Mandelbrot", width, height);
bool running = true;
while(running){
generateAndFillPixels(ms);
char k = key(ms->d);
switch(k){
case 'x':
running = false;
break;
// movement controls
case 'w':
ms->cy -= (ms->scale / ms->movementfactor);
break;
case 'a':
ms->cx -= (ms->scale / ms->movementfactor);
break;
case 's':
ms->cy += (ms->scale / ms->movementfactor);
break;
case 'd':
ms->cx += (ms->scale / ms->movementfactor);
break;
// zoom
case 'q':
ms->scale *= 1.5;
ms->iterations /= 1.05;
break;
case 'e':
ms->scale /= 1.5;
ms->iterations *= 1.05;
break;
case 'r':
ms->cx = 0;
ms->cy = 0;
ms->scale = 1;
break;
// saves images to file
case 'f':
saveCurrentImage(ms);
break;
// change colours
case 'c':
ms->colourset = (ms->colourset + 1) % ms->maxcoloursets;
break;
default:
printf("key: %c\n", k);
break;
}
}
freeScreen(ms);
}
#ifdef test
void testGetIterations(){
assert(getIterations(3, 3, 200) == 0);
assert(getIterations(0, 0, 356) == 356);
}
void testNewScreen(){
screen *ts = newScreen("test screen", 400,400);
freeScreen(ts);
}
void testPrintf(){
printf("testing printf\n");
}
void testDraw(){
display *d = newDisplay("testing draw display", 500, 500);
line(d, 0, 0, 500, 500);
end(d);
}
void testSquish(){
assert(squish(0, 0, 1, 0, 1) == 0);
assert(squish(1, 0, 2, 0, 10) == 5);
assert(squish(100, 0, 400, -200, 200) == -100);
assert(squish(50, 0, 400, -2, 2) == -1.5);
}
void testPackRGBA(){
assert(packRGBA(0, 0, 0, 0) == 0);
assert(packRGBA(1, 1, 1, 1) == 0x01010101);
}
void testColours(){
testPackRGBA();
}
void testAll(){
testPrintf();
testNewScreen();
testDraw();
testSquish();
testGetIterations();
testColours();
}
int main(int n, char *args[n]){
testAll();
return 0;
}
#endif
#ifndef test
int main(int n, char *args[n]){
printf("Lets get infinite!\n");
mandelbrot();
return 0;
}
#endif