1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* strange --- strange attractors */
5 static const char sccsid[] = "@(#)strange.c 5.00 2000/11/01 xlockmore";
9 * Copyright (c) 1997 by Massimino Pascal <Pascal.Massimon@ens.fr>
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.
24 * 01-Nov-2000: Allocation checks
25 * 10-May-1997: jwz@jwz.org: turned into a standalone program.
26 * Made it render into an offscreen bitmap and then copy
27 * that onto the screen, to reduce flicker.
29 * strange attractors are not so hard to find...
34 # define DEFAULTS "*delay: 10000 \n" \
38 # define SMOOTH_COLORS
39 # define refresh_strange 0
40 # define reshape_strange 0
41 # define strange_handle_event 0
42 # include "xlockmore.h" /* from the xscreensaver distribution */
43 #else /* !STANDALONE */
44 # include "xlock.h" /* from the xlockmore distribution */
45 #endif /* !STANDALONE */
49 ENTRYPOINT ModeSpecOpt strange_opts =
50 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
53 ModStruct strange_description =
54 {"strange", "init_strange", "draw_strange", "release_strange",
55 "init_strange", "init_strange", (char *) NULL, &strange_opts,
56 1000, 1, 1, 1, 64, 1.0, "",
57 "Shows strange attractors", 0, NULL};
70 /* #define UNIT2 (3140*UNIT/1000) */
72 #define SKIP_FIRST 100
73 #define MAX_POINTS 5500
74 #define DBL_To_PRM(x) (PRM)( (DBL)(UNIT)*(x) )
77 #define DO_FOLD(a) (a)<0 ? -A->Fold[ (-(a))&(UNIT2-1) ] : A->Fold[ (a)&(UNIT2-1) ]
80 #define DO_FOLD(a) (a)<-UNIT2 ? -A->Fold[(-(a))%UNIT2] : (a)<0 ? -A->Fold[ -(a) ] :\
81 (a)>UNIT2 ? A->Fold[ (a)%UNIT2 ] : A->Fold[ (a) ]
82 #define DO_FOLD(a) DBL_To_PRM( sin( (DBL)(a)/UNIT ) )
83 #define DO_FOLD(a) (a)<0 ? DBL_To_PRM( exp( 16.0*(a)/UNIT2 ) )-1.0 : \
84 DBL_To_PRM( 1.0-exp( -16.0*(a)/UNIT2 ) )
87 /******************************************************************/
91 typedef struct _ATTRACTOR {
92 DBL Prm1[MAX_PRM], Prm2[MAX_PRM];
93 PRM Prm[MAX_PRM], *Fold;
94 void (*Iterate) (struct _ATTRACTOR *, PRM, PRM, PRM *, PRM *);
95 XPoint *Buffer1, *Buffer2;
97 int Col, Count, Speed;
99 Pixmap dbuf; /* jwz */
103 static ATTRACTOR *Root = (ATTRACTOR *) NULL;
105 static DBL Amp_Prm[MAX_PRM] =
107 1.0, 3.5, 3.5, 2.5, 4.7,
108 1.0, 3.5, 3.6, 2.5, 4.7,
109 1.0, 1.5, 2.2, 2.1, 3.5
111 static DBL Mid_Prm[MAX_PRM] =
113 0.0, 1.5, 0.0, .5, 1.5,
114 0.0, 1.5, 0.0, .5, 1.5,
115 0.0, 1.5, -1.0, -.5, 2.5,
119 Gauss_Rand(DBL c, DBL A, DBL S)
123 y = (DBL) LRAND() / MAXRAND;
124 y = A * (1.0 - exp(-y * y * S)) / (1.0 - exp(-S));
132 Random_Prm(DBL * Prm)
136 for (i = 0; i < MAX_PRM; ++i)
137 Prm[i] = Gauss_Rand(Mid_Prm[i], Amp_Prm[i], 4.0);
140 /***************************************************************/
142 /* 2 examples of non-linear map */
145 Iterate_X2(ATTRACTOR * A, PRM x, PRM y, PRM * xo, PRM * yo)
147 PRM xx, yy, xy, x2y, y2x, Tmp;
150 x2y = (xx * y) / UNIT;
152 y2x = (yy * x) / UNIT;
155 Tmp = A->Prm[1] * xx + A->Prm[2] * xy + A->Prm[3] * yy + A->Prm[4] * x2y;
156 Tmp = A->Prm[0] - y + (Tmp / UNIT);
158 Tmp = A->Prm[6] * xx + A->Prm[7] * xy + A->Prm[8] * yy + A->Prm[9] * y2x;
159 Tmp = A->Prm[5] + x + (Tmp / UNIT);
164 Iterate_X3(ATTRACTOR * A, PRM x, PRM y, PRM * xo, PRM * yo)
166 PRM xx, yy, xy, x2y, y2x, Tmp_x, Tmp_y, Tmp_z;
169 x2y = (xx * y) / UNIT;
171 y2x = (yy * x) / UNIT;
174 Tmp_x = A->Prm[1] * xx + A->Prm[2] * xy + A->Prm[3] * yy + A->Prm[4] * x2y;
175 Tmp_x = A->Prm[0] - y + (Tmp_x / UNIT);
176 Tmp_x = DO_FOLD(Tmp_x);
178 Tmp_y = A->Prm[6] * xx + A->Prm[7] * xy + A->Prm[8] * yy + A->Prm[9] * y2x;
179 Tmp_y = A->Prm[5] + x + (Tmp_y / UNIT);
181 Tmp_y = DO_FOLD(Tmp_y);
183 Tmp_z = A->Prm[11] * xx + A->Prm[12] * xy + A->Prm[13] * yy + A->Prm[14] * y2x;
184 Tmp_z = A->Prm[10] + x + (Tmp_z / UNIT);
185 Tmp_z = UNIT + Tmp_z * Tmp_z / UNIT;
187 *xo = (Tmp_x * UNIT) / Tmp_z;
188 *yo = (Tmp_y * UNIT) / Tmp_z;
191 static void (*Funcs[2]) (ATTRACTOR *, PRM, PRM, PRM *, PRM *) = {
192 Iterate_X2, Iterate_X3
195 /***************************************************************/
198 free_strange(Display *display, ATTRACTOR *A)
200 if (A->Buffer1 != NULL) {
201 (void) free((void *) A->Buffer1);
202 A->Buffer1 = (XPoint *) NULL;
204 if (A->Buffer2 != NULL) {
205 (void) free((void *) A->Buffer2);
206 A->Buffer2 = (XPoint *) NULL;
209 XFreePixmap(display, A->dbuf);
213 XFreeGC(display, A->dbuf_gc);
216 if (A->Fold != NULL) {
217 (void) free((void *) A->Fold);
218 A->Fold = (PRM *) NULL;
223 draw_strange(ModeInfo * mi)
229 Display *display = MI_DISPLAY(mi);
230 Window window = MI_WINDOW(mi);
233 void (*Iterate) (ATTRACTOR *, PRM, PRM, PRM *, PRM *);
234 PRM xmin, xmax, ymin, ymax;
239 A = &Root[MI_SCREEN(mi)];
244 Iterate = A->Iterate;
246 u = (DBL) (A->Count) / 1000.0;
247 for (j = MAX_PRM - 1; j >= 0; --j)
248 A->Prm[j] = DBL_To_PRM((1.0 - u) * A->Prm1[j] + u * A->Prm2[j]);
250 x = y = DBL_To_PRM(.0);
251 for (n = SKIP_FIRST; n; --n) {
252 (*Iterate) (A, x, y, &xo, &yo);
253 x = xo + NRAND(8) - 4;
254 y = yo + NRAND(8) - 4;
263 Lx = (DBL) A->Width / UNIT / 2.2;
264 Ly = (DBL) A->Height / UNIT / 2.2;
265 for (n = A->Max_Pt; n; --n) {
266 (*Iterate) (A, x, y, &xo, &yo);
267 Buf->x = (int) (Lx * (x + DBL_To_PRM(1.1)));
268 Buf->y = (int) (Ly * (DBL_To_PRM(1.1) - y));
269 /* (void) fprintf( stderr, "X,Y: %d %d ", Buf->x, Buf->y ); */
280 x = xo + NRAND(8) - 4;
281 y = yo + NRAND(8) - 4;
284 MI_IS_DRAWN(mi) = True;
286 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
288 if (A->dbuf != None) { /* jwz */
289 XSetForeground(display, A->dbuf_gc, 0);
290 /* XDrawPoints(display, A->dbuf, A->dbuf_gc, A->Buffer1,
291 Cur_Pt,CoordModeOrigin); */
292 XFillRectangle(display, A->dbuf, A->dbuf_gc, 0, 0, A->Width, A->Height);
294 XDrawPoints(display, window, gc, A->Buffer1, Cur_Pt, CoordModeOrigin);
296 if (MI_NPIXELS(mi) < 2)
297 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
299 XSetForeground(display, gc, MI_PIXEL(mi, A->Col % MI_NPIXELS(mi)));
301 if (A->dbuf != None) {
302 XSetForeground(display, A->dbuf_gc, 1);
303 XDrawPoints(display, A->dbuf, A->dbuf_gc, A->Buffer2, A->Cur_Pt,
305 XCopyPlane(display, A->dbuf, window, gc, 0, 0, A->Width, A->Height, 0, 0, 1);
307 XDrawPoints(display, window, gc, A->Buffer2, A->Cur_Pt, CoordModeOrigin);
310 A->Buffer1 = A->Buffer2;
313 if ((xmax - xmin < DBL_To_PRM(.2)) && (ymax - ymin < DBL_To_PRM(.2)))
314 A->Count += 4 * A->Speed;
316 A->Count += A->Speed;
317 if (A->Count >= 1000) {
318 for (i = MAX_PRM - 1; i >= 0; --i)
319 A->Prm1[i] = A->Prm2[i];
327 /***************************************************************/
330 init_strange(ModeInfo * mi)
332 Display *display = MI_DISPLAY(mi);
334 Window window = MI_WINDOW(mi);
337 ATTRACTOR *Attractor;
340 if ((Root = (ATTRACTOR *) calloc(MI_NUM_SCREENS(mi),
341 sizeof (ATTRACTOR))) == NULL)
344 Attractor = &Root[MI_SCREEN(mi)];
346 if (Attractor->Fold == NULL) {
349 if ((Attractor->Fold = (PRM *) calloc(UNIT2 + 1,
350 sizeof (PRM))) == NULL) {
351 free_strange(display, Attractor);
354 for (i = 0; i <= UNIT2; ++i) {
357 /* x = ( DBL )(i)/UNIT2; */
358 /* x = sin( M_PI/2.0*x ); */
361 /* x = x*(1.0-x)*4.0; */
362 x = (DBL) (i) / UNIT;
364 Attractor->Fold[i] = DBL_To_PRM(x);
367 if (Attractor->Buffer1 == NULL)
368 if ((Attractor->Buffer1 = (XPoint *) calloc(MAX_POINTS,
369 sizeof (XPoint))) == NULL) {
370 free_strange(display, Attractor);
373 if (Attractor->Buffer2 == NULL)
374 if ((Attractor->Buffer2 = (XPoint *) calloc(MAX_POINTS,
375 sizeof (XPoint))) == NULL) {
376 free_strange(display, Attractor);
379 Attractor->Max_Pt = MAX_POINTS;
381 Attractor->Width = MI_WIDTH(mi);
382 Attractor->Height = MI_HEIGHT(mi);
383 Attractor->Cur_Pt = 0;
384 Attractor->Count = 0;
385 Attractor->Col = NRAND(MI_NPIXELS(mi));
386 Attractor->Speed = 4;
388 Attractor->Iterate = Funcs[NRAND(2)];
389 Random_Prm(Attractor->Prm1);
390 Random_Prm(Attractor->Prm2);
392 if (Attractor->dbuf != None)
393 XFreePixmap(display, Attractor->dbuf);
394 Attractor->dbuf = XCreatePixmap(display, window,
395 Attractor->Width, Attractor->Height, 1);
396 /* Allocation checked */
397 if (Attractor->dbuf != None) {
403 gcv.graphics_exposures = False;
404 #endif /* HAVE_COCOA */
405 gcv.function = GXcopy;
407 if (Attractor->dbuf_gc != None)
408 XFreeGC(display, Attractor->dbuf_gc);
410 if ((Attractor->dbuf_gc = XCreateGC(display, Attractor->dbuf,
412 GCGraphicsExposures |
413 #endif /* HAVE_COCOA */
414 GCFunction | GCForeground | GCBackground,
416 XFreePixmap(display, Attractor->dbuf);
417 Attractor->dbuf = None;
419 XFillRectangle(display, Attractor->dbuf, Attractor->dbuf_gc,
420 0, 0, Attractor->Width, Attractor->Height);
421 XSetBackground(display, gc, MI_BLACK_PIXEL(mi));
422 XSetFunction(display, gc, GXcopy);
429 /* Do not want any exposure events from XCopyPlane */
430 XSetGraphicsExposures(display, MI_GC(mi), False);
433 /***************************************************************/
436 release_strange(ModeInfo * mi)
441 for (screen = 0; screen < MI_NUM_SCREENS(mi); ++screen)
442 free_strange(MI_DISPLAY(mi), &Root[screen]);
443 (void) free((void *) Root);
444 Root = (ATTRACTOR *) NULL;
448 XSCREENSAVER_MODULE ("Strange", strange)
450 #endif /* MODE_strange */