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 # define SMOOTH_COLORS
39 # define reshape_drift 0
40 # define drift_handle_event 0
41 # include "xlockmore.h" /* in xscreensaver distribution */
43 #else /* STANDALONE */
44 # define ENTRYPOINT /**/
45 # include "xlock.h" /* in xlockmore distribution */
46 #endif /* STANDALONE */
50 #define DEF_GROW "False" /* Grow fractals instead of animating one at a time,
51 would then be like flame */
52 #define DEF_LISS "False" /* if this is defined then instead of a point
53 bouncing around in a high dimensional sphere, we
54 use lissojous figures. Only makes sense if
60 static XrmOptionDescRec opts[] =
62 {"-grow", ".drift.grow", XrmoptionNoArg, "on"},
63 {"+grow", ".drift.grow", XrmoptionNoArg, "off"},
64 {"-liss", ".drift.trail", XrmoptionNoArg, "on"},
65 {"+liss", ".drift.trail", XrmoptionNoArg, "off"}
67 static argtype vars[] =
69 {&grow, "grow", "Grow", DEF_GROW, t_Bool},
70 {&liss, "trail", "Trail", DEF_LISS, t_Bool}
72 static OptionStruct desc[] =
74 {"-/+grow", "turn on/off growing fractals, else they are animated"},
75 {"-/+liss", "turn on/off using lissojous figures to get points"}
78 ENTRYPOINT ModeSpecOpt drift_opts =
79 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
82 ModStruct drift_description =
83 {"drift", "init_drift", "draw_drift", "release_drift",
84 "refresh_drift", "init_drift", (char *) NULL, &drift_opts,
85 10000, 30, 1, 1, 64, 1.0, "",
86 "Shows cosmic drifting flame fractals", 0, NULL};
90 #define MAXBATCH1 200 /* mono */
91 #define MAXBATCH2 20 /* color */
92 #define FUSE 10 /* discard this many initial iterations */
97 /* shape of current flame */
99 double f[2][3][MAXLEV]; /* a bunch of non-homogeneous xforms */
100 int variation[10]; /* for each xform */
103 double df[2][3][MAXLEV];
105 /* high-level control */
106 int mode; /* 0->slow/single 1->fast/many */
107 int nfractals; /* draw this many fractals */
109 int fractal_len; /* pts/fractal */
111 int rainbow; /* more than one color per fractal
112 1-> computed by adding dimension to fractal */
114 int width, height; /* of window */
117 /* draw info about current flame */
118 int fuse; /* iterate this many before drawing */
119 int total_points; /* draw this many pts before fractal ends */
120 int npoints; /* how many we've computed but not drawn */
121 XPoint pts[MAXBATCH1]; /* here they are */
122 unsigned long pixcol;
123 /* when drawing in color, we have a buffer per color */
132 long saved_random_bits;
137 eraser_state *eraser;
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;
249 pick_df_coefs(ModeInfo * mi)
251 driftstruct *dp = &drifts[MI_SCREEN(mi)];
255 for (i = 0; i < dp->nxforms; i++) {
258 for (j = 0; j < 2; j++)
259 for (k = 0; k < 3; k++) {
260 dp->df[j][k][i] = ((double) halfrandom(dp, 1000) / 500.0 - 1.0);
261 r += dp->df[j][k][i] * dp->df[j][k][i];
263 r = (3 + halfrandom(dp, 5)) * 0.01 / sqrt(r);
264 for (j = 0; j < 2; j++)
265 for (k = 0; k < 3; k++)
266 dp->df[j][k][i] *= r;
271 free_drift(driftstruct *dp)
273 if (dp->ncpoints != NULL) {
274 (void) free((void *) dp->ncpoints);
275 dp->ncpoints = (int *) NULL;
277 if (dp->cpts != NULL) {
278 (void) free((void *) dp->cpts);
279 dp->cpts = (XPoint *) NULL;
284 initfractal(ModeInfo * mi)
286 driftstruct *dp = &drifts[MI_SCREEN(mi)];
292 dp->total_points = 0;
295 if ((dp->ncpoints = (int *) malloc(sizeof (int) * MI_NCOLORS(mi))) ==
302 if ((dp->cpts = (XPoint *) malloc(MAXBATCH2 * sizeof (XPoint) *
303 MI_NCOLORS(mi))) == NULL) {
310 for (i = 0; i < MI_NPIXELS(mi); i++)
314 dp->nxforms = halfrandom(dp, XFORM_LEN);
315 /* 2, 2, 2, 3, 3, 3, 4, 4, 5 */
316 dp->nxforms = (dp->nxforms >= XFORM_LEN - 1) + dp->nxforms / 3 + 2;
318 dp->c = dp->x = dp->y = 0.0;
319 if (dp->liss && !halfrandom(dp, 10)) {
324 for (i = 0; i < dp->nxforms; i++) {
325 if (NMAJORVARS == dp->major_variation)
326 dp->variation[i] = halfrandom(dp, NMAJORVARS);
328 dp->variation[i] = dp->major_variation;
329 for (j = 0; j < 2; j++)
330 for (k = 0; k < 3; k++) {
332 dp->f[j][k][i] = sin(dp->liss_time * dp->df[j][k][i]);
334 dp->f[j][k][i] = ((double) halfrandom(dp, 1000) / 500.0 - 1.0);
338 dp->pixcol = MI_PIXEL(mi, halfrandom(dp, MI_NPIXELS(mi)));
340 dp->pixcol = MI_WHITE_PIXEL(mi);
346 init_drift(ModeInfo * mi)
350 if (drifts == NULL) {
351 if ((drifts = (driftstruct *) calloc(MI_NUM_SCREENS(mi),
352 sizeof (driftstruct))) == NULL)
355 dp = &drifts[MI_SCREEN(mi)];
357 dp->width = MI_WIDTH(mi);
358 dp->height = MI_HEIGHT(mi);
359 dp->color = MI_NPIXELS(mi) > 2;
361 if (MI_IS_FULLRANDOM(mi)) {
366 dp->liss = (Bool) (LRAND() & 1);
380 iter(driftstruct * dp)
382 int i = frandom(dp, dp->nxforms);
387 nc = (dp->c + 1.0) / 2.0;
391 nx = dp->f[0][0][i] * dp->x + dp->f[0][1][i] * dp->y + dp->f[0][2][i];
392 ny = dp->f[1][0][i] * dp->x + dp->f[1][1][i] * dp->y + dp->f[1][2][i];
395 switch (dp->variation[i]) {
404 double r2 = nx * nx + ny * ny + 1e-6;
421 double r = (nx * nx + ny * ny); /* times k here is fun */
426 if (nx > 1e4 || nx < -1e4 || ny > 1e4 || ny < -1e4)
429 ny = c2 * t + c1 * ny;
430 nx = c1 * nx - c2 * ny;
438 /* Avoid atan2: DOMAIN error message */
439 if (nx == 0.0 && ny == 0.0)
442 r = atan2(nx, ny); /* times k here is fun */
447 nx = c1 * nx - c2 * ny;
448 ny = c2 * t + c1 * ny;
456 /* Avoid atan2: DOMAIN error message */
457 if (nx == 0.0 && ny == 0.0)
460 t = atan2(nx, ny) / M_PI;
462 if (nx > 1e4 || nx < -1e4 || ny > 1e4 || ny < -1e4)
465 ny = sqrt(nx * nx + ny * ny) - 1.0;
472 /* here are some others */
487 double u = nx, v = ny;
489 double emv = exp(-v);
491 nx = (ev + emv) * sin(u) / 2.0;
492 ny = (ev - emv) * cos(u) / 2.0;
509 double r = 0.5 + sqrt(nx * nx + ny * ny + 1e-6);
515 nx = atan(nx) / M_PI_2
516 ny = atan(ny) / M_PI_2
520 /* how to check nan too? some machines don't have finite().
521 don't need to check ny, it'll propogate */
522 if (nx > 1e4 || nx < -1e4) {
523 nx = halfrandom(dp, 1000) / 500.0 - 1.0;
524 ny = halfrandom(dp, 1000) / 500.0 - 1.0;
534 draw(ModeInfo * mi, driftstruct * dp, Drawable d)
536 Display *display = MI_DISPLAY(mi);
540 int fixed_x, fixed_y, npix, c, n;
546 if (!(x > -1.0 && x < 1.0 && y > -1.0 && y < 1.0))
549 fixed_x = (int) ((dp->width / 2) * (x + 1.0));
550 fixed_y = (int) ((dp->height / 2) * (y + 1.0));
554 dp->pts[dp->npoints].x = fixed_x;
555 dp->pts[dp->npoints].y = fixed_y;
557 if (dp->npoints == MAXBATCH1) {
558 XSetForeground(display, gc, dp->pixcol);
559 XDrawPoints(display, d, gc, dp->pts, dp->npoints, CoordModeOrigin);
564 npix = MI_NPIXELS(mi);
565 c = (int) (dp->c * npix);
572 dp->cpts[c * MAXBATCH2 + n].x = fixed_x;
573 dp->cpts[c * MAXBATCH2 + n].y = fixed_y;
574 if (++dp->ncpoints[c] == MAXBATCH2) {
575 XSetForeground(display, gc, MI_PIXEL(mi, c));
576 XDrawPoints(display, d, gc, &(dp->cpts[c * MAXBATCH2]),
577 dp->ncpoints[c], CoordModeOrigin);
584 draw_flush(ModeInfo * mi, driftstruct * dp, Drawable d)
586 Display *display = MI_DISPLAY(mi);
590 int npix = MI_NPIXELS(mi);
593 for (i = 0; i < npix; i++) {
594 if (dp->ncpoints[i]) {
595 XSetForeground(display, gc, MI_PIXEL(mi, i));
596 XDrawPoints(display, d, gc, &(dp->cpts[i * MAXBATCH2]),
597 dp->ncpoints[i], CoordModeOrigin);
603 XSetForeground(display, gc, dp->pixcol);
604 XDrawPoints(display, d, gc, dp->pts,
605 dp->npoints, CoordModeOrigin);
612 draw_drift(ModeInfo * mi)
614 Window window = MI_WINDOW(mi);
619 dp = &drifts[MI_SCREEN(mi)];
620 if (dp->ncpoints == NULL)
623 if (dp->erase_countdown) {
624 if (!--dp->erase_countdown) {
625 dp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), dp->eraser);
630 dp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), dp->eraser);
634 MI_IS_DRAWN(mi) = True;
638 draw(mi, dp, window);
639 if (dp->total_points++ > dp->fractal_len) {
640 draw_flush(mi, dp, window);
641 if (0 == --dp->nfractals) {
643 dp->erase_countdown = 4 * 1000000 / MI_PAUSE(mi);
644 #endif /* STANDALONE */
645 initmode(mi, frandom(dp, 2));
654 draw_flush(mi, dp, window);
657 for (i = 0; i < dp->nxforms; i++)
658 for (j = 0; j < 2; j++)
659 for (k = 0; k < 3; k++) {
661 dp->f[j][k][i] = sin(dp->liss_time * dp->df[j][k][i]);
663 double t = dp->f[j][k][i] += dp->df[j][k][i];
665 if (t < -1.0 || 1.0 < t)
666 dp->df[j][k][i] *= -1.0;
673 release_drift(ModeInfo * mi)
675 if (drifts != NULL) {
678 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
679 free_drift(&drifts[screen]);
680 (void) free((void *) drifts);
681 drifts = (driftstruct *) NULL;
686 refresh_drift(ModeInfo * mi)
691 XSCREENSAVER_MODULE ("Drift", drift)
693 #endif /* MODE_drift */