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 PROGCLASS "Drift"
36 #define HACK_INIT init_drift
37 #define HACK_DRAW draw_drift
38 #define drift_opts xlockmore_opts
39 #define DEFAULTS "*delay: 10000 \n" \
43 #include "xlockmore.h" /* in xscreensaver distribution */
45 #else /* STANDALONE */
46 #include "xlock.h" /* in xlockmore distribution */
48 #endif /* STANDALONE */
52 #define DEF_GROW "False" /* Grow fractals instead of animating one at a time,
53 would then be like flame */
54 #define DEF_LISS "False" /* if this is defined then instead of a point
55 bouncing around in a high dimensional sphere, we
56 use lissojous figures. Only makes sense if
62 static XrmOptionDescRec opts[] =
64 {"-grow", ".drift.grow", XrmoptionNoArg, "on"},
65 {"+grow", ".drift.grow", XrmoptionNoArg, "off"},
66 {"-liss", ".drift.trail", XrmoptionNoArg, "on"},
67 {"+liss", ".drift.trail", XrmoptionNoArg, "off"}
69 static argtype vars[] =
71 {&grow, "grow", "Grow", DEF_GROW, t_Bool},
72 {&liss, "liss", "Liss", DEF_LISS, t_Bool}
74 static OptionStruct desc[] =
76 {"-/+grow", "turn on/off growing fractals, else they are animated"},
77 {"-/+liss", "turn on/off using lissojous figures to get points"}
80 ModeSpecOpt drift_opts =
81 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
84 ModStruct drift_description =
85 {"drift", "init_drift", "draw_drift", "release_drift",
86 "refresh_drift", "init_drift", (char *) NULL, &drift_opts,
87 10000, 30, 1, 1, 64, 1.0, "",
88 "Shows cosmic drifting flame fractals", 0, NULL};
92 #define MAXBATCH1 200 /* mono */
93 #define MAXBATCH2 20 /* color */
94 #define FUSE 10 /* discard this many initial iterations */
99 /* shape of current flame */
101 double f[2][3][MAXLEV]; /* a bunch of non-homogeneous xforms */
102 int variation[10]; /* for each xform */
105 double df[2][3][MAXLEV];
107 /* high-level control */
108 int mode; /* 0->slow/single 1->fast/many */
109 int nfractals; /* draw this many fractals */
111 int fractal_len; /* pts/fractal */
113 int rainbow; /* more than one color per fractal
114 1-> computed by adding dimension to fractal */
116 int width, height; /* of window */
119 /* draw info about current flame */
120 int fuse; /* iterate this many before drawing */
121 int total_points; /* draw this many pts before fractal ends */
122 int npoints; /* how many we've computed but not drawn */
123 XPoint pts[MAXBATCH1]; /* here they are */
124 unsigned long pixcol;
125 /* when drawing in color, we have a buffer per color */
134 long saved_random_bits;
138 static driftstruct *drifts = (driftstruct *) NULL;
141 halfrandom(driftstruct * dp, int mv)
150 dp->lasthalf = (short) (r >> 16);
157 frandom(driftstruct * dp, int n)
162 dp->saved_random_bits = LRAND();
167 result = (int) (dp->saved_random_bits & 1);
168 dp->saved_random_bits >>= 1;
173 result = (int) (dp->saved_random_bits & 3);
174 dp->saved_random_bits >>= 2;
177 return frandom(dp, 3);
181 result = (int) (dp->saved_random_bits & 3);
182 dp->saved_random_bits >>= 2;
187 result = (int) (dp->saved_random_bits & 7);
188 dp->saved_random_bits >>= 3;
191 return frandom(dp, 5);
194 (void) fprintf(stderr, "bad arg to frandom\n");
199 #define DISTRIB_A (halfrandom(dp, 7000) + 9000)
200 #define DISTRIB_B ((frandom(dp, 3) + 1) * (frandom(dp, 3) + 1) * 120000)
201 #define LEN(x) (sizeof(x)/sizeof((x)[0]))
204 initmode(ModeInfo * mi, int mode)
206 driftstruct *dp = &drifts[MI_SCREEN(mi)];
208 #define VARIATION_LEN 14
212 dp->major_variation = halfrandom(dp, VARIATION_LEN);
213 /* 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6, 6 */
214 dp->major_variation = ((dp->major_variation >= VARIATION_LEN >> 1) &&
215 (dp->major_variation < VARIATION_LEN - 1)) ?
216 (dp->major_variation + 1) >> 1 : dp->major_variation >> 1;
221 if (!dp->color || halfrandom(dp, 8)) {
222 dp->nfractals = halfrandom(dp, 30) + 5;
223 dp->fractal_len = DISTRIB_A;
225 dp->nfractals = halfrandom(dp, 5) + 5;
226 dp->fractal_len = DISTRIB_B;
229 dp->rainbow = dp->color;
231 dp->fractal_len = DISTRIB_B;
235 dp->rainbow = dp->color;
236 dp->fractal_len = 2000000;
238 dp->fractal_len = (dp->fractal_len * MI_COUNT(mi)) / 20;
244 pick_df_coefs(ModeInfo * mi)
246 driftstruct *dp = &drifts[MI_SCREEN(mi)];
250 for (i = 0; i < dp->nxforms; i++) {
253 for (j = 0; j < 2; j++)
254 for (k = 0; k < 3; k++) {
255 dp->df[j][k][i] = ((double) halfrandom(dp, 1000) / 500.0 - 1.0);
256 r += dp->df[j][k][i] * dp->df[j][k][i];
258 r = (3 + halfrandom(dp, 5)) * 0.01 / sqrt(r);
259 for (j = 0; j < 2; j++)
260 for (k = 0; k < 3; k++)
261 dp->df[j][k][i] *= r;
266 free_drift(driftstruct *dp)
268 if (dp->ncpoints != NULL) {
269 (void) free((void *) dp->ncpoints);
270 dp->ncpoints = (int *) NULL;
272 if (dp->cpts != NULL) {
273 (void) free((void *) dp->cpts);
274 dp->cpts = (XPoint *) NULL;
279 initfractal(ModeInfo * mi)
281 driftstruct *dp = &drifts[MI_SCREEN(mi)];
287 dp->total_points = 0;
290 if ((dp->ncpoints = (int *) malloc(sizeof (int) * MI_NCOLORS(mi))) ==
297 if ((dp->cpts = (XPoint *) malloc(MAXBATCH2 * sizeof (XPoint) *
298 MI_NCOLORS(mi))) == NULL) {
305 for (i = 0; i < MI_NPIXELS(mi); i++)
309 dp->nxforms = halfrandom(dp, XFORM_LEN);
310 /* 2, 2, 2, 3, 3, 3, 4, 4, 5 */
311 dp->nxforms = (dp->nxforms >= XFORM_LEN - 1) + dp->nxforms / 3 + 2;
313 dp->c = dp->x = dp->y = 0.0;
314 if (dp->liss && !halfrandom(dp, 10)) {
319 for (i = 0; i < dp->nxforms; i++) {
320 if (NMAJORVARS == dp->major_variation)
321 dp->variation[i] = halfrandom(dp, NMAJORVARS);
323 dp->variation[i] = dp->major_variation;
324 for (j = 0; j < 2; j++)
325 for (k = 0; k < 3; k++) {
327 dp->f[j][k][i] = sin(dp->liss_time * dp->df[j][k][i]);
329 dp->f[j][k][i] = ((double) halfrandom(dp, 1000) / 500.0 - 1.0);
333 dp->pixcol = MI_PIXEL(mi, halfrandom(dp, MI_NPIXELS(mi)));
335 dp->pixcol = MI_WHITE_PIXEL(mi);
341 init_drift(ModeInfo * mi)
345 if (drifts == NULL) {
346 if ((drifts = (driftstruct *) calloc(MI_NUM_SCREENS(mi),
347 sizeof (driftstruct))) == NULL)
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 MI_IS_DRAWN(mi) = True;
622 draw(mi, dp, window);
623 if (dp->total_points++ > dp->fractal_len) {
624 draw_flush(mi, dp, window);
625 if (0 == --dp->nfractals) {
627 XSync(MI_DISPLAY(mi), False);
628 sleep(4); /* #### make settable */
629 erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
630 #endif /* STANDALONE */
631 initmode(mi, frandom(dp, 2));
640 draw_flush(mi, dp, window);
643 for (i = 0; i < dp->nxforms; i++)
644 for (j = 0; j < 2; j++)
645 for (k = 0; k < 3; k++) {
647 dp->f[j][k][i] = sin(dp->liss_time * dp->df[j][k][i]);
649 double t = dp->f[j][k][i] += dp->df[j][k][i];
651 if (t < -1.0 || 1.0 < t)
652 dp->df[j][k][i] *= -1.0;
659 release_drift(ModeInfo * mi)
661 if (drifts != NULL) {
664 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
665 free_drift(&drifts[screen]);
666 (void) free((void *) drifts);
667 drifts = (driftstruct *) NULL;
672 refresh_drift(ModeInfo * mi)
677 #endif /* MODE_drift */