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 <tda10.geo@yahoo.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.
35 # define MODE_discrete
36 #define DEFAULTS "*delay: 1000 \n" \
40 # define SMOOTH_COLORS
41 # define reshape_discrete 0
42 # define discrete_handle_event 0
43 # include "xlockmore.h" /* in xscreensaver distribution */
45 #else /* STANDALONE */
46 # include "xlock.h" /* in xlockmore distribution */
47 #endif /* STANDALONE */
51 ENTRYPOINT ModeSpecOpt discrete_opts =
52 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
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};
64 SQRT, BIRDIE, STANDARD, TRIG, CUBIC, HENON, AILUJ, HSHOE, DELOG
67 /*#define TEST STANDARD */
70 static enum ftypes bias[BIASES] =
72 STANDARD, STANDARD, STANDARD, STANDARD,
73 SQRT, SQRT, SQRT, SQRT,
74 BIRDIE, BIRDIE, BIRDIE,
83 int maxy; /* max of the screen */
90 double j; /* discrete parameters */
99 XPoint *pointBuffer; /* pointer for XDrawPoints */
101 int sqrt_sign, std_sign;
104 eraser_state *eraser;
109 static discretestruct *discretes = (discretestruct *) NULL;
112 init_discrete (ModeInfo * mi)
117 if (discretes == NULL) {
119 (discretestruct *) calloc(MI_NUM_SCREENS(mi),
120 sizeof (discretestruct))) == NULL)
123 hp = &discretes[MI_SCREEN(mi)];
125 hp->maxx = MI_WIDTH(mi);
126 hp->maxy = MI_HEIGHT(mi);
130 hp->op = bias[LRAND() % BIASES];
136 hp->is = hp->maxx / (4);
137 hp->js = hp->maxy / (4);
148 hp->is = hp->maxx / 1.5;
149 hp->js = hp->maxy / 1.5;
151 hp->i = hp->j = 0.01;
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));
157 hp->js = hp->maxy * 1.5;
168 range = sqrt((double) hp->maxx * 2 * hp->maxx * 2 +
169 (double) hp->maxy * 2 * hp->maxy * 2) /
170 (10.0 + LRAND() % 10);
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;
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;
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;
197 hp->c = 0.8 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.1;
202 hp->b = 0.5 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.3;
205 hp->is = hp->maxx / (hp->b * 20);
206 hp->js = hp->maxy / (hp->b * 20);
211 hp->b = 0.1 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.1;
214 hp->is = hp->maxx / 4;
215 hp->js = hp->maxy / 4;
225 hp->is = hp->maxx / 4;
226 hp->js = hp->maxx / 4;
228 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 1.5 - 0.5;
229 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 1.5;
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;
238 } while (i < MAXITER); /* wait for a connected set */
246 if (hp->pointBuffer == NULL) {
247 hp->pointBuffer = (XPoint *) malloc(sizeof (XPoint) * MI_COUNT(mi));
248 /* if fails will check later */
252 /* Clear the background. */
256 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
264 draw_discrete (ModeInfo * mi)
266 Display *dsp = MI_DISPLAY(mi);
267 Window win = MI_WINDOW(mi);
269 int count = MI_COUNT(mi);
270 int cycles = MI_CYCLES(mi);
276 if (discretes == NULL)
278 hp = &discretes[MI_SCREEN(mi)];
279 if (hp->pointBuffer == NULL)
283 hp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), hp->eraser);
288 xp = hp->pointBuffer;
292 MI_IS_DRAWN(mi) = True;
294 if (MI_NPIXELS(mi) > 2) {
295 XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
296 if (++hp->pix >= MI_NPIXELS(mi))
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));
317 hp->i = ((double) k / count) * 8 - 1;
319 } else if (k < count / 2) {
321 hp->j = 3 - ((double) k / count) * 8;
322 } else if (k < 3 * count / 4) {
323 hp->i = 5 - ((double) k / count) * 8;
327 hp->j = ((double) k / count) * 8 - 7;
329 for (i = 1; i < (hp->inc % 15); i++) {
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;
342 hp->i = hp->a * oldi * (1 - oldj);
345 hp->i = oldj + hp->a - hp->b * oldi * oldi;
346 hp->j = hp->c * oldi;
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))));
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;
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);
367 hp->j = M_PI + fmod((hp->std_sign ? 1 : -1) * hp->inc * 2 * M_PI / (cycles - 0.5), M_PI);
369 hp->std_sign = !hp->std_sign;
374 hp->i = (1 - hp->c) * cos(M_PI * hp->a * oldj) + hp->c * hp->b;
379 double r2 = oldi * oldi + oldj * oldj;
381 hp->i = hp->a + hp->b * (oldi * cos(r2) - oldj * sin(r2));
382 hp->j = hp->b * (oldj * cos(r2) + oldi * sin(r2));
387 hp->j = hp->a * oldj - oldj * oldj * oldj - hp->b * oldi;
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);
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);
402 XDrawPoints(dsp, win, gc, hp->pointBuffer, count, CoordModeOrigin);
403 if (++hp->count > cycles) {
405 hp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), hp->eraser);
406 #endif /* STANDALONE */
412 release_discrete(ModeInfo * mi)
414 if (discretes != NULL) {
417 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
418 discretestruct *hp = &discretes[screen];
420 if (hp->pointBuffer != NULL) {
421 (void) free((void *) hp->pointBuffer);
422 /* hp->pointBuffer = NULL; */
425 (void) free((void *) discretes);
426 discretes = (discretestruct *) NULL;
431 refresh_discrete(ModeInfo * mi)
436 XSCREENSAVER_MODULE ("Discrete", discrete)
438 #endif /* MODE_discrete */