http://slackware.bholcomb.com/slackware/slackware-11.0/source/xap/xscreensaver/xscree...
[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: 1000 \n" \
37                                         "*count: 4096 \n" \
38                                         "*cycles: 2500 \n" \
39                                         "*ncolors: 100 \n"
40 # define SMOOTH_COLORS
41 # define reshape_discrete 0
42 # define discrete_handle_event 0
43 # include "xlockmore.h"         /* in xscreensaver distribution */
44 # include "erase.h"
45 #else /* STANDALONE */
46 # include "xlock.h"             /* in xlockmore distribution */
47 #endif /* STANDALONE */
48
49 #ifdef MODE_discrete
50
51 ENTRYPOINT ModeSpecOpt discrete_opts =
52 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
53
54 #ifdef USE_MODULES
55 ModStruct   discrete_description =
56 {"discrete", "init_discrete", "draw_discrete", "release_discrete",
57  "refresh_discrete", "init_discrete", (char *) NULL, &discrete_opts,
58  1000, 4096, 2500, 1, 64, 1.0, "",
59  "Shows various discrete maps", 0, NULL};
60
61 #endif
62
63 enum ftypes {
64         SQRT, BIRDIE, STANDARD, TRIG, CUBIC, HENON, AILUJ, HSHOE, DELOG
65 };
66
67 /*#define TEST STANDARD */
68
69 #define BIASES 18
70 static enum ftypes bias[BIASES] =
71 {
72         STANDARD, STANDARD, STANDARD, STANDARD,
73         SQRT, SQRT, SQRT, SQRT,
74         BIRDIE, BIRDIE, BIRDIE,
75         AILUJ, AILUJ, AILUJ,
76         TRIG, TRIG,
77         CUBIC,
78         HENON,
79 };
80
81 typedef struct {
82         int         maxx;
83         int         maxy;       /* max of the screen */
84         double      a;
85         double      b;
86         double      c;
87         double      d;
88         double      e;
89         double      i;
90         double      j;          /* discrete parameters */
91         double      ic;
92         double      jc;
93         double      is;
94         double      js;
95         int         inc;
96         int         pix;
97         enum ftypes op;
98         int         count;
99         XPoint     *pointBuffer;        /* pointer for XDrawPoints */
100
101     int sqrt_sign, std_sign;
102
103 #ifdef STANDALONE
104   eraser_state *eraser;
105 #endif
106
107 } discretestruct;
108
109 static discretestruct *discretes = (discretestruct *) NULL;
110
111 ENTRYPOINT void
112 init_discrete (ModeInfo * mi)
113 {
114         double      range;
115         discretestruct *hp;
116
117         if (discretes == NULL) {
118                 if ((discretes =
119                      (discretestruct *) calloc(MI_NUM_SCREENS(mi),
120                                            sizeof (discretestruct))) == NULL)
121                         return;
122         }
123         hp = &discretes[MI_SCREEN(mi)];
124
125         hp->maxx = MI_WIDTH(mi);
126         hp->maxy = MI_HEIGHT(mi);
127 #ifdef TEST
128         hp->op = TEST;
129 #else
130         hp->op = bias[LRAND() % BIASES];
131 #endif
132         switch (hp->op) {
133                 case HSHOE:
134                         hp->ic = 0;
135                         hp->jc = 0;
136                         hp->is = hp->maxx / (4);
137                         hp->js = hp->maxy / (4);
138                         hp->a = 0.5;
139                         hp->b = 0.5;
140                         hp->c = 0.2;
141                         hp->d = -1.25;
142                         hp->e = 1;
143                         hp->i = hp->j = 0.0;
144                         break;
145                 case DELOG:
146                         hp->ic = 0.5;
147                         hp->jc = 0.3;
148                         hp->is = hp->maxx / 1.5;
149                         hp->js = hp->maxy / 1.5;
150                         hp->a = 2.176399;
151                         hp->i = hp->j = 0.01;
152                         break;
153                 case HENON:
154                         hp->jc = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.4;
155                         hp->ic = 1.3 * (1 - (hp->jc * hp->jc) / (0.4 * 0.4));
156                         hp->is = hp->maxx;
157                         hp->js = hp->maxy * 1.5;
158                         hp->a = 1;
159                         hp->b = 1.4;
160                         hp->c = 0.3;
161                         hp->i = hp->j = 0;
162                         break;
163                 case SQRT:
164                         hp->ic = 0;
165                         hp->jc = 0;
166                         hp->is = 1;
167                         hp->js = 1;
168                         range = sqrt((double) hp->maxx * 2 * hp->maxx * 2 +
169                                      (double) hp->maxy * 2 * hp->maxy * 2) /
170                                 (10.0 + LRAND() % 10);
171
172                         hp->a = (LRAND() / MAXRAND) * range - range / 2.0;
173                         hp->b = (LRAND() / MAXRAND) * range - range / 2.0;
174                         hp->c = (LRAND() / MAXRAND) * range - range / 2.0;
175                         if (!(LRAND() % 2))
176                                 hp->c = 0.0;
177                         hp->i = hp->j = 0.0;
178                         break;
179                 case STANDARD:
180                         hp->ic = M_PI;
181                         hp->jc = M_PI;
182                         hp->is = hp->maxx / (M_PI * 2);
183                         hp->js = hp->maxy / (M_PI * 2);
184                         hp->a = 0;      /* decay */
185                         hp->b = (LRAND() / MAXRAND) * 2.0;
186                         hp->c = 0;
187                         hp->i = M_PI;
188                         hp->j = M_PI;
189                         break;
190                 case BIRDIE:
191                         hp->ic = 0;
192                         hp->jc = 0;
193                         hp->is = hp->maxx / 2;
194                         hp->js = hp->maxy / 2;
195                         hp->a = 1.99 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.2;
196                         hp->b = 0;
197                         hp->c = 0.8 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.1;
198                         hp->i = hp->j = 0;
199                         break;
200                 case TRIG:
201                         hp->a = 5;
202                         hp->b = 0.5 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.3;
203                         hp->ic = hp->a;
204                         hp->jc = 0;
205                         hp->is = hp->maxx / (hp->b * 20);
206                         hp->js = hp->maxy / (hp->b * 20);
207                         hp->i = hp->j = 0;
208                         break;
209                 case CUBIC:
210                         hp->a = 2.77;
211                         hp->b = 0.1 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.1;
212                         hp->ic = 0;
213                         hp->jc = 0;
214                         hp->is = hp->maxx / 4;
215                         hp->js = hp->maxy / 4;
216                         hp->i = hp->j = 0.1;
217                         break;
218                 case AILUJ:
219                         {
220                                 int         i;
221                                 double      x, y, xn, yn;
222
223                                 hp->ic = 0;
224                                 hp->jc = 0;
225                                 hp->is = hp->maxx / 4;
226                                 hp->js = hp->maxx / 4;
227                                 do {
228                                         hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 1.5 - 0.5;
229                                         hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 1.5;
230                                         x = y = 0;
231 #define MAXITER 10
232                                         for (i = 0; i < MAXITER && x * x + y * y < 13; i++) {   /* 'Brot calc */
233                                                 xn = x * x - y * y + hp->a;
234                                                 yn = 2 * x * y + hp->b;
235                                                 x = xn;
236                                                 y = yn;
237                                         }
238                                 } while (i < MAXITER);  /* wait for a connected set */
239                                 hp->i = hp->j = 0.1;
240                                 break;
241                         }
242         }
243         hp->pix = 0;
244         hp->inc = 0;
245
246         if (hp->pointBuffer == NULL) {
247                 hp->pointBuffer = (XPoint *) malloc(sizeof (XPoint) * MI_COUNT(mi));
248                 /* if fails will check later */
249         }
250
251 #ifndef STANDALONE
252         /* Clear the background. */
253         MI_CLEARWINDOW(mi);
254 #endif
255
256         XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
257         hp->count = 0;
258     hp->sqrt_sign = 1;
259     hp->std_sign = 1;
260 }
261
262
263 ENTRYPOINT void
264 draw_discrete (ModeInfo * mi)
265 {
266         Display    *dsp = MI_DISPLAY(mi);
267         Window      win = MI_WINDOW(mi);
268         double      oldj, oldi;
269         int         count = MI_COUNT(mi);
270         int         cycles = MI_CYCLES(mi);
271         int         k;
272         XPoint     *xp;
273         GC          gc = MI_GC(mi);
274         discretestruct *hp;
275
276         if (discretes == NULL)
277                 return;
278         hp = &discretes[MI_SCREEN(mi)];
279         if (hp->pointBuffer == NULL)
280                 return;
281
282     if (hp->eraser) {
283       hp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), hp->eraser);
284       return;
285     }
286
287         k = count;
288         xp = hp->pointBuffer;
289
290         hp->inc++;
291
292         MI_IS_DRAWN(mi) = True;
293
294         if (MI_NPIXELS(mi) > 2) {
295                 XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
296                 if (++hp->pix >= MI_NPIXELS(mi))
297                         hp->pix = 0;
298         }
299         while (k--) {
300                 oldj = hp->j;
301                 oldi = hp->i;
302                 switch (hp->op) {
303                         case HSHOE:
304                                 {
305                                         int         i;
306
307 #if 0
308                                         if (!k) {
309                                                 XSetForeground(dsp, gc, MI_BLACK_PIXEL(mi));
310                                                 XFillRectangle(dsp, win, gc, 0, 0, hp->maxx, hp->maxy);
311                                                 XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
312                                         } else
313 #endif
314 #define HD
315 #ifdef HD
316                                         if (k < count / 4) {
317                                                 hp->i = ((double) k / count) * 8 - 1;
318                                                 hp->j = 1;
319                                         } else if (k < count / 2) {
320                                                 hp->i = 1;
321                                                 hp->j = 3 - ((double) k / count) * 8;
322                                         } else if (k < 3 * count / 4) {
323                                                 hp->i = 5 - ((double) k / count) * 8;
324                                                 hp->j = -1;
325                                         } else {
326                                                 hp->i = -1;
327                                                 hp->j = ((double) k / count) * 8 - 7;
328                                         }
329                                         for (i = 1; i < (hp->inc % 15); i++) {
330                                                 oldj = hp->j;
331                                                 oldi = hp->i;
332 #endif
333                                                 hp->i = (hp->a * oldi + hp->b) * oldj;
334                                                 hp->j = (hp->e - hp->d + hp->c * oldi) * oldj * oldj - hp->c * oldi + hp->d;
335 #ifdef HD
336                                         }
337 #endif
338                                         break;
339                                 }
340                         case DELOG:
341                                 hp->j = oldi;
342                                 hp->i = hp->a * oldi * (1 - oldj);
343                                 break;
344                         case HENON:
345                                 hp->i = oldj + hp->a - hp->b * oldi * oldi;
346                                 hp->j = hp->c * oldi;
347                                 break;
348                         case SQRT:
349                                 if (k) {
350                                         hp->j = hp->a + hp->i;
351                                         hp->i = -oldj + (hp->i < 0
352                                         ? sqrt(fabs(hp->b * (hp->i - hp->c)))
353                                                          : -sqrt(fabs(hp->b * (hp->i - hp->c))));
354                                 } else {
355                                         hp->i = (hp->sqrt_sign ? 1 : -1) * hp->inc * hp->maxx / cycles / 2;
356                                         hp->j = hp->a + hp->i;
357                                         hp->sqrt_sign = !hp->sqrt_sign;
358                                 }
359                                 break;
360                         case STANDARD:
361                                 if (k) {
362                                         hp->j = (1 - hp->a) * oldj + hp->b * sin(oldi) + hp->a * hp->c;
363                                         hp->j = fmod(hp->j + 2 * M_PI, 2 * M_PI);
364                                         hp->i = oldi + hp->j;
365                                         hp->i = fmod(hp->i + 2 * M_PI, 2 * M_PI);
366                                 } else {
367                                         hp->j = M_PI + fmod((hp->std_sign ? 1 : -1) * hp->inc * 2 * M_PI / (cycles - 0.5), M_PI);
368                                         hp->i = M_PI;
369                                         hp->std_sign = !hp->std_sign;
370                                 }
371                                 break;
372                         case BIRDIE:
373                                 hp->j = oldi;
374                                 hp->i = (1 - hp->c) * cos(M_PI * hp->a * oldj) + hp->c * hp->b;
375                                 hp->b = oldj;
376                                 break;
377                         case TRIG:
378                                 {
379                                         double      r2 = oldi * oldi + oldj * oldj;
380
381                                         hp->i = hp->a + hp->b * (oldi * cos(r2) - oldj * sin(r2));
382                                         hp->j = hp->b * (oldj * cos(r2) + oldi * sin(r2));
383                                 }
384                                 break;
385                         case CUBIC:
386                                 hp->i = oldj;
387                                 hp->j = hp->a * oldj - oldj * oldj * oldj - hp->b * oldi;
388                                 break;
389                         case AILUJ:
390                                 hp->i = ((LRAND() < MAXRAND / 2) ? -1 : 1) *
391                                         sqrt(((oldi - hp->a) +
392                                               sqrt((oldi - hp->a) * (oldi - hp->a) + (oldj - hp->b) * (oldj - hp->b))) / 2);
393                                 if (hp->i < 0.00000001 && hp->i > -0.00000001)
394                                         hp->i = (hp->i > 0.0) ? 0.00000001 : -0.00000001;
395                                 hp->j = (oldj - hp->b) / (2 * hp->i);
396                                 break;
397                 }
398                 xp->x = hp->maxx / 2 + (int) ((hp->i - hp->ic) * hp->is);
399                 xp->y = hp->maxy / 2 - (int) ((hp->j - hp->jc) * hp->js);
400                 xp++;
401         }
402         XDrawPoints(dsp, win, gc, hp->pointBuffer, count, CoordModeOrigin);
403         if (++hp->count > cycles) {
404 #ifdef STANDALONE
405       hp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), hp->eraser);
406 #endif /* STANDALONE */
407                 init_discrete(mi);
408         }
409 }
410
411 ENTRYPOINT void
412 release_discrete(ModeInfo * mi)
413 {
414         if (discretes != NULL) {
415                 int         screen;
416
417                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
418                         discretestruct *hp = &discretes[screen];
419
420                         if (hp->pointBuffer != NULL) {
421                                 (void) free((void *) hp->pointBuffer);
422                                 /* hp->pointBuffer = NULL; */
423                         }
424                 }
425                 (void) free((void *) discretes);
426                 discretes = (discretestruct *) NULL;
427         }
428 }
429
430 ENTRYPOINT void
431 refresh_discrete(ModeInfo * mi)
432 {
433         MI_CLEARWINDOW(mi);
434 }
435
436 XSCREENSAVER_MODULE ("Discrete", discrete)
437
438 #endif /* MODE_discrete */