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