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: 20000 \n" \
40 "*fpsSolid: true \n" \
41 "*ignoreRotation: True \n" \
43 # define SMOOTH_COLORS
44 # define release_discrete 0
45 # include "xlockmore.h" /* in xscreensaver distribution */
47 #else /* STANDALONE */
48 # include "xlock.h" /* in xlockmore distribution */
49 #endif /* STANDALONE */
53 ENTRYPOINT ModeSpecOpt discrete_opts =
54 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
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};
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 */
103 int sqrt_sign, std_sign;
106 eraser_state *eraser;
111 static discretestruct *discretes = (discretestruct *) NULL;
113 static void free_discrete(ModeInfo * mi);
116 init_discrete (ModeInfo * mi)
121 MI_INIT (mi, discretes, free_discrete);
122 hp = &discretes[MI_SCREEN(mi)];
124 hp->maxx = MI_WIDTH(mi);
125 hp->maxy = MI_HEIGHT(mi);
129 hp->op = bias[LRAND() % BIASES];
135 hp->is = hp->maxx / (4);
136 hp->js = hp->maxy / (4);
147 hp->is = hp->maxx / 1.5;
148 hp->js = hp->maxy / 1.5;
150 hp->i = hp->j = 0.01;
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));
156 hp->js = hp->maxy * 1.5;
167 range = sqrt((double) hp->maxx * 2 * hp->maxx * 2 +
168 (double) hp->maxy * 2 * hp->maxy * 2) /
169 (10.0 + LRAND() % 10);
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;
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;
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;
196 hp->c = 0.8 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.1;
201 hp->b = 0.5 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.3;
204 hp->is = hp->maxx / (hp->b * 20);
205 hp->js = hp->maxy / (hp->b * 20);
210 hp->b = 0.1 + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.1;
213 hp->is = hp->maxx / 4;
214 hp->js = hp->maxy / 4;
224 hp->is = hp->maxx / 4;
225 hp->js = hp->maxx / 4;
227 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 1.5 - 0.5;
228 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 1.5;
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;
237 } while (i < MAXITER); /* wait for a connected set */
245 if (hp->pointBuffer == NULL) {
246 hp->pointBuffer = (XPoint *) malloc(sizeof (XPoint) * MI_COUNT(mi));
247 /* if fails will check later */
251 /* Clear the background. */
255 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
263 draw_discrete_1 (ModeInfo * mi)
265 Display *dsp = MI_DISPLAY(mi);
266 Window win = MI_WINDOW(mi);
268 int count = MI_COUNT(mi);
269 int cycles = MI_CYCLES(mi);
275 if (discretes == NULL)
277 hp = &discretes[MI_SCREEN(mi)];
278 if (hp->pointBuffer == NULL)
282 xp = hp->pointBuffer;
286 MI_IS_DRAWN(mi) = True;
288 if (MI_NPIXELS(mi) > 2) {
289 XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
290 if (++hp->pix >= MI_NPIXELS(mi))
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));
311 hp->i = ((double) k / count) * 8 - 1;
313 } else if (k < count / 2) {
315 hp->j = 3 - ((double) k / count) * 8;
316 } else if (k < 3 * count / 4) {
317 hp->i = 5 - ((double) k / count) * 8;
321 hp->j = ((double) k / count) * 8 - 7;
323 for (i = 1; i < (hp->inc % 15); i++) {
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;
336 hp->i = hp->a * oldi * (1 - oldj);
339 hp->i = oldj + hp->a - hp->b * oldi * oldi;
340 hp->j = hp->c * oldi;
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))));
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;
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);
361 hp->j = M_PI + fmod((hp->std_sign ? 1 : -1) * hp->inc * 2 * M_PI / (cycles - 0.5), M_PI);
363 hp->std_sign = !hp->std_sign;
368 hp->i = (1 - hp->c) * cos(M_PI * hp->a * oldj) + hp->c * hp->b;
373 double r2 = oldi * oldi + oldj * oldj;
375 hp->i = hp->a + hp->b * (oldi * cos(r2) - oldj * sin(r2));
376 hp->j = hp->b * (oldj * cos(r2) + oldi * sin(r2));
381 hp->j = hp->a * oldj - oldj * oldj * oldj - hp->b * oldi;
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);
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);
396 XDrawPoints(dsp, win, gc, hp->pointBuffer, count, CoordModeOrigin);
400 draw_discrete (ModeInfo * mi)
402 discretestruct *hp = &discretes[MI_SCREEN(mi)];
403 int cycles = MI_CYCLES(mi);
407 hp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), hp->eraser);
413 for (i = 0; i < 10; i++) {
414 draw_discrete_1 (mi);
418 if (hp->count > cycles) {
419 hp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), hp->eraser);
425 reshape_discrete(ModeInfo * mi, int width, int height)
427 discretestruct *hp = &discretes[MI_SCREEN(mi)];
430 XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
434 free_discrete(ModeInfo * mi)
436 discretestruct *hp = &discretes[MI_SCREEN(mi)];
438 if (hp->pointBuffer != NULL) {
439 (void) free((void *) hp->pointBuffer);
440 /* hp->pointBuffer = NULL; */
445 refresh_discrete(ModeInfo * mi)
451 discrete_handle_event (ModeInfo *mi, XEvent *event)
453 discretestruct *hp = &discretes[MI_SCREEN(mi)];
454 if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
456 hp->count = MI_CYCLES(mi);
464 XSCREENSAVER_MODULE ("Discrete", discrete)
466 #endif /* MODE_discrete */