1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
/* -*- Mode: C; tab-width: 4 -*- */
/* strange --- strange attractors */
#if !defined( lint ) && !defined( SABER )
static const char sccsid[] = "@(#)strange.c 5.00 2000/11/01 xlockmore";
#endif
/*-
* Copyright (c) 1997 by Massimino Pascal <Pascal.Massimon@ens.fr>
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation.
*
* This file is provided AS IS with no warranties of any kind. The author
* shall have no liability with respect to the infringement of copyrights,
* trade secrets or any patents by this file or any part thereof. In no
* event will the author be liable for any lost revenue or profits or
* other special, indirect and consequential damages.
*
* Revision History:
* 22-Dec-2004: TDA: Replace Gauss_Rand with a real Gaussian.
* 01-Nov-2000: Allocation checks
* 10-May-1997: jwz@jwz.org: turned into a standalone program.
* Made it render into an offscreen bitmap and then copy
* that onto the screen, to reduce flicker.
*
* strange attractors are not so hard to find...
*/
#ifdef STANDALONE
#define MODE_strange
#define PROGCLASS "Strange"
#define HACK_INIT init_strange
#define HACK_DRAW draw_strange
#define strange_opts xlockmore_opts
#define DEFAULTS "*delay: 1000 \n" \
"*ncolors: 100 \n"
#define SMOOTH_COLORS
#include "xlockmore.h" /* from the xscreensaver distribution */
#else /* !STANDALONE */
#include "xlock.h" /* from the xlockmore distribution */
#endif /* !STANDALONE */
#ifdef MODE_strange
ModeSpecOpt strange_opts =
{0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
#ifdef USE_MODULES
ModStruct strange_description =
{"strange", "init_strange", "draw_strange", "release_strange",
"init_strange", "init_strange", (char *) NULL, &strange_opts,
1000, 1, 1, 1, 64, 1.0, "",
"Shows strange attractors", 0, NULL};
#endif
typedef float DBL;
typedef int PRM;
#define UNIT (1<<12)
#define UNIT2 (1<<14)
/* #define UNIT2 (3140*UNIT/1000) */
#define SKIP_FIRST 100
#define MAX_POINTS 5500
#define DBL_To_PRM(x) (PRM)( (DBL)(UNIT)*(x) )
#define DO_FOLD(a) (a)<0 ? -A->Fold[ (-(a))&(UNIT2-1) ] : A->Fold[ (a)&(UNIT2-1) ]
#if 0
#define DO_FOLD(a) (a)<-UNIT2 ? -A->Fold[(-(a))%UNIT2] : (a)<0 ? -A->Fold[ -(a) ] :\
(a)>UNIT2 ? A->Fold[ (a)%UNIT2 ] : A->Fold[ (a) ]
#define DO_FOLD(a) DBL_To_PRM( sin( (DBL)(a)/UNIT ) )
#define DO_FOLD(a) (a)<0 ? DBL_To_PRM( exp( 16.0*(a)/UNIT2 ) )-1.0 : \
DBL_To_PRM( 1.0-exp( -16.0*(a)/UNIT2 ) )
#endif
/******************************************************************/
#define MAX_PRM 3*5
typedef struct _ATTRACTOR {
DBL Prm1[MAX_PRM], Prm2[MAX_PRM];
PRM Prm[MAX_PRM], *Fold;
void (*Iterate) (struct _ATTRACTOR *, PRM, PRM, PRM *, PRM *);
XPoint *Buffer1, *Buffer2;
int Cur_Pt, Max_Pt;
int Col, Count, Speed;
int Width, Height;
Pixmap dbuf; /* jwz */
GC dbuf_gc;
} ATTRACTOR;
static ATTRACTOR *Root = (ATTRACTOR *) NULL;
static DBL Amp_Prm[MAX_PRM] =
{
1.0, 3.5, 3.5, 2.5, 4.7,
1.0, 3.5, 3.6, 2.5, 4.7,
1.0, 1.5, 2.2, 2.1, 3.5
};
static DBL Mid_Prm[MAX_PRM] =
{
0.0, 1.5, 0.0, .5, 1.5,
0.0, 1.5, 0.0, .5, 1.5,
0.0, 1.5, -1.0, -.5, 2.5,
};
#if 0
static DBL
Gauss_Rand(DBL c, DBL A, DBL S)
{
DBL y;
y = (DBL) LRAND() / MAXRAND;
y = A * (1.0 - exp(-y * y * S)) / (1.0 - exp(-S));
if (NRAND(2))
return (c + y);
else
return (c - y);
}
#else
/* I don't know that it makes a difference, but this one actually *is*
a Gaussian. [TDA] */
/* Generate Gaussian random number: mean c, "amplitude" A (actually
A is 3*standard deviation). 'S' is unused. */
/* Note this generates a pair of gaussian variables, so it saves one
to give out next time it's called */
static double
Gauss_Rand(DBL c, DBL A, DBL S)
{
static double d;
static Bool ready = 0;
if(ready) {
ready = 0;
return c + A/3 * d;
} else {
double x, y, w;
do {
x = 2.0 * (double)LRAND() / MAXRAND - 1.0;
y = 2.0 * (double)LRAND() / MAXRAND - 1.0;
w = x*x + y*y;
} while(w >= 1.0);
w = sqrt((-2 * log(w))/w);
ready = 1;
d = x * w;
return c + A/3 * y * w;
}
}
#endif
static void
Random_Prm(DBL * Prm)
{
int i;
for (i = 0; i < MAX_PRM; ++i)
Prm[i] = Gauss_Rand(Mid_Prm[i], Amp_Prm[i], 4.0);
}
/***************************************************************/
/* 2 examples of non-linear map */
static void
Iterate_X2(ATTRACTOR * A, PRM x, PRM y, PRM * xo, PRM * yo)
{
PRM xx, yy, xy, x2y, y2x, Tmp;
xx = (x * x) / UNIT;
x2y = (xx * y) / UNIT;
yy = (y * y) / UNIT;
y2x = (yy * x) / UNIT;
xy = (x * y) / UNIT;
Tmp = A->Prm[1] * xx + A->Prm[2] * xy + A->Prm[3] * yy + A->Prm[4] * x2y;
Tmp = A->Prm[0] - y + (Tmp / UNIT);
*xo = DO_FOLD(Tmp);
Tmp = A->Prm[6] * xx + A->Prm[7] * xy + A->Prm[8] * yy + A->Prm[9] * y2x;
Tmp = A->Prm[5] + x + (Tmp / UNIT);
*yo = DO_FOLD(Tmp);
}
static void
Iterate_X3(ATTRACTOR * A, PRM x, PRM y, PRM * xo, PRM * yo)
{
PRM xx, yy, xy, x2y, y2x, Tmp_x, Tmp_y, Tmp_z;
xx = (x * x) / UNIT;
x2y = (xx * y) / UNIT;
yy = (y * y) / UNIT;
y2x = (yy * x) / UNIT;
xy = (x * y) / UNIT;
Tmp_x = A->Prm[1] * xx + A->Prm[2] * xy + A->Prm[3] * yy + A->Prm[4] * x2y;
Tmp_x = A->Prm[0] - y + (Tmp_x / UNIT);
Tmp_x = DO_FOLD(Tmp_x);
Tmp_y = A->Prm[6] * xx + A->Prm[7] * xy + A->Prm[8] * yy + A->Prm[9] * y2x;
Tmp_y = A->Prm[5] + x + (Tmp_y / UNIT);
Tmp_y = DO_FOLD(Tmp_y);
Tmp_z = A->Prm[11] * xx + A->Prm[12] * xy + A->Prm[13] * yy + A->Prm[14] * y2x;
Tmp_z = A->Prm[10] + x + (Tmp_z / UNIT);
Tmp_z = UNIT + Tmp_z * Tmp_z / UNIT;
*xo = (Tmp_x * UNIT) / Tmp_z;
*yo = (Tmp_y * UNIT) / Tmp_z;
}
static void (*Funcs[2]) (ATTRACTOR *, PRM, PRM, PRM *, PRM *) = {
Iterate_X2, Iterate_X3
};
/***************************************************************/
static void
free_strange(Display *display, ATTRACTOR *A)
{
if (A->Buffer1 != NULL) {
free(A->Buffer1);
A->Buffer1 = (XPoint *) NULL;
}
if (A->Buffer2 != NULL) {
free(A->Buffer2);
A->Buffer2 = (XPoint *) NULL;
}
if (A->dbuf) {
XFreePixmap(display, A->dbuf);
A->dbuf = None;
}
if (A->dbuf_gc) {
XFreeGC(display, A->dbuf_gc);
A->dbuf_gc = None;
}
if (A->Fold != NULL) {
free(A->Fold);
A->Fold = (PRM *) NULL;
}
}
void
draw_strange(ModeInfo * mi)
{
int i, j, n, Cur_Pt;
PRM x, y, xo, yo;
DBL u;
XPoint *Buf;
Display *display = MI_DISPLAY(mi);
Window window = MI_WINDOW(mi);
GC gc = MI_GC(mi);
DBL Lx, Ly;
void (*Iterate) (ATTRACTOR *, PRM, PRM, PRM *, PRM *);
PRM xmin, xmax, ymin, ymax;
ATTRACTOR *A;
if (Root == NULL)
return;
A = &Root[MI_SCREEN(mi)];
if (A->Fold == NULL)
return;
Cur_Pt = A->Cur_Pt;
Iterate = A->Iterate;
u = (DBL) (A->Count) / 1000.0;
for (j = MAX_PRM - 1; j >= 0; --j)
A->Prm[j] = DBL_To_PRM((1.0 - u) * A->Prm1[j] + u * A->Prm2[j]);
x = y = DBL_To_PRM(.0);
for (n = SKIP_FIRST; n; --n) {
(*Iterate) (A, x, y, &xo, &yo);
x = xo + NRAND(8) - 4;
y = yo + NRAND(8) - 4;
}
xmax = 0;
xmin = UNIT * 4;
ymax = 0;
ymin = UNIT * 4;
A->Cur_Pt = 0;
Buf = A->Buffer2;
Lx = (DBL) A->Width / UNIT / 2.2;
Ly = (DBL) A->Height / UNIT / 2.2;
for (n = A->Max_Pt; n; --n) {
(*Iterate) (A, x, y, &xo, &yo);
Buf->x = (int) (Lx * (x + DBL_To_PRM(1.1)));
Buf->y = (int) (Ly * (DBL_To_PRM(1.1) - y));
/* (void) fprintf( stderr, "X,Y: %d %d ", Buf->x, Buf->y ); */
Buf++;
A->Cur_Pt++;
if (xo > xmax)
xmax = xo;
else if (xo < xmin)
xmin = xo;
if (yo > ymax)
ymax = yo;
else if (yo < ymin)
ymin = yo;
x = xo + NRAND(8) - 4;
y = yo + NRAND(8) - 4;
}
MI_IS_DRAWN(mi) = True;
XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
if (A->dbuf != None) { /* jwz */
XSetForeground(display, A->dbuf_gc, 0);
/* XDrawPoints(display, A->dbuf, A->dbuf_gc, A->Buffer1,
Cur_Pt,CoordModeOrigin); */
XFillRectangle(display, A->dbuf, A->dbuf_gc, 0, 0, A->Width, A->Height);
} else
XDrawPoints(display, window, gc, A->Buffer1, Cur_Pt, CoordModeOrigin);
if (MI_NPIXELS(mi) < 2)
XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
else
XSetForeground(display, gc, MI_PIXEL(mi, A->Col % MI_NPIXELS(mi)));
if (A->dbuf != None) {
XSetForeground(display, A->dbuf_gc, 1);
XDrawPoints(display, A->dbuf, A->dbuf_gc, A->Buffer2, A->Cur_Pt,
CoordModeOrigin);
XCopyPlane(display, A->dbuf, window, gc, 0, 0, A->Width, A->Height, 0, 0, 1);
} else
XDrawPoints(display, window, gc, A->Buffer2, A->Cur_Pt, CoordModeOrigin);
Buf = A->Buffer1;
A->Buffer1 = A->Buffer2;
A->Buffer2 = Buf;
if ((xmax - xmin < DBL_To_PRM(.2)) && (ymax - ymin < DBL_To_PRM(.2)))
A->Count += 4 * A->Speed;
else
A->Count += A->Speed;
if (A->Count >= 1000) {
for (i = MAX_PRM - 1; i >= 0; --i)
A->Prm1[i] = A->Prm2[i];
Random_Prm(A->Prm2);
A->Count = 0;
}
A->Col++;
}
/***************************************************************/
void
init_strange(ModeInfo * mi)
{
Display *display = MI_DISPLAY(mi);
Window window = MI_WINDOW(mi);
GC gc = MI_GC(mi);
ATTRACTOR *Attractor;
if (Root == NULL) {
if ((Root = (ATTRACTOR *) calloc(MI_NUM_SCREENS(mi),
sizeof (ATTRACTOR))) == NULL)
return;
}
Attractor = &Root[MI_SCREEN(mi)];
if (Attractor->Fold == NULL) {
int i;
if ((Attractor->Fold = (PRM *) calloc(UNIT2 + 1,
sizeof (PRM))) == NULL) {
free_strange(display, Attractor);
return;
}
for (i = 0; i <= UNIT2; ++i) {
DBL x;
/* x = ( DBL )(i)/UNIT2; */
/* x = sin( M_PI/2.0*x ); */
/* x = sqrt( x ); */
/* x = x*x; */
/* x = x*(1.0-x)*4.0; */
x = (DBL) (i) / UNIT;
x = sin(x);
Attractor->Fold[i] = DBL_To_PRM(x);
}
}
if (Attractor->Buffer1 == NULL)
if ((Attractor->Buffer1 = (XPoint *) calloc(MAX_POINTS,
sizeof (XPoint))) == NULL) {
free_strange(display, Attractor);
return;
}
if (Attractor->Buffer2 == NULL)
if ((Attractor->Buffer2 = (XPoint *) calloc(MAX_POINTS,
sizeof (XPoint))) == NULL) {
free_strange(display, Attractor);
return;
}
Attractor->Max_Pt = MAX_POINTS;
Attractor->Width = MI_WIDTH(mi);
Attractor->Height = MI_HEIGHT(mi);
Attractor->Cur_Pt = 0;
Attractor->Count = 0;
Attractor->Col = NRAND(MI_NPIXELS(mi));
Attractor->Speed = 4;
Attractor->Iterate = Funcs[NRAND(2)];
Random_Prm(Attractor->Prm1);
Random_Prm(Attractor->Prm2);
#ifndef NO_DBUF
if (Attractor->dbuf != None)
XFreePixmap(display, Attractor->dbuf);
Attractor->dbuf = XCreatePixmap(display, window,
Attractor->Width, Attractor->Height, 1);
/* Allocation checked */
if (Attractor->dbuf != None) {
XGCValues gcv;
gcv.foreground = 0;
gcv.background = 0;
gcv.graphics_exposures = False;
gcv.function = GXcopy;
if (Attractor->dbuf_gc != None)
XFreeGC(display, Attractor->dbuf_gc);
if ((Attractor->dbuf_gc = XCreateGC(display, Attractor->dbuf,
GCForeground | GCBackground | GCGraphicsExposures | GCFunction,
&gcv)) == None) {
XFreePixmap(display, Attractor->dbuf);
Attractor->dbuf = None;
} else {
XFillRectangle(display, Attractor->dbuf, Attractor->dbuf_gc,
0, 0, Attractor->Width, Attractor->Height);
XSetBackground(display, gc, MI_BLACK_PIXEL(mi));
XSetFunction(display, gc, GXcopy);
}
}
#endif
MI_CLEARWINDOW(mi);
/* Do not want any exposure events from XCopyPlane */
XSetGraphicsExposures(display, MI_GC(mi), False);
}
/***************************************************************/
void
release_strange(ModeInfo * mi)
{
if (Root != NULL) {
int screen;
for (screen = 0; screen < MI_NUM_SCREENS(mi); ++screen)
free_strange(MI_DISPLAY(mi), &Root[screen]);
free(Root);
Root = (ATTRACTOR *) NULL;
}
}
#endif /* MODE_strange */
|