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...
32 /* TODO: Bring over tweaks from 3.x version.
33 * For a good idea of what's missing, diff strange.c.20081107-good2 against strange.c-3.29
34 * We forked from the 3.29 series at 20081107, so anything added since then may be missing.
39 # define DEFAULTS "*delay: 10000 \n" \
43 # define SMOOTH_COLORS
44 # define refresh_strange 0
45 # define reshape_strange 0
46 # define strange_handle_event 0
47 # include "xlockmore.h" /* from the xscreensaver distribution */
48 #else /* !STANDALONE */
49 # include "xlock.h" /* from the xlockmore distribution */
50 #endif /* !STANDALONE */
53 #define DEF_CURVE "10"
54 #define DEF_POINTS "5500"
59 static XrmOptionDescRec opts[] =
61 /* {"-curve", ".strange.curve", XrmoptionSepArg, 0}, */
62 {"-points", ".strange.points", XrmoptionSepArg, 0},
64 static argtype vars[] =
66 /* {&curve, "curve", "Curve", DEF_CURVE, t_Int},*/
67 {&points, "points", "Points", DEF_POINTS, t_Int},
69 static OptionStruct desc[] =
71 /* {"-curve", "set the curve factor of the attractors"},*/
72 {"-points", "change the number of points/iterations each frame"},
74 ENTRYPOINT ModeSpecOpt strange_opts =
75 {sizeof opts / sizeof opts[0], opts,
76 sizeof vars / sizeof vars[0], vars, desc};
79 ModStruct strange_description =
80 {"strange", "init_strange", "draw_strange", "release_strange",
81 "init_strange", "init_strange", (char *) NULL, &strange_opts,
82 1000, 1, 1, 1, 64, 1.0, "",
83 "Shows strange attractors", 0, NULL};
95 /* #define UNIT2 (3140*UNIT/1000) */
97 #define SKIP_FIRST 100
98 #define DBL_To_PRM(x) (PRM)( (DBL)(UNIT)*(x) )
101 #define DO_FOLD(a) (a)<0 ? -A->Fold[ (-(a))&(UNIT2-1) ] : A->Fold[ (a)&(UNIT2-1) ]
104 #define DO_FOLD(a) (a)<-UNIT2 ? -A->Fold[(-(a))%UNIT2] : (a)<0 ? -A->Fold[ -(a) ] :\
105 (a)>UNIT2 ? A->Fold[ (a)%UNIT2 ] : A->Fold[ (a) ]
106 #define DO_FOLD(a) DBL_To_PRM( sin( (DBL)(a)/UNIT ) )
107 #define DO_FOLD(a) (a)<0 ? DBL_To_PRM( exp( 16.0*(a)/UNIT2 ) )-1.0 : \
108 DBL_To_PRM( 1.0-exp( -16.0*(a)/UNIT2 ) )
111 /* useAccumulator performs two functions:
112 * If it is defined, then support for the accumulator will be compiled.
113 * It is also the condition for which the accumulator renderer will engage.
115 #define useAccumulator (Root->Max_Pt > 6000)
116 #define ACC_GAMMA 10.0
119 #define VARY_SPEED_TO_AVOID_BOREDOM
120 #define POINTS_HISTORY
121 #define MERGE_FRAMES 3
123 /******************************************************************/
127 typedef struct _ATTRACTOR {
128 DBL Prm1[MAX_PRM], Prm2[MAX_PRM];
129 PRM Prm[MAX_PRM], *Fold;
130 void (*Iterate) (struct _ATTRACTOR *, PRM, PRM, PRM *, PRM *);
131 XPoint *Buffer1, *Buffer2;
133 int Col, Count, Speed;
135 Pixmap dbuf; /* jwz */
137 #ifdef useAccumulator
142 static ATTRACTOR *Root = (ATTRACTOR *) NULL;
144 #ifdef useAccumulator
148 #ifdef POINTS_HISTORY
149 static int numOldPoints;
150 static int* oldPointsX;
151 static int* oldPointsY;
152 static int oldPointsIndex;
153 static int startedClearing;
156 static DBL Amp_Prm[MAX_PRM] =
158 1.0, 3.5, 3.5, 2.5, 4.7,
159 1.0, 3.5, 3.6, 2.5, 4.7,
160 1.0, 1.5, 2.2, 2.1, 3.5
162 static DBL Mid_Prm[MAX_PRM] =
164 0.0, 1.5, 0.0, .5, 1.5,
165 0.0, 1.5, 0.0, .5, 1.5,
166 0.0, 1.5, -1.0, -.5, 2.5,
170 Gauss_Rand(DBL c, DBL A, DBL S)
174 y = (DBL) LRAND() / MAXRAND;
175 y = A * (1.0 - exp(-y * y * S)) / (1.0 - exp(-S));
183 Random_Prm(DBL * Prm)
187 for (i = 0; i < MAX_PRM; ++i)
188 Prm[i] = Gauss_Rand(Mid_Prm[i], Amp_Prm[i], 4.0);
191 /***************************************************************/
193 /* 2 examples of non-linear map */
196 Iterate_X2(ATTRACTOR * A, PRM x, PRM y, PRM * xo, PRM * yo)
198 PRM xx, yy, xy, x2y, y2x, Tmp;
201 x2y = (xx * y) / UNIT;
203 y2x = (yy * x) / UNIT;
206 Tmp = A->Prm[1] * xx + A->Prm[2] * xy + A->Prm[3] * yy + A->Prm[4] * x2y;
207 Tmp = A->Prm[0] - y + (Tmp / UNIT);
209 Tmp = A->Prm[6] * xx + A->Prm[7] * xy + A->Prm[8] * yy + A->Prm[9] * y2x;
210 Tmp = A->Prm[5] + x + (Tmp / UNIT);
215 Iterate_X3(ATTRACTOR * A, PRM x, PRM y, PRM * xo, PRM * yo)
217 PRM xx, yy, xy, x2y, y2x, Tmp_x, Tmp_y, Tmp_z;
220 x2y = (xx * y) / UNIT;
222 y2x = (yy * x) / UNIT;
225 Tmp_x = A->Prm[1] * xx + A->Prm[2] * xy + A->Prm[3] * yy + A->Prm[4] * x2y;
226 Tmp_x = A->Prm[0] - y + (Tmp_x / UNIT);
227 Tmp_x = DO_FOLD(Tmp_x);
229 Tmp_y = A->Prm[6] * xx + A->Prm[7] * xy + A->Prm[8] * yy + A->Prm[9] * y2x;
230 Tmp_y = A->Prm[5] + x + (Tmp_y / UNIT);
232 Tmp_y = DO_FOLD(Tmp_y);
234 Tmp_z = A->Prm[11] * xx + A->Prm[12] * xy + A->Prm[13] * yy + A->Prm[14] * y2x;
235 Tmp_z = A->Prm[10] + x + (Tmp_z / UNIT);
236 Tmp_z = UNIT + Tmp_z * Tmp_z / UNIT;
238 *xo = (Tmp_x * UNIT) / Tmp_z;
239 *yo = (Tmp_y * UNIT) / Tmp_z;
242 static void (*Funcs[2]) (ATTRACTOR *, PRM, PRM, PRM *, PRM *) = {
243 Iterate_X2, Iterate_X3
246 /***************************************************************/
249 free_strange(Display *display, ATTRACTOR *A)
251 if (A->Buffer1 != NULL) {
252 (void) free((void *) A->Buffer1);
253 A->Buffer1 = (XPoint *) NULL;
255 if (A->Buffer2 != NULL) {
256 (void) free((void *) A->Buffer2);
257 A->Buffer2 = (XPoint *) NULL;
260 XFreePixmap(display, A->dbuf);
264 XFreeGC(display, A->dbuf_gc);
267 if (A->Fold != NULL) {
268 (void) free((void *) A->Fold);
269 A->Fold = (PRM *) NULL;
274 draw_strange(ModeInfo * mi)
280 Display *display = MI_DISPLAY(mi);
281 Window window = MI_WINDOW(mi);
284 void (*Iterate) (ATTRACTOR *, PRM, PRM, PRM *, PRM *);
285 PRM xmin, xmax, ymin, ymax;
290 A = &Root[MI_SCREEN(mi)];
295 Iterate = A->Iterate;
297 u = (DBL) (A->Count) / 40000.0;
298 for (j = MAX_PRM - 1; j >= 0; --j)
299 A->Prm[j] = DBL_To_PRM((1.0 - u) * A->Prm1[j] + u * A->Prm2[j]);
301 /* We collect the accumulation of the orbits in the 2d int array field. */
302 #ifndef POINTS_HISTORY
303 #ifdef useAccumulator
304 if (useAccumulator) {
305 for (i=0;i<A->Width;i++) {
306 for (j=0;j<A->Height;j++) {
315 x = y = DBL_To_PRM(.0);
316 for (n = SKIP_FIRST; n; --n) {
317 (*Iterate) (A, x, y, &xo, &yo);
318 x = xo + NRAND(8) - 4;
319 y = yo + NRAND(8) - 4;
328 Lx = (DBL) A->Width / UNIT / 2.2;
329 Ly = (DBL) A->Height / UNIT / 2.2;
330 for (n = A->Max_Pt; n; --n) {
331 (*Iterate) (A, x, y, &xo, &yo);
332 #ifdef useAccumulator
333 if (useAccumulator) {
335 mx = (short) ( A->Width*0.1 + A->Width*0.8 * (xo - xmin) / (xmax - xmin) );
336 my = (short) ( A->Width*0.1 + (A->Height - A->Width*0.2) * (yo - ymin) / (ymax - ymin) );
337 if (mx>=0 && my>=0 && mx<A->Width && my<A->Height) {
340 #ifdef POINTS_HISTORY
341 /* #define clearOldPoint(i) { if (startedClearing) { field[oldPoints[i].x][oldPoints[i].y]--; } }
342 #define saveUnplot(X,Y) { clearOldPoint(oldPointsIndex) oldPoints[oldPointsIndex].x = X; oldPoints[oldPointsIndex].y = Y; oldPointsIndex = (oldPointsIndex + 1) % numOldPoints; if (oldPointsIndex==0) { startedClearing=1; } }
344 if (startedClearing) {
345 int oldX = oldPointsX[oldPointsIndex];
346 int oldY = oldPointsY[oldPointsIndex];
347 if (oldX>=0 && oldY>=0 && oldX<A->Width && oldY<A->Height) {
348 A->accMap[oldX][oldY]--;
351 oldPointsX[oldPointsIndex] = mx;
352 oldPointsY[oldPointsIndex] = my;
353 oldPointsIndex = (oldPointsIndex + 1) % numOldPoints;
354 if (oldPointsIndex==0) { startedClearing=1; }
358 Buf->x = (int) (Lx * (x + DBL_To_PRM(1.1)));
359 Buf->y = (int) (Ly * (DBL_To_PRM(1.1) - y));
362 #ifdef useAccumulator
365 /* (void) fprintf( stderr, "X,Y: %d %d ", Buf->x, Buf->y ); */
374 x = xo + NRAND(8) - 4;
375 y = yo + NRAND(8) - 4;
378 MI_IS_DRAWN(mi) = True;
380 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
382 #ifdef useAccumulator
383 if (useAccumulator) {
386 #ifdef VARY_SPEED_TO_AVOID_BOREDOM
389 colorScale = (A->Width*A->Height/640.0/480.0*800000.0/(float)A->Max_Pt*(float)NUM_COLS/256);
390 if (A->dbuf != None) {
391 XSetForeground(display, A->dbuf_gc, 0);
392 XFillRectangle(display, A->dbuf, A->dbuf_gc, 0, 0, A->Width, A->Height);
394 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
395 XFillRectangle(display, window, gc, 0, 0, A->Width, A->Height);
397 for (i=0;i<A->Width;i++) {
398 for (j=0;j<A->Height;j++) {
399 if (A->accMap[i][j]>0) {
400 col = (float)A->accMap[i][j] * colorScale;
401 if (col>NUM_COLS-1) {
404 #ifdef VARY_SPEED_TO_AVOID_BOREDOM
406 if (col<NUM_COLS-1) /* we don't count maxxed out pixels */
410 if (MI_NPIXELS(mi) < 2)
411 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
413 /*XSetForeground(display, gc, MI_PIXEL(mi, A->Col % MI_NPIXELS(mi)));*/
414 XSetForeground(display, gc, cols[col].pixel);
415 if (A->dbuf != None) {
416 XSetForeground(display, A->dbuf_gc, cols[col].pixel);
417 XDrawPoint(display, A->dbuf, A->dbuf_gc, i, j);
419 XSetForeground(display, gc, cols[col].pixel);
420 XDrawPoint(display, window, gc, i, j);
425 if (A->dbuf != None) {
426 XCopyArea(display, A->dbuf, window, gc, 0, 0, A->Width, A->Height, 0, 0);
428 #ifdef VARY_SPEED_TO_AVOID_BOREDOM
429 /* Increaase the rate of change of the parameters if the attractor has become visually boring. */
430 if ((xmax - xmin < DBL_To_PRM(.2)) && (ymax - ymin < DBL_To_PRM(.2))) {
432 } else if (pixelCount>0 && pixelCount<A->Width*A->Height/1000) {
433 A->Speed *= 1.25; /* A->Count = 1000; */
435 A->Speed = 4; /* reset to normal/default */
439 A->Count += A->Speed;
440 if (A->Count >= 1000) {
441 for (i = MAX_PRM - 1; i >= 0; --i)
442 A->Prm1[i] = A->Prm2[i];
450 if (A->dbuf != None) { /* jwz */
451 XSetForeground(display, A->dbuf_gc, 0);
452 /* XDrawPoints(display, A->dbuf, A->dbuf_gc, A->Buffer1,
453 Cur_Pt,CoordModeOrigin); */
454 XFillRectangle(display, A->dbuf, A->dbuf_gc, 0, 0, A->Width, A->Height);
456 XDrawPoints(display, window, gc, A->Buffer1, Cur_Pt, CoordModeOrigin);
458 if (MI_NPIXELS(mi) < 2)
459 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
461 XSetForeground(display, gc, MI_PIXEL(mi, A->Col % MI_NPIXELS(mi)));
463 if (A->dbuf != None) {
464 XSetForeground(display, A->dbuf_gc, 1);
465 XDrawPoints(display, A->dbuf, A->dbuf_gc, A->Buffer2, A->Cur_Pt,
467 XCopyPlane(display, A->dbuf, window, gc, 0, 0, A->Width, A->Height, 0, 0, 1);
469 XDrawPoints(display, window, gc, A->Buffer2, A->Cur_Pt, CoordModeOrigin);
471 #ifdef useAccumulator
476 A->Buffer1 = A->Buffer2;
479 if ((xmax - xmin < DBL_To_PRM(.2)) && (ymax - ymin < DBL_To_PRM(.2)))
480 A->Count += 4 * A->Speed;
482 A->Count += A->Speed;
483 if (A->Count >= 1000) {
484 for (i = MAX_PRM - 1; i >= 0; --i)
485 A->Prm1[i] = A->Prm2[i];
493 /***************************************************************/
496 init_strange(ModeInfo * mi)
498 Display *display = MI_DISPLAY(mi);
499 Window window = MI_WINDOW(mi);
503 ATTRACTOR *Attractor;
505 #ifdef POINTS_HISTORY
511 if ((Root = (ATTRACTOR *) calloc(MI_NUM_SCREENS(mi),
512 sizeof (ATTRACTOR))) == NULL)
515 Attractor = &Root[MI_SCREEN(mi)];
517 if (Attractor->Fold == NULL) {
520 if ((Attractor->Fold = (PRM *) calloc(UNIT2 + 1,
521 sizeof (PRM))) == NULL) {
522 free_strange(display, Attractor);
525 for (i = 0; i <= UNIT2; ++i) {
528 /* x = ( DBL )(i)/UNIT2; */
529 /* x = sin( M_PI/2.0*x ); */
532 /* x = x*(1.0-x)*4.0; */
533 x = (DBL) (i) / UNIT;
535 Attractor->Fold[i] = DBL_To_PRM(x);
539 Attractor->Max_Pt = points;
541 if (Attractor->Buffer1 == NULL)
542 if ((Attractor->Buffer1 = (XPoint *) calloc(Attractor->Max_Pt,
543 sizeof (XPoint))) == NULL) {
544 free_strange(display, Attractor);
547 if (Attractor->Buffer2 == NULL)
548 if ((Attractor->Buffer2 = (XPoint *) calloc(Attractor->Max_Pt,
549 sizeof (XPoint))) == NULL) {
550 free_strange(display, Attractor);
554 Attractor->Width = MI_WIDTH(mi);
555 Attractor->Height = MI_HEIGHT(mi);
556 Attractor->Cur_Pt = 0;
557 Attractor->Count = 0;
558 Attractor->Col = NRAND(MI_NPIXELS(mi));
559 Attractor->Speed = 4;
561 Attractor->Iterate = Funcs[NRAND(2)];
562 Random_Prm(Attractor->Prm1);
563 Random_Prm(Attractor->Prm2);
565 if (Attractor->dbuf != None)
566 XFreePixmap(display, Attractor->dbuf);
567 #ifdef useAccumulator
568 #define colorDepth ( useAccumulator ? MI_DEPTH(mi) : 1 )
572 Attractor->dbuf = XCreatePixmap(display, window,
573 Attractor->Width, Attractor->Height, colorDepth);
574 /* Allocation checked */
575 if (Attractor->dbuf != None) {
581 gcv.graphics_exposures = False;
582 #endif /* HAVE_COCOA */
583 gcv.function = GXcopy;
585 if (Attractor->dbuf_gc != None)
586 XFreeGC(display, Attractor->dbuf_gc);
588 if ((Attractor->dbuf_gc = XCreateGC(display, Attractor->dbuf,
590 GCGraphicsExposures |
591 #endif /* HAVE_COCOA */
592 GCFunction | GCForeground | GCBackground,
594 XFreePixmap(display, Attractor->dbuf);
595 Attractor->dbuf = None;
597 XFillRectangle(display, Attractor->dbuf, Attractor->dbuf_gc,
598 0, 0, Attractor->Width, Attractor->Height);
599 XSetBackground(display, gc, MI_BLACK_PIXEL(mi));
600 XSetFunction(display, gc, GXcopy);
606 #ifdef useAccumulator
608 if (useAccumulator) {
609 XWindowAttributes xgwa;
611 XGetWindowAttributes (display, window, &xgwa);
612 /* cmap = xgwa.colormap; */
613 /* cmap = XCreateColormap(display, window, MI_VISUAL(mi), AllocAll); */
614 Attractor->accMap = (int**)calloc(Attractor->Width,sizeof(int*));
615 for (i=0;i<Attractor->Width;i++) {
616 Attractor->accMap[i] = (int*)calloc(Attractor->Height,sizeof(int));
617 for (j=0;j<Attractor->Height;j++) {
618 Attractor->accMap[i][j] = 0;
621 #ifdef POINTS_HISTORY
622 numOldPoints = A->Max_Pt * MERGE_FRAMES;
623 oldPointsX = (int*)calloc(numOldPoints,sizeof(int));
624 oldPointsY = (int*)calloc(numOldPoints,sizeof(int));
626 cols = (XColor*)calloc(NUM_COLS,sizeof(XColor));
627 for (i=0;i<NUM_COLS;i++) {
631 li = MINBLUE + (255.0-MINBLUE) * log(1.0 + ACC_GAMMA*(float)i/NUM_COLS) / log(1.0 + ACC_GAMMA);
635 cols[i].blue = 65536*li/FULLBLUE;
637 cols[i].red = 65536*(li-FULLBLUE)/(256-FULLBLUE);
638 cols[i].green = 65536*(li-FULLBLUE)/(256-FULLBLUE);
639 cols[i].blue = 65535;
641 XAllocColor (display, xgwa.colormap, &cols[i]);
643 if (!XAllocColor(MI_DISPLAY(mi), cmap, &cols[i])) {
644 if (!XAllocColor(display, cmap, &cols[i])) {
645 cols[i].pixel = WhitePixel (display, DefaultScreen (display));
646 cols[i].red = cols[i].green = cols[i].blue = 0xFFFF;
651 XSetWindowColormap(display, window, cmap);
652 (void) XSetWMColormapWindows(display, window, &window, 1);
653 XInstallColormap(display, cmap);
654 XStoreColors(display, cmap, cols, 256);
661 /* Do not want any exposure events from XCopyPlane */
662 XSetGraphicsExposures(display, MI_GC(mi), False);
665 /***************************************************************/
668 release_strange(ModeInfo * mi)
673 #ifdef useAccumulator
675 (void) free((void *) cols);
676 for (i=0;i<Root->Width;i++) {
677 (void) free((void *) Root->accMap[i]);
679 (void) free((void *) Root->accMap);
681 #ifdef POINTS_HISTORY
685 for (screen = 0; screen < MI_NUM_SCREENS(mi); ++screen)
686 free_strange(MI_DISPLAY(mi), &Root[screen]);
687 (void) free((void *) Root);
688 Root = (ATTRACTOR *) NULL;
692 XSCREENSAVER_MODULE ("Strange", strange)
694 #endif /* MODE_strange */