1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* discrete --- chaotic mappings */
5 static const char sccsid[] = "@(#)discrete.c 5.00 2000/11/01 xlockmore";
9 * Copyright (c) 1996 by Tim Auckland <Tim.Auckland@Procket.com>
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.
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.
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.
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.
36 #define PROGCLASS "Discrete"
37 #define HACK_INIT init_discrete
38 #define HACK_DRAW draw_discrete
39 #define discrete_opts xlockmore_opts
40 #define DEFAULTS "*delay: 1000 \n" \
45 #include "xlockmore.h" /* in xscreensaver distribution */
47 #else /* STANDALONE */
48 #include "xlock.h" /* in xlockmore distribution */
49 #endif /* STANDALONE */
53 ModeSpecOpt discrete_opts =
54 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
57 ModStruct discrete_description =
58 {"discrete", "init_discrete", "draw_discrete", "release_discrete",
59 "refresh_discrete", "init_discrete", (char *) NULL, &discrete_opts,
60 1000, 4096, 2500, 1, 64, 1.0, "",
61 "Shows various discrete maps", 0, NULL};
66 SQRT, BIRDIE, STANDARD, TRIG, CUBIC, HENON, AILUJ, HSHOE, DELOG
69 /*#define TEST STANDARD */
72 static enum ftypes bias[BIASES] =
74 STANDARD, STANDARD, STANDARD, STANDARD,
75 SQRT, SQRT, SQRT, SQRT,
76 BIRDIE, BIRDIE, BIRDIE,
85 int maxy; /* max of the screen */
92 double j; /* discrete parameters */
101 XPoint *pointBuffer; /* pointer for XDrawPoints */
104 static discretestruct *discretes = (discretestruct *) NULL;
107 init_discrete(ModeInfo * mi)
112 if (discretes == NULL) {
114 (discretestruct *) calloc(MI_NUM_SCREENS(mi),
115 sizeof (discretestruct))) == NULL)
118 hp = &discretes[MI_SCREEN(mi)];
120 hp->maxx = MI_WIDTH(mi);
121 hp->maxy = MI_HEIGHT(mi);
125 hp->op = bias[LRAND() % BIASES];
131 hp->is = hp->maxx / (4);
132 hp->js = hp->maxy / (4);
143 hp->is = hp->maxx / 1.5;
144 hp->js = hp->maxy / 1.5;
146 hp->i = hp->j = 0.01;
149 hp->jc = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.4;
150 hp->ic = 1.3 * (1 - (hp->jc * hp->jc) / (0.4 * 0.4));
152 hp->js = hp->maxy * 1.5;
163 range = sqrt((double) hp->maxx * 2 * hp->maxx * 2 +
164 (double) hp->maxy * 2 * hp->maxy * 2) /
165 (10.0 + LRAND() % 10);
167 hp->a = (LRAND() / MAXRAND) * range - range / 2.0;
168 hp->b = (LRAND() / MAXRAND) * range - range / 2.0;
169 hp->c = (LRAND() / MAXRAND) * range - range / 2.0;
177 hp->is = hp->maxx / (M_PI * 2);
178 hp->js = hp->maxy / (M_PI * 2);
179 hp->a = 0; /* decay */
180 hp->b = (LRAND() / MAXRAND) * 2.0;
188 hp->is = hp->maxx / 2;
189 hp->js = hp->maxy / 2;
190 hp->a = 1.99 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.2;
192 hp->c = 0.8 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.1;
197 hp->b = 0.5 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.3;
200 hp->is = hp->maxx / (hp->b * 20);
201 hp->js = hp->maxy / (hp->b * 20);
206 hp->b = 0.1 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.1;
209 hp->is = hp->maxx / 4;
210 hp->js = hp->maxy / 4;
220 hp->is = hp->maxx / 4;
221 hp->js = hp->maxx / 4;
223 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 1.5 - 0.5;
224 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 1.5;
227 for (i = 0; i < MAXITER && x * x + y * y < 13; i++) { /* 'Brot calc */
228 xn = x * x - y * y + hp->a;
229 yn = 2 * x * y + hp->b;
233 } while (i < MAXITER); /* wait for a connected set */
241 if (hp->pointBuffer == NULL) {
242 hp->pointBuffer = (XPoint *) malloc(sizeof (XPoint) * MI_COUNT(mi));
243 /* if fails will check later */
246 /* Clear the background. */
249 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
255 draw_discrete(ModeInfo * mi)
257 Display *dsp = MI_DISPLAY(mi);
258 Window win = MI_WINDOW(mi);
260 int count = MI_COUNT(mi);
261 int cycles = MI_CYCLES(mi);
267 if (discretes == NULL)
269 hp = &discretes[MI_SCREEN(mi)];
270 if (hp->pointBuffer == NULL)
274 xp = hp->pointBuffer;
278 MI_IS_DRAWN(mi) = True;
280 if (MI_NPIXELS(mi) > 2) {
281 XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
282 if (++hp->pix >= MI_NPIXELS(mi))
295 XSetForeground(dsp, gc, MI_BLACK_PIXEL(mi));
296 XFillRectangle(dsp, win, gc, 0, 0, hp->maxx, hp->maxy);
297 XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
303 hp->i = ((double) k / count) * 8 - 1;
305 } else if (k < count / 2) {
307 hp->j = 3 - ((double) k / count) * 8;
308 } else if (k < 3 * count / 4) {
309 hp->i = 5 - ((double) k / count) * 8;
313 hp->j = ((double) k / count) * 8 - 7;
315 for (i = 1; i < (hp->inc % 15); i++) {
319 hp->i = (hp->a * oldi + hp->b) * oldj;
320 hp->j = (hp->e - hp->d + hp->c * oldi) * oldj * oldj - hp->c * oldi + hp->d;
328 hp->i = hp->a * oldi * (1 - oldj);
331 hp->i = oldj + hp->a - hp->b * oldi * oldi;
332 hp->j = hp->c * oldi;
336 hp->j = hp->a + hp->i;
337 hp->i = -oldj + (hp->i < 0
338 ? sqrt(fabs(hp->b * (hp->i - hp->c)))
339 : -sqrt(fabs(hp->b * (hp->i - hp->c))));
343 hp->i = s * hp->inc * hp->maxx / cycles / 2;
344 hp->j = hp->a + hp->i;
350 hp->j = (1 - hp->a) * oldj + hp->b * sin(oldi) + hp->a * hp->c;
351 hp->j = fmod(hp->j + 2 * M_PI, 2 * M_PI);
352 hp->i = oldi + hp->j;
353 hp->i = fmod(hp->i + 2 * M_PI, 2 * M_PI);
357 hp->j = M_PI + fmod(s * hp->inc * 2 * M_PI / (cycles - 0.5), M_PI);
364 hp->i = (1 - hp->c) * cos(M_PI * hp->a * oldj) + hp->c * hp->b;
369 double r2 = oldi * oldi + oldj * oldj;
371 hp->i = hp->a + hp->b * (oldi * cos(r2) - oldj * sin(r2));
372 hp->j = hp->b * (oldj * cos(r2) + oldi * sin(r2));
377 hp->j = hp->a * oldj - oldj * oldj * oldj - hp->b * oldi;
380 hp->i = ((LRAND() < MAXRAND / 2) ? -1 : 1) *
381 sqrt(((oldi - hp->a) +
382 sqrt((oldi - hp->a) * (oldi - hp->a) + (oldj - hp->b) * (oldj - hp->b))) / 2);
383 if (hp->i < 0.00000001 && hp->i > -0.00000001)
384 hp->i = (hp->i > 0.0) ? 0.00000001 : -0.00000001;
385 hp->j = (oldj - hp->b) / (2 * hp->i);
388 xp->x = hp->maxx / 2 + (int) ((hp->i - hp->ic) * hp->is);
389 xp->y = hp->maxy / 2 - (int) ((hp->j - hp->jc) * hp->js);
392 XDrawPoints(dsp, win, gc, hp->pointBuffer, count, CoordModeOrigin);
393 if (++hp->count > cycles) {
395 erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
396 #endif /* STANDALONE */
402 release_discrete(ModeInfo * mi)
404 if (discretes != NULL) {
407 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
408 discretestruct *hp = &discretes[screen];
410 if (hp->pointBuffer != NULL) {
411 (void) free((void *) hp->pointBuffer);
412 /* hp->pointBuffer = NULL; */
415 (void) free((void *) discretes);
416 discretes = (discretestruct *) NULL;
421 refresh_discrete(ModeInfo * mi)
426 #endif /* MODE_discrete */