-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractal.c
420 lines (358 loc) · 10.9 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* fractal.c: X-Fractals / routines to calculate point values and draw fractal image
*
* Authored by Parmjit Virk (2017)
*
* Licensed under the MIT license as per the Open Source Initiative 2017.
* See the LICENSE file for the complete license information,
* or visit https://opensource.org/licenses/MIT for details.
*
*/
#include <math.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include "Xfractals.h"
/* Define max. number of iterations for each fractal point ... */
#define ITER_MAX 155
/* Define local function prototypes ... */
/* TODO: put these in a separate library header file */
void getNewBounds(int, int, int, int, int, double *, double *, double *, double *);
void calculateMandelbrot(double, double, double *, double *, double, double, double, double);
void calculateJulia(double, double, double *, double *, double, double, double, double);
void calculateSpiral(double, double, double *, double *, double, double, double, double);
/* TODO: refactor color routines as void to improve performance */
unsigned long calculateColorBanded(int, int, int);
unsigned long calculateColorBlueDark(int, int, int);
unsigned long calculateColorPurpleDark(int, int, int);
unsigned long calculateColorBlueLight(int, int, int);
unsigned long calculateColorRedDark(int, int, int);
unsigned long calculateColorGreenLight(int, int, int);
unsigned long calculateColorGreenBanded(int, int, int);
unsigned long calculateColorBlueGreenBanded(int, int, int);
/*
Function createFractal
-> Generate/store pixel color data for a given fractal and region ...
*/
void createFractal
(int fractal_type, int fractal_color, unsigned long fractal_points[][HEIGHT][1],
int px1, int py1, int px2, int py2)
{
/* <*fractalRoutine>, pointer to a function body */
/* <*fractalColorRoutine>, pointer to a function body */
void (*fractalRoutine)(double, double, double *, double *, double, double, double, double);
unsigned long (*fractalColorRoutine)(int, int, int);
double dist_max,
real,
imag;
double xn,
xnew,
yn,
ynew,
x_inc,
y_inc,
orig1,
orig2,
dist;
int px,
py,
iter_count;
/*
Retain these doubles in memory even after function terminates!
-> static vars
*/
static double xmin,
xmax,
ymin,
ymax;
/*
Triplet of color data, positive values only!
-> unsigned data type
*/
unsigned long index_r,
index_g,
index_b;
/* Set appropriate values based on user choices ... */
switch(fractal_type)
{
case 1:
/* Assign the function pointer to a function body */
fractalRoutine = &calculateMandelbrot;
dist_max = 2.0;
real = 0.0;
imag = 0.0;
break;
case 2:
fractalRoutine = &calculateJulia;
dist_max = 2.0;
real = 0.3;
imag = 0.6;
break;
case 3:
fractalRoutine = &calculateSpiral;
dist_max = 4.0;
real = 0.85;
imag = 0.6;
break;
}
switch(fractal_color)
{
case 1:
/* Assign the function pointer to a function body */
fractalColorRoutine = &calculateColorBanded;
break;
case 2:
fractalColorRoutine = &calculateColorBlueDark;
break;
case 3:
fractalColorRoutine = &calculateColorPurpleDark;
break;
case 4:
fractalColorRoutine = &calculateColorBlueLight;
break;
case 5:
fractalColorRoutine = &calculateColorRedDark;
break;
case 6:
fractalColorRoutine = &calculateColorGreenLight;
break;
case 7:
fractalColorRoutine = &calculateColorGreenBanded;
break;
case 8:
fractalColorRoutine = &calculateColorBlueGreenBanded;
break;
}
/* Determine fractal bounds ... */
getNewBounds(fractal_type, px1, py1, px2, py2, &xmin, &ymin, &xmax, &ymax);
/*
Generate fractal color data ...
-> store in <fractal_points> array
*/
iter_count = 0;
dist = 0;
xn = xmin;
yn = ymax;
orig1 = xmin;
orig2 = ymax;
x_inc = ((xmax-xmin)/WIDTH);
y_inc = ((ymax-ymin)/HEIGHT);
for (px = 0 ; px < WIDTH ; px++)
{
for (py = 0 ; py < HEIGHT ; py++)
{
while ((iter_count <= ITER_MAX) && (dist < dist_max))
{
/* Call specified fractal routine */
fractalRoutine(xn, yn, &xnew, &ynew, orig1, orig2, real, imag);
xn = xnew;
yn = ynew;
dist = sqrt((xn*xn)+(yn*yn));
iter_count++;
}
if (dist < dist_max)
{
/* Set these points to black */
index_r = 0;
index_g = 0;
index_b = 0;
}
else
{
/* Set these points to some iterated value */
index_r = iter_count;
index_g = iter_count;
index_b = iter_count;
}
/*
Build a 24-bit long unsigned value from the color triplets ...
-> required by a TrueColor visual type to render color!
*/
fractal_points[px][py][0] = fractalColorRoutine(index_r, index_g, index_b);
iter_count = 0;
dist = 0;
xn = orig1;
orig2 = (orig2 - y_inc);
yn = orig2;
}
yn = ymax;
orig2 = ymax;
orig1 = (orig1 + x_inc);
xn = orig1;
}
return;
}
/*
Function drawFractal
-> Draw fractal into a given window ...
*/
void drawFractal(Display *display, Window *window, GC *gc, unsigned long fractal_points[][HEIGHT][1])
{
int x, y;
for (x = 0 ; x < WIDTH ; x++)
{
for (y = 0 ; y < HEIGHT ; y++)
{
/* Set foreground color to <fractal_points> value */
XSetForeground(display, *gc, fractal_points[x][y][0]);
/* Color pixel at the specified (x,y) coordinate in window */
XDrawPoint(display, *window, *gc, x, y);
}
}
/* Force processing of all directives in X buffer ...*/
XFlush(display);
return;
}
/*
Function getNewBounds
-> Determine fractal bounds based on user input ...
*/
void getNewBounds
(int type, int px1, int py1, int px2, int py2,
double *xmin, double *ymin, double *xmax, double *ymax)
{
int px_min,
px_max,
py_min,
py_max;
double x_diff,
y_diff;
/* If first value is -1 (i.e. first viewing), use the defaults */
if (px1 == -1)
{
if (type == 1)
{
*xmin = -2.5;
*xmax = 1.5;
*ymin = -1.5;
*ymax = 1.5;
}
else if (type == 2)
{
*xmin = -0.241001;
*xmax = 0.222222;
*ymin = 0.413542;
*ymax = 0.760960;
}
else
{
*xmin = -1.5;
*xmax = 2.5;
*ymin = -1.5;
*ymax = 1.5;
}
}
else if ((px1 != px2) && (py1 != py2))
{
/*
User has made a region selection for zooming
-> determine new bound values based on hotspot vertecies
*/
if (px1 < px2)
{
px_min = px1;
px_max = px2;
}
else
{
px_min = px2;
px_max = px1;
}
if (py1 < py2)
{
py_min = py1;
py_max = py2;
}
else
{
py_min = py2;
py_max = py1;
}
x_diff = (((*xmax) - (*xmin)) / WIDTH);
y_diff = (((*ymax) - (*ymin)) / HEIGHT);
*xmax = ((*xmin) + (px_max * x_diff));
*xmin = ((*xmin) + (px_min * x_diff));
*ymin = ((*ymax) - (py_max * y_diff));
*ymax = ((*ymax) - (py_min * y_diff));
}
else if ((px1 == px2) || (py1 == py2))
{
/*
User has single-clicked on the same point
-> shift current fractal view to center on this point
-> no zooming perfomed!
*/
x_diff = (((*xmax) - (*xmin)) / WIDTH);
y_diff = (((*ymax) - (*ymin)) / HEIGHT);
*xmax = (((*xmin) + (px1 * x_diff)) + ((WIDTH / 2) * x_diff));
*xmin = (((*xmin) + (px1 * x_diff)) - ((WIDTH / 2) * x_diff));
*ymin = (((*ymax) - (py1 * y_diff)) - ((HEIGHT / 2) * y_diff));
*ymax = (((*ymax) - (py1 * y_diff)) + ((HEIGHT / 2) * y_diff));
}
return;
}
/* Algorithms for various fractal types ... */
void calculateMandelbrot
(double xn, double yn, double *xnew, double *ynew,
double orig1, double orig2, double real, double imag)
{
*xnew = ((xn*xn) - (yn*yn) + orig1);
*ynew = (2 * xn * yn + orig2);
return;
}
void calculateJulia
(double xn, double yn, double *xnew, double *ynew,
double orig1, double orig2, double real, double imag)
{
*xnew = ((xn*xn) - (yn*yn) + real);
*ynew = (2 * xn * yn + imag);
return;
}
void calculateSpiral
(double xn, double yn, double *xnew, double *ynew,
double orig1, double orig2, double real, double imag)
{
*xnew = ((real * xn) - (real * xn * xn) + (real * yn * yn) - (imag * yn) + (2 * imag * xn * yn));
*ynew = ((real * yn) + (imag * xn) - (imag * xn * xn) + (imag * yn * yn) - (2 * real * xn * yn));
return;
}
unsigned long calculateColorBanded
(int index_red, int index_green, int index_blue)
{
return (index_red + (index_green << 4) + (index_blue << 8));
}
unsigned long calculateColorBlueDark
(int index_red, int index_green, int index_blue)
{
return (index_red + index_green + index_blue);
}
unsigned long calculateColorPurpleDark
(int index_red, int index_green, int index_blue)
{
return (index_red + index_green + (index_blue << 16));
}
unsigned long calculateColorBlueLight
(int index_red, int index_green, int index_blue)
{
return (index_red + index_green + (index_blue << 8));
}
unsigned long calculateColorRedDark
(int index_red, int index_green, int index_blue)
{
return ((index_red + index_green + index_blue) << 16);
}
unsigned long calculateColorGreenLight
(int index_red, int index_green, int index_blue)
{
return ((index_red + index_green + index_blue) << 8);
}
unsigned long calculateColorGreenBanded
(int index_red, int index_green, int index_blue)
{
return ((index_red + index_green + index_blue) << 12);
}
unsigned long calculateColorBlueGreenBanded
(int index_red, int index_green, int index_blue)
{
return ((65000 * (0.01 * index_red)) + (65000 * (0.01 * index_green)) + (65000 * (0.01 * index_blue)));
}