fe7ad4366d3064868edf3950ffb82ec808f4ca72
[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
47 # define UNIFORM_COLORS
48 # include "xlockmore.h"                         /* in xscreensaver distribution */
49 #else  /* !STANDALONE */
50 # include "xlock.h"                                     /* in xlockmore distribution */
51 #endif /* !STANDALONE */
52
53
54 #define DEF_MOUSE "False"
55
56 ENTRYPOINT ModeSpecOpt julia_opts = { 0, };
57
58
59 #define numpoints ((0x2<<jp->depth)-1)
60
61 typedef struct {
62         int         centerx;
63         int         centery;    /* center of the screen */
64         double      cr;
65         double      ci;         /* julia params */
66         int         depth;
67         int         inc;
68         int         circsize;
69         int         erase;
70         int         pix;
71         long        itree;
72         int         buffer;
73         int         nbuffers;
74         int         redrawing, redrawpos;
75         Pixmap      pixmap;
76 #ifndef HAVE_COCOA
77         Cursor      cursor;
78 #endif
79         GC          stippledGC;
80         XPoint    **pointBuffer;        /* pointer for XDrawPoints */
81     Bool        button_down_p;
82     int         mouse_x, mouse_y;
83
84 } juliastruct;
85
86 static juliastruct *julias = NULL;
87
88 /* How many segments to draw per cycle when redrawing */
89 #define REDRAWSTEP 3
90
91 static void
92 apply(juliastruct * jp, register double xr, register double xi, int d)
93 {
94         double      theta, r;
95
96         jp->pointBuffer[jp->buffer][jp->itree].x =
97                 (int) (0.5 * xr * jp->centerx + jp->centerx);
98         jp->pointBuffer[jp->buffer][jp->itree].y =
99                 (int) (0.5 * xi * jp->centery + jp->centery);
100         jp->itree++;
101
102         if (d > 0) {
103                 xi -= jp->ci;
104                 xr -= jp->cr;
105
106 /* Avoid atan2: DOMAIN error message */
107                 if (xi == 0.0 && xr == 0.0)
108                         theta = 0.0;
109                 else
110                         theta = atan2(xi, xr) / 2.0;
111
112                 /*r = pow(xi * xi + xr * xr, 0.25); */
113                 r = sqrt(sqrt(xi * xi + xr * xr));      /* 3 times faster */
114
115                 xr = r * cos(theta);
116                 xi = r * sin(theta);
117
118                 d--;
119                 apply(jp, xr, xi, d);
120                 apply(jp, -xr, -xi, d);
121         }
122 }
123
124 static void
125 incr(ModeInfo * mi, juliastruct * jp)
126 {
127         if (jp->button_down_p)
128           {
129                 jp->cr = ((double) (jp->mouse_x + 2 - jp->centerx)) * 2 / jp->centerx;
130                 jp->ci = ((double) (jp->mouse_y + 2 - jp->centery)) * 2 / jp->centery;
131           }
132         else
133           {
134 #if 0
135                 jp->cr = 1.5 * (sin(M_PI * (jp->inc / 300.0)) *
136                                                 sin(jp->inc * M_PI / 200.0));
137                 jp->ci = 1.5 * (cos(M_PI * (jp->inc / 300.0)) *
138                                                 cos(jp->inc * M_PI / 200.0));
139
140                 jp->cr += 0.5 * cos(M_PI * jp->inc / 400.0);
141                 jp->ci += 0.5 * sin(M_PI * jp->inc / 400.0);
142 #else
143         jp->cr = 1.5 * (sin(M_PI * (jp->inc / 290.0)) *
144                         sin(jp->inc * M_PI / 210.0));
145         jp->ci = 1.5 * (cos(M_PI * (jp->inc / 310.0)) *
146                         cos(jp->inc * M_PI / 190.0));
147
148         jp->cr += 0.5 * cos(M_PI * jp->inc / 395.0);
149         jp->ci += 0.5 * sin(M_PI * jp->inc / 410.0);
150 #endif
151           }
152 }
153
154 ENTRYPOINT void
155 init_julia(ModeInfo * mi)
156 {
157         Display    *display = MI_DISPLAY(mi);
158         Window      window = MI_WINDOW(mi);
159         juliastruct *jp;
160         XGCValues   gcv;
161         int         i;
162
163         if (julias == NULL) {
164                 if ((julias = (juliastruct *) calloc(MI_NUM_SCREENS(mi),
165                                               sizeof (juliastruct))) == NULL)
166                         return;
167         }
168         jp = &julias[MI_SCREEN(mi)];
169
170         jp->centerx = MI_WIN_WIDTH(mi) / 2;
171         jp->centery = MI_WIN_HEIGHT(mi) / 2;
172
173         jp->depth = MI_BATCHCOUNT(mi);
174         if (jp->depth > 10)
175                 jp->depth = 10;
176
177
178 #ifndef HAVE_COCOA
179         if (jp->button_down_p && !jp->cursor && !jp->cursor)
180           {
181                 Pixmap bit;
182                 XColor black;
183                 black.red = black.green = black.blue = 0;
184                 black.flags = DoRed|DoGreen|DoBlue;
185                 bit = XCreatePixmapFromBitmapData (display, window, "\000", 1, 1,
186                                                                                    MI_WIN_BLACK_PIXEL(mi),
187                                                                                    MI_WIN_BLACK_PIXEL(mi), 1);
188                 jp->cursor = XCreatePixmapCursor (display, bit, bit, &black, &black,
189                                                                                   0, 0);
190                 XFreePixmap (display, bit);
191           }
192 #endif /* HAVE_COCOA */
193
194         if (jp->pixmap != None &&
195             jp->circsize != (MIN(jp->centerx, jp->centery) / 60) * 2 + 1) {
196                 XFreePixmap(display, jp->pixmap);
197                 jp->pixmap = None;
198         }
199         if (jp->pixmap == None) {
200                 GC          fg_gc = None, bg_gc = None;
201
202                 jp->circsize = MAX(8, (MIN(jp->centerx, jp->centery) / 96) * 2 + 1);
203                 jp->pixmap = XCreatePixmap(display, window, jp->circsize, jp->circsize, 1);
204                 gcv.foreground = 1;
205                 fg_gc = XCreateGC(display, jp->pixmap, GCForeground, &gcv);
206                 gcv.foreground = 0;
207                 bg_gc = XCreateGC(display, jp->pixmap, GCForeground, &gcv);
208                 XFillRectangle(display, jp->pixmap, bg_gc,
209                                0, 0, jp->circsize, jp->circsize);
210                 if (jp->circsize < 2)
211                         XDrawPoint(display, jp->pixmap, fg_gc, 0, 0);
212                 else
213                         XFillArc(display, jp->pixmap, fg_gc,
214                                  0, 0, jp->circsize, jp->circsize, 0, 23040);
215                 if (fg_gc != None)
216                         XFreeGC(display, fg_gc);
217                 if (bg_gc != None)
218                         XFreeGC(display, bg_gc);
219         }
220
221 #ifndef HAVE_COCOA
222         if (MI_WIN_IS_INROOT(mi))
223           ;
224         else if (jp->circsize > 0)
225           XDefineCursor (display, window, jp->cursor);
226         else
227           XUndefineCursor (display, window);
228 #endif /* HAVE_COCOA */
229
230         if (!jp->stippledGC) {
231                 gcv.foreground = MI_WIN_BLACK_PIXEL(mi);
232                 gcv.background = MI_WIN_BLACK_PIXEL(mi);
233                 if ((jp->stippledGC = XCreateGC(display, window,
234                                  GCForeground | GCBackground, &gcv)) == None)
235                         return;
236         }
237         if (MI_NPIXELS(mi) > 2)
238                 jp->pix = NRAND(MI_NPIXELS(mi));
239         jp->inc = ((LRAND() & 1) * 2 - 1) * NRAND(200);
240         jp->nbuffers = (MI_CYCLES(mi) + 1);
241         if (!jp->pointBuffer)
242                 jp->pointBuffer = (XPoint **) calloc(jp->nbuffers, sizeof (XPoint *));
243         for (i = 0; i < jp->nbuffers; ++i)
244                 if (jp->pointBuffer[i])
245                         (void) memset((char *) jp->pointBuffer[i], 0,
246                                       numpoints * sizeof (XPoint));
247                 else
248                         jp->pointBuffer[i] = (XPoint *) calloc(numpoints, sizeof (XPoint));
249         jp->buffer = 0;
250         jp->redrawing = 0;
251         jp->erase = 0;
252         XClearWindow(display, window);
253 }
254
255
256 static void
257 reshape_julia (ModeInfo *mi, int w, int h)
258 {
259   init_julia (mi);
260 }
261
262
263 ENTRYPOINT Bool
264 julia_handle_event (ModeInfo *mi, XEvent *event)
265 {
266   juliastruct *jp = &julias[MI_SCREEN(mi)];
267
268   if (event->xany.type == ButtonPress &&
269       event->xbutton.button == Button1)
270     {
271       jp->button_down_p = True;
272       jp->mouse_x = event->xbutton.x;
273       jp->mouse_y = event->xbutton.y;
274       return True;
275     }
276   else if (event->xany.type == ButtonRelease &&
277            event->xbutton.button == Button1)
278     {
279       jp->button_down_p = False;
280       return True;
281     }
282   else if (event->xany.type == MotionNotify && jp->button_down_p)
283     {
284       jp->mouse_x = event->xmotion.x;
285       jp->mouse_y = event->xmotion.y;
286       return True;
287     }
288
289   return False;
290 }
291
292
293
294 /* hack: moved here by jwz. */
295 #define ERASE_IMAGE(d,w,g,x,y,xl,yl,xs,ys) \
296 if (yl<y) \
297 (y<yl+ys)?XFillRectangle(d,w,g,xl,yl,xs,y-yl): \
298 XFillRectangle(d,w,g,xl,yl,xs,ys); \
299 else if (yl>y) \
300 (y>yl-ys)?XFillRectangle(d,w,g,xl,y+ys,xs,yl-y): \
301 XFillRectangle(d,w,g,xl,yl,xs,ys); \
302 if (xl<x) \
303 (x<xl+xs)?XFillRectangle(d,w,g,xl,yl,x-xl,ys): \
304 XFillRectangle(d,w,g,xl,yl,xs,ys); \
305 else if (xl>x) \
306 (x>xl-xs)?XFillRectangle(d,w,g,x+xs,yl,xl-x,ys): \
307 XFillRectangle(d,w,g,xl,yl,xs,ys)
308
309
310 ENTRYPOINT void
311 draw_julia (ModeInfo * mi)
312 {
313         Display    *display = MI_DISPLAY(mi);
314         Window      window = MI_WINDOW(mi);
315         GC          gc = MI_GC(mi);
316         juliastruct *jp = &julias[MI_SCREEN(mi)];
317         double      r, theta;
318         register double xr = 0.0, xi = 0.0;
319         int         k = 64, rnd = 0, i, j;
320         XPoint     *xp = jp->pointBuffer[jp->buffer], old_circle, new_circle;
321
322         old_circle.x = (int) (jp->centerx * jp->cr / 2) + jp->centerx - 2;
323         old_circle.y = (int) (jp->centery * jp->ci / 2) + jp->centery - 2;
324         incr(mi, jp);
325         new_circle.x = (int) (jp->centerx * jp->cr / 2) + jp->centerx - 2;
326         new_circle.y = (int) (jp->centery * jp->ci / 2) + jp->centery - 2;
327         XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
328         XFillArc(display, window, gc, 
329              old_circle.x-jp->circsize/2-2,
330              old_circle.y-jp->circsize/2-2,
331              jp->circsize+4, jp->circsize+4,
332              0, 360*64);
333         /* draw a circle at the c-parameter so you can see it's effect on the
334            structure of the julia set */
335         XSetForeground(display, jp->stippledGC, MI_WIN_WHITE_PIXEL(mi));
336 #ifndef HAVE_COCOA
337         XSetTSOrigin(display, jp->stippledGC, new_circle.x, new_circle.y);
338         XSetStipple(display, jp->stippledGC, jp->pixmap);
339         XSetFillStyle(display, jp->stippledGC, FillOpaqueStippled);
340 #endif /* HAVE_COCOA */
341         XDrawArc(display, window, jp->stippledGC, 
342              new_circle.x-jp->circsize/2,
343              new_circle.y-jp->circsize/2,
344              jp->circsize, jp->circsize,
345              0, 360*64);
346
347         if (jp->erase == 1) {
348                 XDrawPoints(display, window, gc,
349                     jp->pointBuffer[jp->buffer], numpoints, CoordModeOrigin);
350         }
351         jp->inc++;
352         if (MI_NPIXELS(mi) > 2) {
353                 XSetForeground(display, gc, MI_PIXEL(mi, jp->pix));
354                 if (++jp->pix >= MI_NPIXELS(mi))
355                         jp->pix = 0;
356         } else
357                 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
358         while (k--) {
359
360                 /* save calls to LRAND by using bit shifts over and over on the same
361                    int for 32 iterations, then get a new random int */
362                 if (!(k % 32))
363                         rnd = LRAND();
364
365                 /* complex sqrt: x^0.5 = radius^0.5*(cos(theta/2) + i*sin(theta/2)) */
366
367                 xi -= jp->ci;
368                 xr -= jp->cr;
369
370                 /* Avoid atan2: DOMAIN error message */
371                 if (xi == 0.0 && xr == 0.0)
372                         theta = 0.0;
373                 else
374                         theta = atan2(xi, xr) / 2.0;
375
376                 /*r = pow(xi * xi + xr * xr, 0.25); */
377                 r = sqrt(sqrt(xi * xi + xr * xr));      /* 3 times faster */
378
379                 xr = r * cos(theta);
380                 xi = r * sin(theta);
381
382                 if ((rnd >> (k % 32)) & 0x1) {
383                         xi = -xi;
384                         xr = -xr;
385                 }
386                 xp->x = jp->centerx + (int) ((jp->centerx >> 1) * xr);
387                 xp->y = jp->centery + (int) ((jp->centery >> 1) * xi);
388                 xp++;
389         }
390
391         jp->itree = 0;
392         apply(jp, xr, xi, jp->depth);
393
394         XDrawPoints(display, window, gc,
395                     jp->pointBuffer[jp->buffer], numpoints, CoordModeOrigin);
396
397         jp->buffer++;
398         if (jp->buffer > jp->nbuffers - 1) {
399                 jp->buffer -= jp->nbuffers;
400                 jp->erase = 1;
401         }
402         if (jp->redrawing) {
403                 for (i = 0; i < REDRAWSTEP; i++) {
404                         j = (jp->buffer - jp->redrawpos + jp->nbuffers) % jp->nbuffers;
405                         XDrawPoints(display, window, gc,
406                              jp->pointBuffer[j], numpoints, CoordModeOrigin);
407
408                         if (++(jp->redrawpos) >= jp->nbuffers) {
409                                 jp->redrawing = 0;
410                                 break;
411                         }
412                 }
413         }
414 }
415
416 ENTRYPOINT void
417 release_julia (ModeInfo * mi)
418 {
419         if (julias != NULL) {
420                 int         screen;
421
422                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
423                         Display    *display = MI_DISPLAY(mi);
424                         juliastruct *jp = &julias[screen];
425                         int         buffer;
426
427                         if (jp->pointBuffer) {
428                                 for (buffer = 0; buffer < jp->nbuffers; buffer++)
429                                         if (jp->pointBuffer[buffer])
430                                                 (void) free((void *) jp->pointBuffer[buffer]);
431                                 (void) free((void *) jp->pointBuffer);
432                         }
433                         if (jp->stippledGC != None)
434                                 XFreeGC(display, jp->stippledGC);
435                         if (jp->pixmap != None)
436                                 XFreePixmap(display, jp->pixmap);
437 #ifndef HAVE_COCOA
438                         if (jp->cursor)
439                           XFreeCursor (display, jp->cursor);
440 #endif
441                 }
442                 (void) free((void *) julias);
443                 julias = NULL;
444         }
445 }
446
447 ENTRYPOINT void
448 refresh_julia (ModeInfo * mi)
449 {
450         juliastruct *jp = &julias[MI_SCREEN(mi)];
451
452         jp->redrawing = 1;
453         jp->redrawpos = 0;
454 }
455
456 XSCREENSAVER_MODULE ("Julia", julia)