1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* discrete --- chaotic mappings */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)discrete.c 5.00 2000/11/01 xlockmore";
10 * Copyright (c) 1996 by Tim Auckland <Tim.Auckland@Procket.com>
12 * Permission to use, copy, modify, and distribute this software and its
13 * documentation for any purpose and without fee is hereby granted,
14 * provided that the above copyright notice appear in all copies and that
15 * both that copyright notice and this permission notice appear in
16 * supporting documentation.
18 * This file is provided AS IS with no warranties of any kind. The author
19 * shall have no liability with respect to the infringement of copyrights,
20 * trade secrets or any patents by this file or any part thereof. In no
21 * event will the author be liable for any lost revenue or profits or
22 * other special, indirect and consequential damages.
24 * "discrete" shows a number of fractals based on the "discrete map"
25 * type of dynamical systems. They include a different way of looking
26 * at the HOPALONG system, an inverse julia-set iteration, the "Standard
27 * Map" and the "Bird in a Thornbush" fractal.
30 * 01-Nov-2000: Allocation checks
31 * 31-Jul-1997: Ported to xlockmore-4
32 * 08-Aug-1996: Adapted from hop.c Copyright (c) 1991 by Patrick J. Naughton.
37 #define PROGCLASS "Discrete"
38 #define HACK_INIT init_discrete
39 #define HACK_DRAW draw_discrete
40 #define discrete_opts xlockmore_opts
41 #define DEFAULTS "*delay: 1000 \n" \
46 #include "xlockmore.h" /* in xscreensaver distribution */
48 #else /* STANDALONE */
49 #include "xlock.h" /* in xlockmore distribution */
50 #endif /* STANDALONE */
54 ModeSpecOpt discrete_opts =
55 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
58 ModStruct discrete_description =
59 {"discrete", "init_discrete", "draw_discrete", "release_discrete",
60 "refresh_discrete", "init_discrete", (char *) NULL, &discrete_opts,
61 1000, 4096, 2500, 1, 64, 1.0, "",
62 "Shows various discrete maps", 0, NULL};
67 SQRT, BIRDIE, STANDARD, TRIG, CUBIC, HENON, AILUJ, HSHOE, DELOG
70 /*#define TEST STANDARD */
73 static enum ftypes bias[BIASES] =
75 STANDARD, STANDARD, STANDARD, STANDARD,
76 SQRT, SQRT, SQRT, SQRT,
77 BIRDIE, BIRDIE, BIRDIE,
86 int maxy; /* max of the screen */
93 double j; /* discrete parameters */
102 XPoint *pointBuffer; /* pointer for XDrawPoints */
105 static discretestruct *discretes = (discretestruct *) NULL;
108 init_discrete(ModeInfo * mi)
113 if (discretes == NULL) {
115 (discretestruct *) calloc(MI_NUM_SCREENS(mi),
116 sizeof (discretestruct))) == NULL)
119 hp = &discretes[MI_SCREEN(mi)];
121 hp->maxx = MI_WIDTH(mi);
122 hp->maxy = MI_HEIGHT(mi);
126 hp->op = bias[LRAND() % BIASES];
132 hp->is = hp->maxx / (4);
133 hp->js = hp->maxy / (4);
144 hp->is = hp->maxx / 1.5;
145 hp->js = hp->maxy / 1.5;
147 hp->i = hp->j = 0.01;
150 hp->jc = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.4;
151 hp->ic = 1.3 * (1 - (hp->jc * hp->jc) / (0.4 * 0.4));
153 hp->js = hp->maxy * 1.5;
164 range = sqrt((double) hp->maxx * 2 * hp->maxx * 2 +
165 (double) hp->maxy * 2 * hp->maxy * 2) /
166 (10.0 + LRAND() % 10);
168 hp->a = (LRAND() / MAXRAND) * range - range / 2.0;
169 hp->b = (LRAND() / MAXRAND) * range - range / 2.0;
170 hp->c = (LRAND() / MAXRAND) * range - range / 2.0;
178 hp->is = hp->maxx / (M_PI * 2);
179 hp->js = hp->maxy / (M_PI * 2);
180 hp->a = 0; /* decay */
181 hp->b = (LRAND() / MAXRAND) * 2.0;
189 hp->is = hp->maxx / 2;
190 hp->js = hp->maxy / 2;
191 hp->a = 1.99 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.2;
193 hp->c = 0.8 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.1;
198 hp->b = 0.5 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.3;
201 hp->is = hp->maxx / (hp->b * 20);
202 hp->js = hp->maxy / (hp->b * 20);
207 hp->b = 0.1 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.1;
210 hp->is = hp->maxx / 4;
211 hp->js = hp->maxy / 4;
221 hp->is = hp->maxx / 4;
222 hp->js = hp->maxx / 4;
224 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 1.5 - 0.5;
225 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 1.5;
228 for (i = 0; i < MAXITER && x * x + y * y < 13; i++) { /* 'Brot calc */
229 xn = x * x - y * y + hp->a;
230 yn = 2 * x * y + hp->b;
234 } while (i < MAXITER); /* wait for a connected set */
242 if (hp->pointBuffer == NULL) {
243 hp->pointBuffer = (XPoint *) malloc(sizeof (XPoint) * MI_COUNT(mi));
244 /* if fails will check later */
247 /* Clear the background. */
250 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
256 draw_discrete(ModeInfo * mi)
258 Display *dsp = MI_DISPLAY(mi);
259 Window win = MI_WINDOW(mi);
261 int count = MI_COUNT(mi);
262 int cycles = MI_CYCLES(mi);
268 if (discretes == NULL)
270 hp = &discretes[MI_SCREEN(mi)];
271 if (hp->pointBuffer == NULL)
275 xp = hp->pointBuffer;
279 MI_IS_DRAWN(mi) = True;
281 if (MI_NPIXELS(mi) > 2) {
282 XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
283 if (++hp->pix >= MI_NPIXELS(mi))
296 XSetForeground(dsp, gc, MI_BLACK_PIXEL(mi));
297 XFillRectangle(dsp, win, gc, 0, 0, hp->maxx, hp->maxy);
298 XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
304 hp->i = ((double) k / count) * 8 - 1;
306 } else if (k < count / 2) {
308 hp->j = 3 - ((double) k / count) * 8;
309 } else if (k < 3 * count / 4) {
310 hp->i = 5 - ((double) k / count) * 8;
314 hp->j = ((double) k / count) * 8 - 7;
316 for (i = 1; i < (hp->inc % 15); i++) {
320 hp->i = (hp->a * oldi + hp->b) * oldj;
321 hp->j = (hp->e - hp->d + hp->c * oldi) * oldj * oldj - hp->c * oldi + hp->d;
329 hp->i = hp->a * oldi * (1 - oldj);
332 hp->i = oldj + hp->a - hp->b * oldi * oldi;
333 hp->j = hp->c * oldi;
337 hp->j = hp->a + hp->i;
338 hp->i = -oldj + (hp->i < 0
339 ? sqrt(fabs(hp->b * (hp->i - hp->c)))
340 : -sqrt(fabs(hp->b * (hp->i - hp->c))));
344 hp->i = s * hp->inc * hp->maxx / cycles / 2;
345 hp->j = hp->a + hp->i;
351 hp->j = (1 - hp->a) * oldj + hp->b * sin(oldi) + hp->a * hp->c;
352 hp->j = fmod(hp->j + 2 * M_PI, 2 * M_PI);
353 hp->i = oldi + hp->j;
354 hp->i = fmod(hp->i + 2 * M_PI, 2 * M_PI);
358 hp->j = M_PI + fmod(s * hp->inc * 2 * M_PI / (cycles - 0.5), M_PI);
365 hp->i = (1 - hp->c) * cos(M_PI * hp->a * oldj) + hp->c * hp->b;
370 double r2 = oldi * oldi + oldj * oldj;
372 hp->i = hp->a + hp->b * (oldi * cos(r2) - oldj * sin(r2));
373 hp->j = hp->b * (oldj * cos(r2) + oldi * sin(r2));
378 hp->j = hp->a * oldj - oldj * oldj * oldj - hp->b * oldi;
381 hp->i = ((LRAND() < MAXRAND / 2) ? -1 : 1) *
382 sqrt(((oldi - hp->a) +
383 sqrt((oldi - hp->a) * (oldi - hp->a) + (oldj - hp->b) * (oldj - hp->b))) / 2);
384 if (hp->i < 0.00000001 && hp->i > -0.00000001)
385 hp->i = (hp->i > 0.0) ? 0.00000001 : -0.00000001;
386 hp->j = (oldj - hp->b) / (2 * hp->i);
389 xp->x = hp->maxx / 2 + (int) ((hp->i - hp->ic) * hp->is);
390 xp->y = hp->maxy / 2 - (int) ((hp->j - hp->jc) * hp->js);
393 XDrawPoints(dsp, win, gc, hp->pointBuffer, count, CoordModeOrigin);
394 if (++hp->count > cycles) {
396 erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
397 #endif /* STANDALONE */
403 release_discrete(ModeInfo * mi)
405 if (discretes != NULL) {
408 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
409 discretestruct *hp = &discretes[screen];
411 if (hp->pointBuffer != NULL) {
412 (void) free((void *) hp->pointBuffer);
413 /* hp->pointBuffer = NULL; */
416 (void) free((void *) discretes);
417 discretes = (discretestruct *) NULL;
422 refresh_discrete(ModeInfo * mi)
427 #endif /* MODE_discrete */