1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* drift --- drifting recursive fractal cosmic flames */
5 static const char sccsid[] = "@(#)drift.c 5.00 2000/11/01 xlockmore";
9 * Copyright (c) 1991 by Patrick J. Naughton.
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: Jamie Zawinski <jwz@jwz.org> compatible with xscreensaver
26 * 01-Jan-1997: Moved new flame to drift. Compile time options now run time.
27 * 01-Jun-1995: Updated by Scott Draves.
28 * 27-Jun-1991: vary number of functions used.
29 * 24-Jun-1991: fixed portability problem with integer mod (%).
30 * 06-Jun-1991: Written, received from Scott Draves <spot@cs.cmu.edu>
35 # define DEFAULTS "*delay: 10000 \n" \
38 "*fpsSolid: true \n" \
39 "*ignoreRotation: True \n" \
41 # define SMOOTH_COLORS
42 # define release_drift 0
43 # define reshape_drift 0
44 # define drift_handle_event 0
45 # include "xlockmore.h" /* in xscreensaver distribution */
46 #else /* STANDALONE */
47 # define ENTRYPOINT /**/
48 # include "xlock.h" /* in xlockmore distribution */
49 #endif /* STANDALONE */
53 #define DEF_GROW "False" /* Grow fractals instead of animating one at a time,
54 would then be like flame */
55 #define DEF_LISS "False" /* if this is defined then instead of a point
56 bouncing around in a high dimensional sphere, we
57 use lissojous figures. Only makes sense if
63 static XrmOptionDescRec opts[] =
65 {"-grow", ".drift.grow", XrmoptionNoArg, "on"},
66 {"+grow", ".drift.grow", XrmoptionNoArg, "off"},
67 {"-liss", ".drift.trail", XrmoptionNoArg, "on"},
68 {"+liss", ".drift.trail", XrmoptionNoArg, "off"}
70 static argtype vars[] =
72 {&grow, "grow", "Grow", DEF_GROW, t_Bool},
73 {&liss, "trail", "Trail", DEF_LISS, t_Bool}
75 static OptionStruct desc[] =
77 {"-/+grow", "turn on/off growing fractals, else they are animated"},
78 {"-/+liss", "turn on/off using lissojous figures to get points"}
81 ENTRYPOINT ModeSpecOpt drift_opts =
82 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
85 ModStruct drift_description =
86 {"drift", "init_drift", "draw_drift", (char *) NULL,
87 "refresh_drift", "init_drift", "free_drift", &drift_opts,
88 10000, 30, 1, 1, 64, 1.0, "",
89 "Shows cosmic drifting flame fractals", 0, NULL};
93 #define MAXBATCH1 200 /* mono */
94 #define MAXBATCH2 20 /* color */
95 #define FUSE 10 /* discard this many initial iterations */
100 /* shape of current flame */
102 double f[2][3][MAXLEV]; /* a bunch of non-homogeneous xforms */
103 int variation[10]; /* for each xform */
106 double df[2][3][MAXLEV];
108 /* high-level control */
109 int mode; /* 0->slow/single 1->fast/many */
110 int nfractals; /* draw this many fractals */
112 int fractal_len; /* pts/fractal */
114 int rainbow; /* more than one color per fractal
115 1-> computed by adding dimension to fractal */
117 int width, height; /* of window */
120 /* draw info about current flame */
121 int fuse; /* iterate this many before drawing */
122 int total_points; /* draw this many pts before fractal ends */
123 int npoints; /* how many we've computed but not drawn */
124 XPoint pts[MAXBATCH1]; /* here they are */
125 unsigned long pixcol;
126 /* when drawing in color, we have a buffer per color */
135 long saved_random_bits;
141 static driftstruct *drifts = (driftstruct *) NULL;
144 halfrandom(driftstruct * dp, int mv)
153 dp->lasthalf = (short) (r >> 16);
160 frandom(driftstruct * dp, int n)
165 dp->saved_random_bits = LRAND();
170 result = (int) (dp->saved_random_bits & 1);
171 dp->saved_random_bits >>= 1;
176 result = (int) (dp->saved_random_bits & 3);
177 dp->saved_random_bits >>= 2;
180 return frandom(dp, 3);
184 result = (int) (dp->saved_random_bits & 3);
185 dp->saved_random_bits >>= 2;
190 result = (int) (dp->saved_random_bits & 7);
191 dp->saved_random_bits >>= 3;
194 return frandom(dp, 5);
197 (void) fprintf(stderr, "bad arg to frandom\n");
202 #define DISTRIB_A (halfrandom(dp, 7000) + 9000)
203 #define DISTRIB_B ((frandom(dp, 3) + 1) * (frandom(dp, 3) + 1) * 120000)
204 #define LEN(x) (sizeof(x)/sizeof((x)[0]))
207 initmode(ModeInfo * mi, int mode)
209 driftstruct *dp = &drifts[MI_SCREEN(mi)];
211 #define VARIATION_LEN 14
215 dp->major_variation = halfrandom(dp, VARIATION_LEN);
216 /* 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6, 6 */
217 dp->major_variation = ((dp->major_variation >= VARIATION_LEN >> 1) &&
218 (dp->major_variation < VARIATION_LEN - 1)) ?
219 (dp->major_variation + 1) >> 1 : dp->major_variation >> 1;
224 if (!dp->color || halfrandom(dp, 8)) {
225 dp->nfractals = halfrandom(dp, 30) + 5;
226 dp->fractal_len = DISTRIB_A;
228 dp->nfractals = halfrandom(dp, 5) + 5;
229 dp->fractal_len = DISTRIB_B;
232 dp->rainbow = dp->color;
234 dp->fractal_len = DISTRIB_B;
238 dp->rainbow = dp->color;
239 dp->fractal_len = 2000000;
241 dp->fractal_len = (dp->fractal_len * MI_COUNT(mi)) / 20;
247 pick_df_coefs(ModeInfo * mi)
249 driftstruct *dp = &drifts[MI_SCREEN(mi)];
253 for (i = 0; i < dp->nxforms; i++) {
256 for (j = 0; j < 2; j++)
257 for (k = 0; k < 3; k++) {
258 dp->df[j][k][i] = ((double) halfrandom(dp, 1000) / 500.0 - 1.0);
259 r += dp->df[j][k][i] * dp->df[j][k][i];
261 r = (3 + halfrandom(dp, 5)) * 0.01 / sqrt(r);
262 for (j = 0; j < 2; j++)
263 for (k = 0; k < 3; k++)
264 dp->df[j][k][i] *= r;
269 free_drift(ModeInfo * mi)
271 driftstruct *dp = &drifts[MI_SCREEN(mi)];
272 if (dp->ncpoints != NULL) {
273 (void) free((void *) dp->ncpoints);
274 dp->ncpoints = (int *) NULL;
276 if (dp->cpts != NULL) {
277 (void) free((void *) dp->cpts);
278 dp->cpts = (XPoint *) NULL;
283 initfractal(ModeInfo * mi)
285 driftstruct *dp = &drifts[MI_SCREEN(mi)];
291 dp->total_points = 0;
294 if ((dp->ncpoints = (int *) malloc(sizeof (int) * MI_NCOLORS(mi))) ==
301 if ((dp->cpts = (XPoint *) malloc(MAXBATCH2 * sizeof (XPoint) *
302 MI_NCOLORS(mi))) == NULL) {
309 for (i = 0; i < MI_NPIXELS(mi); i++)
313 dp->nxforms = halfrandom(dp, XFORM_LEN);
314 /* 2, 2, 2, 3, 3, 3, 4, 4, 5 */
315 dp->nxforms = (dp->nxforms >= XFORM_LEN - 1) + dp->nxforms / 3 + 2;
317 dp->c = dp->x = dp->y = 0.0;
318 if (dp->liss && !halfrandom(dp, 10)) {
323 for (i = 0; i < dp->nxforms; i++) {
324 if (NMAJORVARS == dp->major_variation)
325 dp->variation[i] = halfrandom(dp, NMAJORVARS);
327 dp->variation[i] = dp->major_variation;
328 for (j = 0; j < 2; j++)
329 for (k = 0; k < 3; k++) {
331 dp->f[j][k][i] = sin(dp->liss_time * dp->df[j][k][i]);
333 dp->f[j][k][i] = ((double) halfrandom(dp, 1000) / 500.0 - 1.0);
337 dp->pixcol = MI_PIXEL(mi, halfrandom(dp, MI_NPIXELS(mi)));
339 dp->pixcol = MI_WHITE_PIXEL(mi);
345 init_drift(ModeInfo * mi)
349 MI_INIT (mi, drifts);
350 dp = &drifts[MI_SCREEN(mi)];
352 dp->width = MI_WIDTH(mi);
353 dp->height = MI_HEIGHT(mi);
354 dp->color = MI_NPIXELS(mi) > 2;
356 if (MI_IS_FULLRANDOM(mi)) {
361 dp->liss = (Bool) (LRAND() & 1);
375 iter(driftstruct * dp)
377 int i = frandom(dp, dp->nxforms);
382 nc = (dp->c + 1.0) / 2.0;
386 nx = dp->f[0][0][i] * dp->x + dp->f[0][1][i] * dp->y + dp->f[0][2][i];
387 ny = dp->f[1][0][i] * dp->x + dp->f[1][1][i] * dp->y + dp->f[1][2][i];
390 switch (dp->variation[i]) {
399 double r2 = nx * nx + ny * ny + 1e-6;
416 double r = (nx * nx + ny * ny); /* times k here is fun */
421 if (nx > 1e4 || nx < -1e4 || ny > 1e4 || ny < -1e4)
424 ny = c2 * t + c1 * ny;
425 nx = c1 * nx - c2 * ny;
433 /* Avoid atan2: DOMAIN error message */
434 if (nx == 0.0 && ny == 0.0)
437 r = atan2(nx, ny); /* times k here is fun */
442 nx = c1 * nx - c2 * ny;
443 ny = c2 * t + c1 * ny;
451 /* Avoid atan2: DOMAIN error message */
452 if (nx == 0.0 && ny == 0.0)
455 t = atan2(nx, ny) / M_PI;
457 if (nx > 1e4 || nx < -1e4 || ny > 1e4 || ny < -1e4)
460 ny = sqrt(nx * nx + ny * ny) - 1.0;
467 /* here are some others */
482 double u = nx, v = ny;
484 double emv = exp(-v);
486 nx = (ev + emv) * sin(u) / 2.0;
487 ny = (ev - emv) * cos(u) / 2.0;
504 double r = 0.5 + sqrt(nx * nx + ny * ny + 1e-6);
510 nx = atan(nx) / M_PI_2
511 ny = atan(ny) / M_PI_2
515 /* how to check nan too? some machines don't have finite().
516 don't need to check ny, it'll propogate */
517 if (nx > 1e4 || nx < -1e4) {
518 nx = halfrandom(dp, 1000) / 500.0 - 1.0;
519 ny = halfrandom(dp, 1000) / 500.0 - 1.0;
529 draw(ModeInfo * mi, driftstruct * dp, Drawable d)
531 Display *display = MI_DISPLAY(mi);
535 int fixed_x, fixed_y, npix, c, n;
541 if (!(x > -1.0 && x < 1.0 && y > -1.0 && y < 1.0))
544 fixed_x = (int) ((dp->width / 2) * (x + 1.0));
545 fixed_y = (int) ((dp->height / 2) * (y + 1.0));
549 dp->pts[dp->npoints].x = fixed_x;
550 dp->pts[dp->npoints].y = fixed_y;
552 if (dp->npoints == MAXBATCH1) {
553 XSetForeground(display, gc, dp->pixcol);
554 XDrawPoints(display, d, gc, dp->pts, dp->npoints, CoordModeOrigin);
559 npix = MI_NPIXELS(mi);
560 c = (int) (dp->c * npix);
567 dp->cpts[c * MAXBATCH2 + n].x = fixed_x;
568 dp->cpts[c * MAXBATCH2 + n].y = fixed_y;
569 if (++dp->ncpoints[c] == MAXBATCH2) {
570 XSetForeground(display, gc, MI_PIXEL(mi, c));
571 XDrawPoints(display, d, gc, &(dp->cpts[c * MAXBATCH2]),
572 dp->ncpoints[c], CoordModeOrigin);
579 draw_flush(ModeInfo * mi, driftstruct * dp, Drawable d)
581 Display *display = MI_DISPLAY(mi);
585 int npix = MI_NPIXELS(mi);
588 for (i = 0; i < npix; i++) {
589 if (dp->ncpoints[i]) {
590 XSetForeground(display, gc, MI_PIXEL(mi, i));
591 XDrawPoints(display, d, gc, &(dp->cpts[i * MAXBATCH2]),
592 dp->ncpoints[i], CoordModeOrigin);
598 XSetForeground(display, gc, dp->pixcol);
599 XDrawPoints(display, d, gc, dp->pts,
600 dp->npoints, CoordModeOrigin);
607 draw_drift(ModeInfo * mi)
609 Window window = MI_WINDOW(mi);
614 dp = &drifts[MI_SCREEN(mi)];
615 if (dp->ncpoints == NULL)
618 if (dp->erase_countdown) {
619 if (!--dp->erase_countdown) {
620 initmode(mi, frandom(dp, 2));
626 MI_IS_DRAWN(mi) = True;
630 draw(mi, dp, window);
631 if (dp->total_points++ > dp->fractal_len) {
632 draw_flush(mi, dp, window);
633 if (0 == --dp->nfractals) {
634 dp->erase_countdown = 4 * 1000000 / MI_PAUSE(mi);
644 draw_flush(mi, dp, window);
647 for (i = 0; i < dp->nxforms; i++)
648 for (j = 0; j < 2; j++)
649 for (k = 0; k < 3; k++) {
651 dp->f[j][k][i] = sin(dp->liss_time * dp->df[j][k][i]);
653 double t = dp->f[j][k][i] += dp->df[j][k][i];
655 if (t < -1.0 || 1.0 < t)
656 dp->df[j][k][i] *= -1.0;
664 refresh_drift(ModeInfo * mi)
670 XSCREENSAVER_MODULE ("Drift", drift)
672 #endif /* MODE_drift */