From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[xscreensaver] / hacks / julia.c
1 /* -*- Mode: C; tab-width: 4 -*-
2  * julia --- continuously varying Julia set.
3  */
4 #if 0
5 static const char sccsid[] = "@(#)julia.c       4.03 97/04/10 xlockmore";
6 #endif
7
8 /* Copyright (c) 1995 Sean McCullough <bankshot@mailhost.nmt.edu>.
9  *
10  * Permission to use, copy, modify, and distribute this software and its
11  * documentation for any purpose and without fee is hereby granted,
12  * provided that the above copyright notice appear in all copies and that
13  * both that copyright notice and this permission notice appear in
14  * supporting documentation.
15  *
16  * This file is provided AS IS with no warranties of any kind.  The author
17  * shall have no liability with respect to the infringement of copyrights,
18  * trade secrets or any patents by this file or any part thereof.  In no
19  * event will the author be liable for any lost revenue or profits or
20  * other special, indirect and consequential damages.
21  *
22  * Revision History:
23  * 10-Jun-06: j.grahl@ucl.ac.uk: tweaked functions for parameter of Julia set
24  * 28-May-97: jwz@jwz.org: added interactive frobbing with the mouse.
25  * 10-May-97: jwz@jwz.org: turned into a standalone program.
26  * 02-Dec-95: snagged boilerplate from hop.c
27  *           used ifs {w0 = sqrt(x-c), w1 = -sqrt(x-c)} with random iteration 
28  *           to plot the julia set, and sinusoidially varied parameter for set 
29  *           and plotted parameter with a circle.
30  */
31
32 /*-
33  * One thing to note is that batchcount is the *depth* of the search tree,
34  * so the number of points computed is 2^batchcount - 1.  I use 8 or 9
35  * on a dx266 and it looks okay.  The sinusoidal variation of the parameter
36  * might not be as interesting as it could, but it still gives an idea of
37  * the effect of the parameter.
38  */
39
40 #ifdef STANDALONE
41 # define DEFAULTS       "*count:                  1000   \n" \
42                                         "*cycles:                 20     \n" \
43                                         "*delay:                  10000  \n" \
44                                         "*ncolors:                200    \n" \
45                                         "*fpsSolid:               true   \n" \
46                                         "*ignoreRotation: True   \n" \
47
48 # define UNIFORM_COLORS
49 # define release_julia 0
50 # define reshape_julia 0
51 # include "xlockmore.h"                         /* in xscreensaver distribution */
52 #else  /* !STANDALONE */
53 # include "xlock.h"                                     /* in xlockmore distribution */
54 #endif /* !STANDALONE */
55
56
57 #define DEF_MOUSE "False"
58
59 ENTRYPOINT ModeSpecOpt julia_opts = { 0, };
60
61
62 #define numpoints ((0x2<<jp->depth)-1)
63
64 typedef struct {
65         int         centerx;
66         int         centery;    /* center of the screen */
67         double      cr;
68         double      ci;         /* julia params */
69         int         depth;
70         int         inc;
71         int         circsize;
72         int         erase;
73         int         pix;
74         long        itree;
75         int         buffer;
76         int         nbuffers;
77         int         redrawing, redrawpos;
78         Pixmap      pixmap;
79 #ifndef HAVE_JWXYZ
80         Cursor      cursor;
81 #endif
82         GC          stippledGC;
83         XPoint    **pointBuffer;        /* pointer for XDrawPoints */
84     Bool        button_down_p;
85     int         mouse_x, mouse_y;
86
87 } juliastruct;
88
89 static juliastruct *julias = NULL;
90
91 /* How many segments to draw per cycle when redrawing */
92 #define REDRAWSTEP 3
93
94 static void
95 apply(juliastruct * jp, register double xr, register double xi, int d)
96 {
97         double      theta, r;
98
99         jp->pointBuffer[jp->buffer][jp->itree].x =
100                 (int) (0.5 * xr * jp->centerx + jp->centerx);
101         jp->pointBuffer[jp->buffer][jp->itree].y =
102                 (int) (0.5 * xi * jp->centery + jp->centery);
103         jp->itree++;
104
105         if (d > 0) {
106                 xi -= jp->ci;
107                 xr -= jp->cr;
108
109 /* Avoid atan2: DOMAIN error message */
110                 if (xi == 0.0 && xr == 0.0)
111                         theta = 0.0;
112                 else
113                         theta = atan2(xi, xr) / 2.0;
114
115                 /*r = pow(xi * xi + xr * xr, 0.25); */
116                 r = sqrt(sqrt(xi * xi + xr * xr));      /* 3 times faster */
117
118                 xr = r * cos(theta);
119                 xi = r * sin(theta);
120
121                 d--;
122                 apply(jp, xr, xi, d);
123                 apply(jp, -xr, -xi, d);
124         }
125 }
126
127 static void
128 incr(ModeInfo * mi, juliastruct * jp)
129 {
130         if (jp->button_down_p)
131           {
132                 jp->cr = ((double) (jp->mouse_x + 2 - jp->centerx)) * 2 / jp->centerx;
133                 jp->ci = ((double) (jp->mouse_y + 2 - jp->centery)) * 2 / jp->centery;
134           }
135         else
136           {
137 #if 0
138                 jp->cr = 1.5 * (sin(M_PI * (jp->inc / 300.0)) *
139                                                 sin(jp->inc * M_PI / 200.0));
140                 jp->ci = 1.5 * (cos(M_PI * (jp->inc / 300.0)) *
141                                                 cos(jp->inc * M_PI / 200.0));
142
143                 jp->cr += 0.5 * cos(M_PI * jp->inc / 400.0);
144                 jp->ci += 0.5 * sin(M_PI * jp->inc / 400.0);
145 #else
146         jp->cr = 1.5 * (sin(M_PI * (jp->inc / 290.0)) *
147                         sin(jp->inc * M_PI / 210.0));
148         jp->ci = 1.5 * (cos(M_PI * (jp->inc / 310.0)) *
149                         cos(jp->inc * M_PI / 190.0));
150
151         jp->cr += 0.5 * cos(M_PI * jp->inc / 395.0);
152         jp->ci += 0.5 * sin(M_PI * jp->inc / 410.0);
153 #endif
154           }
155 }
156
157 ENTRYPOINT void
158 init_julia(ModeInfo * mi)
159 {
160         Display    *display = MI_DISPLAY(mi);
161         Window      window = MI_WINDOW(mi);
162         juliastruct *jp;
163         XGCValues   gcv;
164         int         i;
165
166         MI_INIT (mi, julias);
167         jp = &julias[MI_SCREEN(mi)];
168
169         jp->centerx = MI_WIN_WIDTH(mi) / 2;
170         jp->centery = MI_WIN_HEIGHT(mi) / 2;
171
172         jp->depth = MI_BATCHCOUNT(mi);
173         if (jp->depth > 10)
174                 jp->depth = 10;
175
176
177 #ifndef HAVE_JWXYZ
178         if (jp->button_down_p && !jp->cursor && !jp->cursor)
179           {
180                 Pixmap bit;
181                 XColor black;
182                 black.red = black.green = black.blue = 0;
183                 black.flags = DoRed|DoGreen|DoBlue;
184                 bit = XCreatePixmapFromBitmapData (display, window, "\000", 1, 1,
185                                                                                    MI_WIN_BLACK_PIXEL(mi),
186                                                                                    MI_WIN_BLACK_PIXEL(mi), 1);
187                 jp->cursor = XCreatePixmapCursor (display, bit, bit, &black, &black,
188                                                                                   0, 0);
189                 XFreePixmap (display, bit);
190           }
191 #endif /* HAVE_JWXYZ */
192
193         if (jp->pixmap != None &&
194             jp->circsize != (MIN(jp->centerx, jp->centery) / 60) * 2 + 1) {
195                 XFreePixmap(display, jp->pixmap);
196                 jp->pixmap = None;
197         }
198         if (jp->pixmap == None) {
199                 GC          fg_gc = None, bg_gc = None;
200
201                 jp->circsize = MAX(8, (MIN(jp->centerx, jp->centery) / 96) * 2 + 1);
202                 jp->pixmap = XCreatePixmap(display, window, jp->circsize, jp->circsize, 1);
203                 gcv.foreground = 1;
204                 fg_gc = XCreateGC(display, jp->pixmap, GCForeground, &gcv);
205                 gcv.foreground = 0;
206                 bg_gc = XCreateGC(display, jp->pixmap, GCForeground, &gcv);
207                 XFillRectangle(display, jp->pixmap, bg_gc,
208                                0, 0, jp->circsize, jp->circsize);
209                 if (jp->circsize < 2)
210                         XDrawPoint(display, jp->pixmap, fg_gc, 0, 0);
211                 else
212                         XFillArc(display, jp->pixmap, fg_gc,
213                                  0, 0, jp->circsize, jp->circsize, 0, 23040);
214                 if (fg_gc != None)
215                         XFreeGC(display, fg_gc);
216                 if (bg_gc != None)
217                         XFreeGC(display, bg_gc);
218         }
219
220 #ifndef HAVE_JWXYZ
221         if (MI_WIN_IS_INROOT(mi))
222           ;
223         else if (jp->circsize > 0)
224           XDefineCursor (display, window, jp->cursor);
225         else
226           XUndefineCursor (display, window);
227 #endif /* HAVE_JWXYZ */
228
229         if (!jp->stippledGC) {
230                 gcv.foreground = MI_WIN_BLACK_PIXEL(mi);
231                 gcv.background = MI_WIN_BLACK_PIXEL(mi);
232                 if ((jp->stippledGC = XCreateGC(display, window,
233                                  GCForeground | GCBackground, &gcv)) == None)
234                         return;
235         }
236         if (MI_NPIXELS(mi) > 2)
237                 jp->pix = NRAND(MI_NPIXELS(mi));
238         jp->inc = ((LRAND() & 1) * 2 - 1) * NRAND(200);
239         jp->nbuffers = (MI_CYCLES(mi) + 1);
240         if (!jp->pointBuffer)
241                 jp->pointBuffer = (XPoint **) calloc(jp->nbuffers, sizeof (XPoint *));
242         for (i = 0; i < jp->nbuffers; ++i)
243                 if (jp->pointBuffer[i])
244                         (void) memset((char *) jp->pointBuffer[i], 0,
245                                       numpoints * sizeof (XPoint));
246                 else
247                         jp->pointBuffer[i] = (XPoint *) calloc(numpoints, sizeof (XPoint));
248         jp->buffer = 0;
249         jp->redrawing = 0;
250         jp->erase = 0;
251         XClearWindow(display, window);
252 }
253
254
255 ENTRYPOINT Bool
256 julia_handle_event (ModeInfo *mi, XEvent *event)
257 {
258   juliastruct *jp = &julias[MI_SCREEN(mi)];
259
260   if (event->xany.type == ButtonPress &&
261       event->xbutton.button == Button1)
262     {
263       jp->button_down_p = True;
264       jp->mouse_x = event->xbutton.x;
265       jp->mouse_y = event->xbutton.y;
266       return True;
267     }
268   else if (event->xany.type == ButtonRelease &&
269            event->xbutton.button == Button1)
270     {
271       jp->button_down_p = False;
272       return True;
273     }
274   else if (event->xany.type == MotionNotify && jp->button_down_p)
275     {
276       jp->mouse_x = event->xmotion.x;
277       jp->mouse_y = event->xmotion.y;
278       return True;
279     }
280
281   return False;
282 }
283
284
285
286 /* hack: moved here by jwz. */
287 #define ERASE_IMAGE(d,w,g,x,y,xl,yl,xs,ys) \
288 if (yl<y) \
289 (y<yl+ys)?XFillRectangle(d,w,g,xl,yl,xs,y-yl): \
290 XFillRectangle(d,w,g,xl,yl,xs,ys); \
291 else if (yl>y) \
292 (y>yl-ys)?XFillRectangle(d,w,g,xl,y+ys,xs,yl-y): \
293 XFillRectangle(d,w,g,xl,yl,xs,ys); \
294 if (xl<x) \
295 (x<xl+xs)?XFillRectangle(d,w,g,xl,yl,x-xl,ys): \
296 XFillRectangle(d,w,g,xl,yl,xs,ys); \
297 else if (xl>x) \
298 (x>xl-xs)?XFillRectangle(d,w,g,x+xs,yl,xl-x,ys): \
299 XFillRectangle(d,w,g,xl,yl,xs,ys)
300
301
302 ENTRYPOINT void
303 draw_julia (ModeInfo * mi)
304 {
305         Display    *display = MI_DISPLAY(mi);
306         Window      window = MI_WINDOW(mi);
307         GC          gc = MI_GC(mi);
308         juliastruct *jp = &julias[MI_SCREEN(mi)];
309         double      r, theta;
310         register double xr = 0.0, xi = 0.0;
311         int         k = 64, rnd = 0, i, j;
312         XPoint     *xp = jp->pointBuffer[jp->buffer], old_circle, new_circle;
313
314         old_circle.x = (int) (jp->centerx * jp->cr / 2) + jp->centerx - 2;
315         old_circle.y = (int) (jp->centery * jp->ci / 2) + jp->centery - 2;
316         incr(mi, jp);
317         new_circle.x = (int) (jp->centerx * jp->cr / 2) + jp->centerx - 2;
318         new_circle.y = (int) (jp->centery * jp->ci / 2) + jp->centery - 2;
319         XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
320         XFillArc(display, window, gc, 
321              old_circle.x-jp->circsize/2-2,
322              old_circle.y-jp->circsize/2-2,
323              jp->circsize+4, jp->circsize+4,
324              0, 360*64);
325         /* draw a circle at the c-parameter so you can see it's effect on the
326            structure of the julia set */
327         XSetForeground(display, jp->stippledGC, MI_WIN_WHITE_PIXEL(mi));
328 #ifndef HAVE_JWXYZ
329         XSetTSOrigin(display, jp->stippledGC, new_circle.x, new_circle.y);
330         XSetStipple(display, jp->stippledGC, jp->pixmap);
331         XSetFillStyle(display, jp->stippledGC, FillOpaqueStippled);
332 #endif /* HAVE_JWXYZ */
333         XDrawArc(display, window, jp->stippledGC, 
334              new_circle.x-jp->circsize/2,
335              new_circle.y-jp->circsize/2,
336              jp->circsize, jp->circsize,
337              0, 360*64);
338
339         if (jp->erase == 1) {
340                 XDrawPoints(display, window, gc,
341                     jp->pointBuffer[jp->buffer], numpoints, CoordModeOrigin);
342         }
343         jp->inc++;
344         if (MI_NPIXELS(mi) > 2) {
345                 XSetForeground(display, gc, MI_PIXEL(mi, jp->pix));
346                 if (++jp->pix >= MI_NPIXELS(mi))
347                         jp->pix = 0;
348         } else
349                 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
350         while (k--) {
351
352                 /* save calls to LRAND by using bit shifts over and over on the same
353                    int for 32 iterations, then get a new random int */
354                 if (!(k % 32))
355                         rnd = LRAND();
356
357                 /* complex sqrt: x^0.5 = radius^0.5*(cos(theta/2) + i*sin(theta/2)) */
358
359                 xi -= jp->ci;
360                 xr -= jp->cr;
361
362                 /* Avoid atan2: DOMAIN error message */
363                 if (xi == 0.0 && xr == 0.0)
364                         theta = 0.0;
365                 else
366                         theta = atan2(xi, xr) / 2.0;
367
368                 /*r = pow(xi * xi + xr * xr, 0.25); */
369                 r = sqrt(sqrt(xi * xi + xr * xr));      /* 3 times faster */
370
371                 xr = r * cos(theta);
372                 xi = r * sin(theta);
373
374                 if ((rnd >> (k % 32)) & 0x1) {
375                         xi = -xi;
376                         xr = -xr;
377                 }
378                 xp->x = jp->centerx + (int) ((jp->centerx >> 1) * xr);
379                 xp->y = jp->centery + (int) ((jp->centery >> 1) * xi);
380                 xp++;
381         }
382
383         jp->itree = 0;
384         apply(jp, xr, xi, jp->depth);
385
386         XDrawPoints(display, window, gc,
387                     jp->pointBuffer[jp->buffer], numpoints, CoordModeOrigin);
388
389         jp->buffer++;
390         if (jp->buffer > jp->nbuffers - 1) {
391                 jp->buffer -= jp->nbuffers;
392                 jp->erase = 1;
393         }
394         if (jp->redrawing) {
395                 for (i = 0; i < REDRAWSTEP; i++) {
396                         j = (jp->buffer - jp->redrawpos + jp->nbuffers) % jp->nbuffers;
397                         XDrawPoints(display, window, gc,
398                              jp->pointBuffer[j], numpoints, CoordModeOrigin);
399
400                         if (++(jp->redrawpos) >= jp->nbuffers) {
401                                 jp->redrawing = 0;
402                                 break;
403                         }
404                 }
405         }
406 }
407
408 ENTRYPOINT void
409 free_julia (ModeInfo * mi)
410 {
411         Display    *display = MI_DISPLAY(mi);
412         juliastruct *jp = &julias[MI_SCREEN(mi)];
413         int         buffer;
414
415         if (jp->pointBuffer) {
416                 for (buffer = 0; buffer < jp->nbuffers; buffer++)
417                         if (jp->pointBuffer[buffer])
418                                 (void) free((void *) jp->pointBuffer[buffer]);
419                 (void) free((void *) jp->pointBuffer);
420         }
421         if (jp->stippledGC != None)
422                 XFreeGC(display, jp->stippledGC);
423         if (jp->pixmap != None)
424                 XFreePixmap(display, jp->pixmap);
425 #ifndef HAVE_JWXYZ
426         if (jp->cursor)
427           XFreeCursor (display, jp->cursor);
428 #endif
429 }
430
431 #ifndef STANDALONE
432 ENTRYPOINT void
433 refresh_julia (ModeInfo * mi)
434 {
435         juliastruct *jp = &julias[MI_SCREEN(mi)];
436
437         jp->redrawing = 1;
438         jp->redrawpos = 0;
439 }
440 #endif
441
442 XSCREENSAVER_MODULE ("Julia", julia)