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