addaaaaf4365253deca7bfded1ab504660d2e797
[xscreensaver] / hacks / epicycle.c
1 /* epicycle --- The motion of a body with epicycles, as in the pre-Copernican
2  * cosmologies.
3  *
4  * Copyright (c) 1998  James Youngman <jay@gnu.org>
5  * 
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation.  No representations are made about the suitability of this
11  * software for any purpose.  It is provided "as is" without express or 
12  * implied warranty.
13  */
14
15 /* Standard C headers; screenhack.h assumes that these have already
16  * been included if required -- for example, it defines M_PI if not
17  * already defined.
18  */
19 #include <float.h>
20 #include <math.h>
21
22
23 #include "screenhack.h"
24 #include "erase.h"
25
26 /* MIT-SHM headers omitted; this screenhack doesn't use it */
27
28
29
30 /*********************************************************/
31 /******************** MAGIC CONSTANTS ********************/
32 /*********************************************************/
33 #define MIN_RADIUS (5)          /* smallest allowable circle radius */
34 #define FILL_PROPORTION (0.9)   /* proportion of screen to fill by scaling. */
35 /*********************************************************/
36 /***************** END OF MAGIC CONSTANTS ****************/
37 /*********************************************************/
38
39
40
41 #define FULLCIRCLE (2.0 * M_PI) /* radians in a circle. */
42
43
44 /* Name of the Screensaver hack */
45 char *progclass="Epicycle";
46
47 /* Some of these resource values here are hand-tuned to give a
48  * pleasing variety of interesting shapes.  These are not the only
49  * good settings, but you may find you need to change some as a group
50  * to get pleasing figures.
51  */
52 char *defaults [] = {
53   "*background: black",
54   "*foreground: white",
55   "*colors:     100",
56   "*color0:     red",
57   "*delay:      1000",
58   "*holdtime:   2",
59   "*lineWidth:  4",
60   "*minCircles:  2",
61   "*maxCircles:  10",
62   "*minSpeed:   0.003",
63   "*maxSpeed:   0.005",
64   "*harmonics:  8",
65   "*timestep:   1.0",
66   "*timestepCoarseFactor: 1.0", /* no option for this resource. */
67   "*divisorPoisson: 0.4",
68   "*sizeFactorMin: 1.05",
69   "*sizeFactorMax: 2.05",
70   0
71 };
72
73 /* options passed to this program */
74 XrmOptionDescRec options [] = {
75   { "-color0",          ".color0",        XrmoptionSepArg, 0 },
76   { "-colors",          ".colors",        XrmoptionSepArg, 0 },
77   { "-colours",         ".colors",        XrmoptionSepArg, 0 },
78   { "-foreground",      ".foreground",    XrmoptionSepArg, 0 },
79   { "-delay",           ".delay",         XrmoptionSepArg, 0 },
80   { "-holdtime",        ".holdtime",      XrmoptionSepArg, 0 },
81   { "-linewidth",       ".lineWidth",     XrmoptionSepArg, 0 },
82   { "-min_circles",     ".minCircles",    XrmoptionSepArg, 0 },
83   { "-max_circles",     ".maxCircles",    XrmoptionSepArg, 0 },
84   { "-min_speed",       ".minSpeed",      XrmoptionSepArg, 0 },
85   { "-max_speed",       ".maxSpeed",      XrmoptionSepArg, 0 },
86   { "-harmonics",       ".harmonics",     XrmoptionSepArg, 0 },
87   { "-timestep",        ".timestep",      XrmoptionSepArg, 0 },
88   { "-divisor_poisson",".divisorPoisson",XrmoptionSepArg, 0 },
89   { "-size_factor_min", ".sizeFactorMin", XrmoptionSepArg, 0 },
90   { "-size_factor_max", ".sizeFactorMax", XrmoptionSepArg, 0 },
91   { 0, 0, 0, 0 }
92 };
93
94
95 static Display *dpy;
96 static Window window;
97 static GC color0;
98 static int width, height;
99 static int x_offset, y_offset;
100 static int unit_pixels;
101 static unsigned long bg;
102 static Colormap cmap;
103 static int restart = 0;
104 static int stop = 0;
105 static double wdot_max;
106 static XColor *colors = NULL;
107 static int ncolors = 2;
108 static int color_shift_pos=0;   /* how far we are towards that. */
109 static double colour_cycle_rate = 1.0;
110 static int harmonics = 8;
111 static double divisorPoisson = 0.4;
112 static double sizeFactorMin = 1.05;
113 static double sizeFactorMax = 2.05;
114 static int minCircles;
115 static int maxCircles;
116
117 /* Each circle is centred on a point on the rim of another circle.
118  */
119 struct tagCircle
120 {
121   long radius;                  /* in pixels */
122   double w;                     /* position (radians ccw from x-axis) */
123   double initial_w;             /* starting position */
124   double wdot;                  /* rotation rate (change in w per iteration) */
125   int    divisor;
126
127   struct tagCircle *pchild;
128 };
129 typedef struct tagCircle Circle;
130
131
132 struct tagBody                  /* a body that moves on a system of circles. */
133 {
134   int x_origin, y_origin;
135   int x, y;
136   int old_x, old_y;
137   int current_color;            /* pixel index into colors[] */
138   Circle *epicycles;            /* system of circles on which it moves. */
139   struct tagBody *next;         /* next in list. */
140 };
141 typedef struct tagBody Body;
142
143
144 /* Determine the GCD of two numbers using Euclid's method.  The other
145  * possible algorighm is Stein's method, but it's probably only going
146  * to be much faster on machines with no divide instruction, like the
147  * ARM and the Z80.  The former is very fast anyway and the latter
148  * probably won't run X clients; in any case, this calculation is not
149  * the bulk of the computational expense of the program.  I originally
150  * tried using Stein's method, but I wanted to remove the gotos.  Not
151  * wanting to introduce possible bugs, I plumped for Euclid's method
152  * instead.  Lastly, Euclid's algorithm is preferred to the
153  * generalisation for N inputs.
154  *
155  * See Knuth, section 4.5.2.
156  */
157 static int
158 gcd(int u, int v)               /* Euclid's Method */
159 {
160   /* If either operand of % is negative, the sign of the result is
161    * implementation-defined.  See section 6.3.5 "Multiplicative
162    * Operators" of the ANSI C Standard (page 46 [LEFT HAND PAGE!] of
163    * "Annotated C Standard", Osborne, ISBN 0-07-881952-0).
164    */
165   if (u < 0) u = -u;
166   if (v < 0) v = -v;
167   
168   while (0 != v)
169     {
170       int r;
171       r = u % v;
172       u = v;
173       v = r;
174     }
175   return u;
176 }
177
178 /* Determine the Lowest Common Multiple of two integers, using
179  * Euclid's Proposition 34, as explained in Knuth's The Art of
180  * Computer Programming, Vol 2, section 4.5.2.
181  */
182 static int
183 lcm(int u, int v)
184 {
185   return u / gcd(u,v) * v;
186 }
187
188 static long 
189 random_radius(double scale)     
190 {
191   long r;
192
193   r = frand(scale) * unit_pixels/2; /* for frand() see utils/yarandom.h */
194   if (r < MIN_RADIUS)
195     r = MIN_RADIUS;
196   return r;
197 }
198
199
200 static long
201 random_divisor(void)
202 {
203   int divisor = 1;
204   int sign;
205
206   while (frand(1.0) < divisorPoisson && divisor <= harmonics)
207     {
208       ++divisor;
209     }
210   sign = (frand(1.0) < 0.5) ? +1 : -1;
211   return sign * divisor;
212 }
213
214
215 static void
216 oom(void)
217 {
218   fprintf(stderr, "Failed to allocate memory!\n");
219   exit(-1);
220 }
221
222
223 /* Construct a circle or die.
224  */
225 Circle *
226 new_circle(double scale)
227 {
228   Circle *p = malloc(sizeof(Circle));
229   
230   p->radius = random_radius(scale);
231   p->w = p->initial_w = 0.0;
232   p->divisor = random_divisor();
233   p->wdot = wdot_max / p->divisor;
234   p->pchild = NULL;
235   
236   return p;
237 }
238
239 static void delete_circle(Circle *p)
240 {
241   free(p);
242 }
243
244 static void 
245 delete_circle_chain(Circle *p)
246 {
247   while (p)
248     {
249       Circle *q = p->pchild;
250       delete_circle(p);
251       p = q;
252     }
253 }
254
255 Circle *
256 new_circle_chain(void)
257 {
258   Circle *head;
259   double scale = 1.0, factor;
260   int n;
261
262   /* Parent circles are larger than their children by a factor of at
263    * least FACTOR_MIN and at most FACTOR_MAX.
264    */
265   factor = sizeFactorMin + frand(sizeFactorMax - sizeFactorMin);
266   
267   /* There are between minCircles and maxCircles in each figure.
268    */
269   n = minCircles + rand() % (maxCircles - minCircles);
270   
271   head = NULL;
272   while (n--)
273     {
274       Circle *p = new_circle(scale);
275       p->pchild = head;
276       head = p;
277
278       scale /= factor;
279     }
280   return head;
281 }
282
283 static void
284 assign_random_common_w(Circle *p)
285 {
286   double w_common = frand(FULLCIRCLE);  /* anywhere on the circle */
287   while (p)
288     {
289       p->initial_w = w_common;
290       p = p->pchild;
291     }
292 }
293
294 static Body *
295 new_body(void)
296 {
297   Body *p = malloc(sizeof(Body));
298   if (NULL == p)
299     oom();
300   p->epicycles = new_circle_chain();
301   p->current_color = 0;         /* ?? start them all on different colors? */
302   p->next = NULL;
303   p->x = p->y = 0;
304   p->old_x = p->old_y = 0;
305   p->x_origin = p->y_origin = 0;
306
307   /* Start all the epicycles at the same w value to make it easier to
308    * figure out at what T value the cycle is closed.   We don't just fix
309    * the initial W value because that makes all the patterns tend to 
310    * be symmetrical about the X axis.
311    */
312   assign_random_common_w(p->epicycles);
313   return p;
314 }
315
316 static void
317 delete_body(Body *p)
318 {
319   delete_circle_chain(p->epicycles);
320   free(p);
321 }
322
323
324 static void 
325 draw_body(Body *pb, GC gc)
326 {
327   XDrawLine(dpy, window, gc, pb->old_x, pb->old_y, pb->x, pb->y);
328 }
329
330 static long
331 compute_divisor_lcm(Circle *p)
332 {
333   long l = 1;
334   
335   while (p)
336     {
337       l = lcm(l, p->divisor);
338       p = p->pchild;
339     }
340   return l;
341 }
342
343               
344 /* move_body()
345  *
346  * Calculate the position for the body at time T.  We work in double 
347  * rather than int to avoid the cumulative errors that would be caused
348  * by the rounding implicit in an assignment to int.
349  */
350 static void
351 move_body(Body *pb, double t)
352 {
353   Circle *p;
354   double x, y;
355
356   pb->old_x = pb->x;
357   pb->old_y = pb->y;
358   
359   x = pb->x_origin;
360   y = pb->y_origin;
361   
362   for (p=pb->epicycles; NULL != p; p=p->pchild)
363     {
364       /* angular pos = initial_pos + time * angular speed */
365       /* but this is an angular position, so modulo FULLCIRCLE. */
366       p->w = fmod(p->initial_w + (t * p->wdot), FULLCIRCLE);
367       
368       x += (p->radius * cos(p->w));
369       y += (p->radius * sin(p->w));
370     }
371   
372   pb->x = (int)x;
373   pb->y = (int)y;
374 }
375
376 static int
377 colour_init(XWindowAttributes *pxgwa)
378 {
379   XGCValues gcv;
380
381 #if 0
382   int H = random() % 360;       /* colour choice from attraction.c. */
383   double S1 = 0.25;
384   double S2 = 1.00;
385   double V = frand(0.25) + 0.75;
386   int line_width = 0;
387 #endif
388
389   int retval = 1;
390   unsigned long valuemask = 0L;
391   unsigned long fg;
392   
393   /* Free any already allocated colors...
394    */
395   if (colors)
396     {
397       free_colors(dpy, cmap, colors, ncolors);
398       colors = 0;
399       ncolors = 0;
400     }
401         
402   ncolors = get_integer_resource ("colors", "Colors");
403   if (0 == ncolors)             /* English spelling? */
404     ncolors = get_integer_resource ("colours", "Colors");
405   
406   if (ncolors < 2)
407     ncolors = 2;
408   if (ncolors <= 2)
409     mono_p = True;
410   colors = 0;
411
412   if (!mono_p)
413     {
414       colors = (XColor *) malloc(sizeof(*colors) * (ncolors+1));
415       if (!colors)
416         oom();
417           
418       make_smooth_colormap (dpy, pxgwa->visual, cmap, colors, &ncolors,
419                             True, /* allocate */
420                             False, /* not writable */
421                             True); /* verbose (complain about failure) */
422       if (ncolors <= 2)
423         {
424           if (colors)
425             free (colors);
426           colors = 0;
427           mono_p = True;
428         }
429     }
430
431   
432   bg = get_pixel_resource ("background", "Background", dpy, cmap);
433
434   /* Set the line width
435    */
436   gcv.line_width = get_integer_resource ("lineWidth", "Integer");
437   if (gcv.line_width)
438     {
439       valuemask |= GCLineWidth;
440
441       gcv.join_style = JoinRound;
442       gcv.cap_style = CapRound;
443           
444       valuemask |= (GCCapStyle | GCJoinStyle);
445     }
446   
447
448   /* Set the drawing function.
449    */
450   gcv.function = GXcopy;
451   valuemask |= GCFunction;
452   
453   /* Set the foreground.
454    */
455   if (mono_p)
456     fg = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
457   else
458     fg = bg ^ get_pixel_resource (("color0"), "Foreground", dpy, cmap); 
459   gcv.foreground = fg;
460   valuemask |= GCForeground;
461
462   /* Actually create the GC.
463    */
464   color0 = XCreateGC (dpy, window, valuemask, &gcv);
465   
466   return retval;
467 }
468
469
470 /* check_events(); originally from XScreensaver: hacks/maze.c,
471  * but now quite heavily modified.
472  *
473  * Reaction to events:-
474  *
475  * Mouse 1 -- new figure }
476  *       2 -- new figure }-- ignored when running on root window.
477  *       3 -- exit       }
478  *
479  * Window resized or exposed -- new figure.
480  * Window iconised -- wait until it's re-mapped, then start a new figure.
481  */
482 static int
483 check_events (void)                        /* X event handler [ rhess ] */
484 {
485   XEvent e;
486   int unmapped = 0;
487         
488   while (unmapped || XPending(dpy))
489     {
490       XNextEvent(dpy, &e);
491                 
492       switch (e.type)
493         {
494         case ButtonPress:
495           switch (e.xbutton.button)
496             {
497             case 3:
498               exit (0);
499               break;
500                                 
501             case 2:
502             case 1:
503             default:
504               restart = 1 ;
505               stop = 0 ;
506               break;
507             }
508           break;
509                         
510         case ConfigureNotify:
511           restart = 1;
512           break;
513                         
514         case UnmapNotify:
515           printf("unmapped!\n");
516           unmapped = 1;
517           restart = 1;                  /* restart with new fig. when re-mapped. */
518           break;
519                         
520         case Expose:            
521           if (0 == e.xexpose.count)
522             {
523                                 /* We can get several expose events in the queue.
524                                  * Only the last one has a zero count.  We eat
525                                  * events in this function so as to avoid restarting
526                                  * the screensaver many times in quick succession.
527                                  */
528               restart = 1;
529             }
530           /* If we had been unmapped and are now waiting to be re-mapped,
531            * indicate that we condition we are waiting for is now met.
532            */
533           if (unmapped)
534             printf("re-mapped!\n");
535           unmapped = 0;
536           break;
537         }
538                 
539       /* If we're unmapped, don't return to the caller.  This
540        * prevents us wasting CPU, calculating new positions for
541        * things that will never be plotted.   This is a real CPU
542        * saver.
543        */
544       if (!unmapped)
545         return 1;
546     }
547   return 0;
548 }
549
550
551 static void
552 setup(void)
553 {
554   XWindowAttributes xgwa;
555   int root;
556   
557   XGetWindowAttributes (dpy, window, &xgwa);
558   cmap = xgwa.colormap;
559
560   width = xgwa.width;
561   height = xgwa.height;
562   x_offset = width / 2;
563   y_offset = height / 2;
564   unit_pixels = width < height ? width : height;
565
566   {
567     static Bool done = False;
568     if (!done)
569       {
570         colour_init(&xgwa);
571         done = True;
572       }
573   }
574   
575   root = get_boolean_resource("root", "Boolean");
576   
577   if (root)
578     {
579       XSelectInput(dpy, window, ExposureMask);
580     }
581   else
582     {
583       XSelectInput(dpy, window,
584                    ExposureMask|ButtonPressMask|StructureNotifyMask);
585     }
586   
587 }
588 static void
589 color_step(Body *pb, double frac)
590 {
591   if (!mono_p)
592     {
593       int newshift = ncolors * fmod(frac * colour_cycle_rate, 1.0);
594       if (newshift != color_shift_pos)
595         {
596           pb->current_color = newshift;
597           XSetForeground (dpy, color0, colors[pb->current_color].pixel);
598           color_shift_pos = newshift;
599         }
600     }
601 }
602
603
604 long
605 distance(long x1, long y1, long x2, long y2)
606 {
607   long dx, dy;
608
609   dx = x2 - x1;
610   dy = y2 - y1;
611   return dx*dx + dy*dy;
612 }
613
614 #if 0
615 static int poisson_irand(double p)
616 {
617   int r = 1;
618   while (fabs(frand(1.0)) < p)
619     ++r;
620   return r < 1 ? 1 : r;
621 }
622 #endif
623
624 static void
625 precalculate_figure(Body *pb,
626                     double xtime, double step,
627                     int *x_max, int *y_max,
628                     int *x_min, int *y_min)
629 {
630   double t;
631   
632   move_body(pb, 0.0); /* move once to avoid initial line from origin */
633   *x_min = *x_max = pb->x;
634   *y_min = *y_max = pb->y;
635   
636   for (t=0.0; t<xtime; t += step)
637     {
638       move_body(pb, t); /* move once to avoid initial line from origin */
639       if (pb->x > *x_max)
640         *x_max = pb->x;
641       if (pb->x < *x_min)
642         *x_min = pb->x;
643       if (pb->y > *y_max)
644         *y_max = pb->y;
645       if (pb->y < *y_min)
646         *y_min = pb->y;
647     }
648 }
649
650 static int i_max(int a, int b)
651 {
652   return (a>b) ? a : b;
653 }
654
655 static void rescale_circles(Body *pb,
656                             int x_max, int y_max,
657                             int x_min, int y_min)
658 {
659   double xscale, yscale, scale;
660   double xm, ym;
661   
662   x_max -= x_offset;
663   x_min -= x_offset;
664   y_max -= y_offset;
665   y_min -= y_offset;
666
667   x_max = i_max(x_max, -x_min);
668   y_max = i_max(y_max, -y_min);
669
670
671   xm = width / 2.0;
672   ym = height / 2.0;
673   if (x_max > xm)
674     xscale = xm / x_max;
675   else
676     xscale = 1.0;
677   if (y_max > ym)
678     yscale = ym / y_max;
679   else
680     yscale = 1.0;
681
682   if (xscale < yscale)          /* wider than tall */
683     scale = xscale;             /* ensure width fits onscreen */
684   else
685     scale = yscale;             /* ensure height fits onscreen */
686
687
688   scale *= FILL_PROPORTION;     /* only fill FILL_PROPORTION of screen */
689   if (scale < 1.0)              /* only reduce, don't enlarge. */
690     {
691       Circle *p;
692       for (p=pb->epicycles; p; p=p->pchild)
693         {
694           p->radius *= scale;
695         }
696     }
697   else
698     {
699       printf("enlarge by x%.2f skipped...\n", scale);
700     }
701 }
702
703
704 /* angular speeds of the circles are harmonics of a fundamental
705  * value.  That should please the Pythagoreans among you... :-)
706  */
707 static double 
708 random_wdot_max(void)
709 {
710   /* Maximum and minimum values for the choice of wdot_max.  Possible
711    * epicycle speeds vary from wdot_max to (wdot_max * harmonics).
712    */
713   double minspeed, maxspeed;
714   minspeed = get_float_resource("minSpeed", "Double");
715   maxspeed = get_float_resource("maxSpeed", "Double");
716   return harmonics * (minspeed + FULLCIRCLE * frand(maxspeed-minspeed));
717 }
718
719 /* this is the function called for your screensaver */
720 /*GLOBAL*/ void
721 screenhack(Display *disp, Window win)
722 {
723   Body *pb = NULL;
724   long l;
725   double t, timestep, circle, xtime, timestep_coarse;
726   int delay;
727   int uncleared = 1;
728   int xmax, xmin, ymax, ymin;
729   int holdtime = get_integer_resource ("holdtime", "Integer");
730
731   dpy = disp;
732   window = win;
733
734   circle = FULLCIRCLE;
735   
736   XClearWindow(dpy, window);
737   uncleared = 0;
738   
739   delay = get_integer_resource ("delay", "Integer");
740   harmonics = get_integer_resource("harmonics", "Integer");
741   divisorPoisson = get_float_resource("divisorPoisson", "Double");
742   
743   timestep = get_float_resource("timestep", "Double");
744   timestep_coarse = timestep *
745     get_float_resource("timestepCoarseFactor", "Double");
746   
747   sizeFactorMin = get_float_resource("sizeFactorMin", "Double");
748   sizeFactorMax = get_float_resource("sizeFactorMax", "Double");
749
750   minCircles = get_integer_resource ("minCircles", "Integer");
751   maxCircles = get_integer_resource ("maxCircles", "Integer");
752
753   xtime = 0; /* is this right? */
754   while (0 == stop)
755     {
756       setup(); /* do this inside the loop to cope with any window resizing */
757       restart = 0;
758
759       /* Flush any outstanding events; this has the side effect of
760        * reducing the number of "false restarts"; resdtarts caused by
761        * one event (e.g. ConfigureNotify) followed by another
762        * (e.g. Expose).
763        */
764       XSync(dpy, True);
765           
766       wdot_max = random_wdot_max();
767           
768       if (pb)
769         {
770           delete_body(pb);
771           pb = NULL;
772         }
773       pb = new_body();
774       pb->x_origin = pb->x = x_offset;
775       pb->y_origin = pb->y = y_offset;
776           
777       
778       if (uncleared)
779         {
780           erase_full_window(dpy, window);
781           uncleared = 0;
782         }
783
784       fflush(stdout);
785       precalculate_figure(pb, xtime, timestep_coarse,
786                           &xmax, &ymax, &xmin, &ymin);
787
788       rescale_circles(pb, xmax, ymax, xmin, ymin);
789       
790       move_body(pb, 0.0); /* move once to avoid initial line from origin */
791       move_body(pb, 0.0); /* move once to avoid initial line from origin */
792
793       
794       t = 0.0;                  /* start at time zero. */
795
796       l = compute_divisor_lcm(pb->epicycles);
797       
798       colour_cycle_rate = fabs(l);
799       
800       xtime = fabs(l * circle / wdot_max);
801
802       if (colors)                               /* (colors==NULL) if mono_p */
803         XSetForeground (dpy, color0, colors[pb->current_color].pixel);
804
805       while (0 == restart)
806         {
807           color_step(pb, t/xtime );
808           draw_body(pb, color0);
809           uncleared = 1;
810
811           
812           /* Check if the figure is complete...*/
813           if (t > xtime)
814             {
815               XSync (dpy, False);
816
817               if (holdtime)
818                 sleep(holdtime); /* show complete figure for a bit. */
819
820               restart = 1;      /* begin new figure. */
821             }
822           
823           
824           if (delay)
825             usleep (delay);
826           
827           t += timestep;
828           move_body(pb, t);
829           check_events();
830         }
831     }
832 }
833