From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / discrete.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* discrete --- chaotic mappings */
3
4 #if 0
5 static const char sccsid[] = "@(#)discrete.c    5.00 2000/11/01 xlockmore";
6 #endif
7
8 /*-
9  * Copyright (c) 1996 by Tim Auckland <tda10.geo@yahoo.com>
10  *
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation for any purpose and without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and that
14  * both that copyright notice and this permission notice appear in
15  * supporting documentation.
16  *
17  * This file is provided AS IS with no warranties of any kind.  The author
18  * shall have no liability with respect to the infringement of copyrights,
19  * trade secrets or any patents by this file or any part thereof.  In no
20  * event will the author be liable for any lost revenue or profits or
21  * other special, indirect and consequential damages.
22  *
23  * "discrete" shows a number of fractals based on the "discrete map"
24  * type of dynamical systems.  They include a different way of looking
25  * at the HOPALONG system, an inverse julia-set iteration, the "Standard
26  * Map" and the "Bird in a Thornbush" fractal.
27  *
28  * Revision History:
29  * 01-Nov-2000: Allocation checks
30  * 31-Jul-1997: Ported to xlockmore-4
31  * 08-Aug-1996: Adapted from hop.c Copyright (c) 1991 by Patrick J. Naughton.
32  */
33
34 #ifdef STANDALONE
35 # define MODE_discrete
36 #define DEFAULTS        "*delay: 20000 \n" \
37                                         "*count:  4096 \n" \
38                                         "*cycles: 2500 \n" \
39                                         "*ncolors: 100 \n" \
40                                         "*fpsSolid: true \n" \
41                                     "*ignoreRotation: True \n" \
42
43 # define SMOOTH_COLORS
44 # define release_discrete 0
45 # include "xlockmore.h"         /* in xscreensaver distribution */
46 # include "erase.h"
47 #else /* STANDALONE */
48 # include "xlock.h"             /* in xlockmore distribution */
49 #endif /* STANDALONE */
50
51 #ifdef MODE_discrete
52
53 ENTRYPOINT ModeSpecOpt discrete_opts =
54 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
55
56 #ifdef USE_MODULES
57 ModStruct   discrete_description =
58 {"discrete", "init_discrete", "draw_discrete", (char *) NULL,
59  "refresh_discrete", "init_discrete", (char *) NULL, &discrete_opts,
60  1000, 4096, 2500, 1, 64, 1.0, "",
61  "Shows various discrete maps", 0, NULL};
62
63 #endif
64
65 enum ftypes {
66         SQRT, BIRDIE, STANDARD, TRIG, CUBIC, HENON, AILUJ, HSHOE, DELOG
67 };
68
69 /*#define TEST STANDARD */
70
71 #define BIASES 18
72 static enum ftypes bias[BIASES] =
73 {
74         STANDARD, STANDARD, STANDARD, STANDARD,
75         SQRT, SQRT, SQRT, SQRT,
76         BIRDIE, BIRDIE, BIRDIE,
77         AILUJ, AILUJ, AILUJ,
78         TRIG, TRIG,
79         CUBIC,
80         HENON,
81 };
82
83 typedef struct {
84         int         maxx;
85         int         maxy;       /* max of the screen */
86         double      a;
87         double      b;
88         double      c;
89         double      d;
90         double      e;
91         double      i;
92         double      j;          /* discrete parameters */
93         double      ic;
94         double      jc;
95         double      is;
96         double      js;
97         int         inc;
98         int         pix;
99         enum ftypes op;
100         int         count;
101         XPoint     *pointBuffer;        /* pointer for XDrawPoints */
102
103     int sqrt_sign, std_sign;
104
105 #ifdef STANDALONE
106   eraser_state *eraser;
107 #endif
108
109 } discretestruct;
110
111 static discretestruct *discretes = (discretestruct *) NULL;
112
113 static void free_discrete(ModeInfo * mi);
114
115 ENTRYPOINT void
116 init_discrete (ModeInfo * mi)
117 {
118         double      range;
119         discretestruct *hp;
120
121         MI_INIT (mi, discretes, free_discrete);
122         hp = &discretes[MI_SCREEN(mi)];
123
124         hp->maxx = MI_WIDTH(mi);
125         hp->maxy = MI_HEIGHT(mi);
126 #ifdef TEST
127         hp->op = TEST;
128 #else
129         hp->op = bias[LRAND() % BIASES];
130 #endif
131         switch (hp->op) {
132                 case HSHOE:
133                         hp->ic = 0;
134                         hp->jc = 0;
135                         hp->is = hp->maxx / (4);
136                         hp->js = hp->maxy / (4);
137                         hp->a = 0.5;
138                         hp->b = 0.5;
139                         hp->c = 0.2;
140                         hp->d = -1.25;
141                         hp->e = 1;
142                         hp->i = hp->j = 0.0;
143                         break;
144                 case DELOG:
145                         hp->ic = 0.5;
146                         hp->jc = 0.3;
147                         hp->is = hp->maxx / 1.5;
148                         hp->js = hp->maxy / 1.5;
149                         hp->a = 2.176399;
150                         hp->i = hp->j = 0.01;
151                         break;
152                 case HENON:
153                         hp->jc = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.4;
154                         hp->ic = 1.3 * (1 - (hp->jc * hp->jc) / (0.4 * 0.4));
155                         hp->is = hp->maxx;
156                         hp->js = hp->maxy * 1.5;
157                         hp->a = 1;
158                         hp->b = 1.4;
159                         hp->c = 0.3;
160                         hp->i = hp->j = 0;
161                         break;
162                 case SQRT:
163                         hp->ic = 0;
164                         hp->jc = 0;
165                         hp->is = 1;
166                         hp->js = 1;
167                         range = sqrt((double) hp->maxx * 2 * hp->maxx * 2 +
168                                      (double) hp->maxy * 2 * hp->maxy * 2) /
169                                 (10.0 + LRAND() % 10);
170
171                         hp->a = (LRAND() / MAXRAND) * range - range / 2.0;
172                         hp->b = (LRAND() / MAXRAND) * range - range / 2.0;
173                         hp->c = (LRAND() / MAXRAND) * range - range / 2.0;
174                         if (!(LRAND() % 2))
175                                 hp->c = 0.0;
176                         hp->i = hp->j = 0.0;
177                         break;
178                 case STANDARD:
179                         hp->ic = M_PI;
180                         hp->jc = M_PI;
181                         hp->is = hp->maxx / (M_PI * 2);
182                         hp->js = hp->maxy / (M_PI * 2);
183                         hp->a = 0;      /* decay */
184                         hp->b = (LRAND() / MAXRAND) * 2.0;
185                         hp->c = 0;
186                         hp->i = M_PI;
187                         hp->j = M_PI;
188                         break;
189                 case BIRDIE:
190                         hp->ic = 0;
191                         hp->jc = 0;
192                         hp->is = hp->maxx / 2;
193                         hp->js = hp->maxy / 2;
194                         hp->a = 1.99 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.2;
195                         hp->b = 0;
196                         hp->c = 0.8 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.1;
197                         hp->i = hp->j = 0;
198                         break;
199                 case TRIG:
200                         hp->a = 5;
201                         hp->b = 0.5 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.3;
202                         hp->ic = hp->a;
203                         hp->jc = 0;
204                         hp->is = hp->maxx / (hp->b * 20);
205                         hp->js = hp->maxy / (hp->b * 20);
206                         hp->i = hp->j = 0;
207                         break;
208                 case CUBIC:
209                         hp->a = 2.77;
210                         hp->b = 0.1 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.1;
211                         hp->ic = 0;
212                         hp->jc = 0;
213                         hp->is = hp->maxx / 4;
214                         hp->js = hp->maxy / 4;
215                         hp->i = hp->j = 0.1;
216                         break;
217                 case AILUJ:
218                         {
219                                 int         i;
220                                 double      x, y, xn, yn;
221
222                                 hp->ic = 0;
223                                 hp->jc = 0;
224                                 hp->is = hp->maxx / 4;
225                                 hp->js = hp->maxx / 4;
226                                 do {
227                                         hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 1.5 - 0.5;
228                                         hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 1.5;
229                                         x = y = 0;
230 #define MAXITER 10
231                                         for (i = 0; i < MAXITER && x * x + y * y < 13; i++) {   /* 'Brot calc */
232                                                 xn = x * x - y * y + hp->a;
233                                                 yn = 2 * x * y + hp->b;
234                                                 x = xn;
235                                                 y = yn;
236                                         }
237                                 } while (i < MAXITER);  /* wait for a connected set */
238                                 hp->i = hp->j = 0.1;
239                                 break;
240                         }
241         }
242         hp->pix = 0;
243         hp->inc = 0;
244
245         if (hp->pointBuffer == NULL) {
246                 hp->pointBuffer = (XPoint *) malloc(sizeof (XPoint) * MI_COUNT(mi));
247                 /* if fails will check later */
248         }
249
250 #ifndef STANDALONE
251         /* Clear the background. */
252         MI_CLEARWINDOW(mi);
253 #endif
254
255         XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
256         hp->count = 0;
257     hp->sqrt_sign = 1;
258     hp->std_sign = 1;
259 }
260
261
262 static void
263 draw_discrete_1 (ModeInfo * mi)
264 {
265         Display    *dsp = MI_DISPLAY(mi);
266         Window      win = MI_WINDOW(mi);
267         double      oldj, oldi;
268         int         count = MI_COUNT(mi);
269         int         cycles = MI_CYCLES(mi);
270         int         k;
271         XPoint     *xp;
272         GC          gc = MI_GC(mi);
273         discretestruct *hp;
274
275         if (discretes == NULL)
276                 return;
277         hp = &discretes[MI_SCREEN(mi)];
278         if (hp->pointBuffer == NULL)
279                 return;
280
281         k = count;
282         xp = hp->pointBuffer;
283
284         hp->inc++;
285
286         MI_IS_DRAWN(mi) = True;
287
288         if (MI_NPIXELS(mi) > 2) {
289                 XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
290                 if (++hp->pix >= MI_NPIXELS(mi))
291                         hp->pix = 0;
292         }
293         while (k--) {
294                 oldj = hp->j;
295                 oldi = hp->i;
296                 switch (hp->op) {
297                         case HSHOE:
298                                 {
299                                         int         i;
300
301 #if 0
302                                         if (!k) {
303                                                 XSetForeground(dsp, gc, MI_BLACK_PIXEL(mi));
304                                                 XFillRectangle(dsp, win, gc, 0, 0, hp->maxx, hp->maxy);
305                                                 XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
306                                         } else
307 #endif
308 #define HD
309 #ifdef HD
310                                         if (k < count / 4) {
311                                                 hp->i = ((double) k / count) * 8 - 1;
312                                                 hp->j = 1;
313                                         } else if (k < count / 2) {
314                                                 hp->i = 1;
315                                                 hp->j = 3 - ((double) k / count) * 8;
316                                         } else if (k < 3 * count / 4) {
317                                                 hp->i = 5 - ((double) k / count) * 8;
318                                                 hp->j = -1;
319                                         } else {
320                                                 hp->i = -1;
321                                                 hp->j = ((double) k / count) * 8 - 7;
322                                         }
323                                         for (i = 1; i < (hp->inc % 15); i++) {
324                                                 oldj = hp->j;
325                                                 oldi = hp->i;
326 #endif
327                                                 hp->i = (hp->a * oldi + hp->b) * oldj;
328                                                 hp->j = (hp->e - hp->d + hp->c * oldi) * oldj * oldj - hp->c * oldi + hp->d;
329 #ifdef HD
330                                         }
331 #endif
332                                         break;
333                                 }
334                         case DELOG:
335                                 hp->j = oldi;
336                                 hp->i = hp->a * oldi * (1 - oldj);
337                                 break;
338                         case HENON:
339                                 hp->i = oldj + hp->a - hp->b * oldi * oldi;
340                                 hp->j = hp->c * oldi;
341                                 break;
342                         case SQRT:
343                                 if (k) {
344                                         hp->j = hp->a + hp->i;
345                                         hp->i = -oldj + (hp->i < 0
346                                         ? sqrt(fabs(hp->b * (hp->i - hp->c)))
347                                                          : -sqrt(fabs(hp->b * (hp->i - hp->c))));
348                                 } else {
349                                         hp->i = (hp->sqrt_sign ? 1 : -1) * hp->inc * hp->maxx / cycles / 2;
350                                         hp->j = hp->a + hp->i;
351                                         hp->sqrt_sign = !hp->sqrt_sign;
352                                 }
353                                 break;
354                         case STANDARD:
355                                 if (k) {
356                                         hp->j = (1 - hp->a) * oldj + hp->b * sin(oldi) + hp->a * hp->c;
357                                         hp->j = fmod(hp->j + 2 * M_PI, 2 * M_PI);
358                                         hp->i = oldi + hp->j;
359                                         hp->i = fmod(hp->i + 2 * M_PI, 2 * M_PI);
360                                 } else {
361                                         hp->j = M_PI + fmod((hp->std_sign ? 1 : -1) * hp->inc * 2 * M_PI / (cycles - 0.5), M_PI);
362                                         hp->i = M_PI;
363                                         hp->std_sign = !hp->std_sign;
364                                 }
365                                 break;
366                         case BIRDIE:
367                                 hp->j = oldi;
368                                 hp->i = (1 - hp->c) * cos(M_PI * hp->a * oldj) + hp->c * hp->b;
369                                 hp->b = oldj;
370                                 break;
371                         case TRIG:
372                                 {
373                                         double      r2 = oldi * oldi + oldj * oldj;
374
375                                         hp->i = hp->a + hp->b * (oldi * cos(r2) - oldj * sin(r2));
376                                         hp->j = hp->b * (oldj * cos(r2) + oldi * sin(r2));
377                                 }
378                                 break;
379                         case CUBIC:
380                                 hp->i = oldj;
381                                 hp->j = hp->a * oldj - oldj * oldj * oldj - hp->b * oldi;
382                                 break;
383                         case AILUJ:
384                                 hp->i = ((LRAND() < MAXRAND / 2) ? -1 : 1) *
385                                         sqrt(((oldi - hp->a) +
386                                               sqrt((oldi - hp->a) * (oldi - hp->a) + (oldj - hp->b) * (oldj - hp->b))) / 2);
387                                 if (hp->i < 0.00000001 && hp->i > -0.00000001)
388                                         hp->i = (hp->i > 0.0) ? 0.00000001 : -0.00000001;
389                                 hp->j = (oldj - hp->b) / (2 * hp->i);
390                                 break;
391                 }
392                 xp->x = hp->maxx / 2 + (int) ((hp->i - hp->ic) * hp->is);
393                 xp->y = hp->maxy / 2 - (int) ((hp->j - hp->jc) * hp->js);
394                 xp++;
395         }
396         XDrawPoints(dsp, win, gc, hp->pointBuffer, count, CoordModeOrigin);
397 }
398
399 ENTRYPOINT void
400 draw_discrete (ModeInfo * mi)
401 {
402   discretestruct *hp = &discretes[MI_SCREEN(mi)];
403   int cycles = MI_CYCLES(mi);
404   int i;
405
406   if (hp->eraser) {
407     hp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), hp->eraser);
408     if (!hp->eraser)
409       init_discrete(mi);
410     return;
411   }
412
413   for (i = 0; i < 10; i++) {
414     draw_discrete_1 (mi);
415     hp->count++;
416   }
417
418   if (hp->count > cycles) {
419     hp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), hp->eraser);
420   }
421 }
422
423
424 ENTRYPOINT void
425 reshape_discrete(ModeInfo * mi, int width, int height)
426 {
427   discretestruct *hp = &discretes[MI_SCREEN(mi)];
428   hp->maxx = width;
429   hp->maxy = height;
430   XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
431 }
432
433 static void
434 free_discrete(ModeInfo * mi)
435 {
436         discretestruct *hp = &discretes[MI_SCREEN(mi)];
437
438         if (hp->pointBuffer != NULL) {
439                 (void) free((void *) hp->pointBuffer);
440                 /* hp->pointBuffer = NULL; */
441         }
442 }
443
444 ENTRYPOINT void
445 refresh_discrete(ModeInfo * mi)
446 {
447         MI_CLEARWINDOW(mi);
448 }
449
450 ENTRYPOINT Bool
451 discrete_handle_event (ModeInfo *mi, XEvent *event)
452 {
453   discretestruct *hp = &discretes[MI_SCREEN(mi)];
454   if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
455     {
456       hp->count = MI_CYCLES(mi);
457       return True;
458     }
459   return False;
460 }
461
462
463
464 XSCREENSAVER_MODULE ("Discrete", discrete)
465
466 #endif /* MODE_discrete */