summaryrefslogtreecommitdiffstats
path: root/hacks/epicycle.c
blob: a8a257cc012e5628b96d89941de0f9e6a279154e (plain) (blame)
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/* epicycle --- The motion of a body with epicycles, as in the pre-Copernican
 * cosmologies.
 *
 * Copyright (c) 1998  James Youngman <jay@gnu.org>
 * 
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

/* Standard C headers; screenhack.h assumes that these have already
 * been included if required -- for example, it defines M_PI if not
 * already defined.
 */
#include <float.h>
#include <math.h>


#include "screenhack.h"
#include "erase.h"

/* MIT-SHM headers omitted; this screenhack doesn't use it */



/*********************************************************/
/******************** MAGIC CONSTANTS ********************/
/*********************************************************/
#define MIN_RADIUS (5)		/* smallest allowable circle radius */
#define FILL_PROPORTION (0.9)	/* proportion of screen to fill by scaling. */
/*********************************************************/
/***************** END OF MAGIC CONSTANTS ****************/
/*********************************************************/



#define FULLCIRCLE (2.0 * M_PI)	/* radians in a circle. */


/* Some of these resource values here are hand-tuned to give a
 * pleasing variety of interesting shapes.  These are not the only
 * good settings, but you may find you need to change some as a group
 * to get pleasing figures.
 */
static const char *epicycle_defaults [] = {
  ".background:	black",
  ".foreground:	white",
  "*fpsSolid:	true",
  "*colors:	100",
  "*color0:	red",
  "*delay:	20000",
  "*holdtime:	2",
  "*lineWidth:	4",
  "*minCircles:  2",
  "*maxCircles:  10",
  "*minSpeed:	0.003",
  "*maxSpeed:	0.005",
  "*harmonics:	8",
  "*timestep:	1.0",
  "*timestepCoarseFactor: 1.0", /* no option for this resource. */
  "*divisorPoisson: 0.4",
  "*sizeFactorMin: 1.05",
  "*sizeFactorMax: 2.05",
#ifdef HAVE_MOBILE
  "*ignoreRotation: True",
#endif
  0
};

/* options passed to this program */
static XrmOptionDescRec epicycle_options [] = {
  { "-color0",		".color0",	  XrmoptionSepArg, 0 },
  { "-colors",		".colors",	  XrmoptionSepArg, 0 },
  { "-colours",		".colors",	  XrmoptionSepArg, 0 },
  { "-foreground",	".foreground",	  XrmoptionSepArg, 0 },
  { "-delay",		".delay",	  XrmoptionSepArg, 0 },
  { "-holdtime",	".holdtime",	  XrmoptionSepArg, 0 },
  { "-linewidth",	".lineWidth",	  XrmoptionSepArg, 0 },
  { "-min_circles",	".minCircles",	  XrmoptionSepArg, 0 },
  { "-max_circles",	".maxCircles",	  XrmoptionSepArg, 0 },
  { "-min_speed",	".minSpeed",	  XrmoptionSepArg, 0 },
  { "-max_speed",	".maxSpeed",	  XrmoptionSepArg, 0 },
  { "-harmonics",	".harmonics",	  XrmoptionSepArg, 0 },
  { "-timestep",	".timestep",	  XrmoptionSepArg, 0 },
  { "-divisor_poisson",".divisorPoisson",XrmoptionSepArg, 0 },
  { "-size_factor_min", ".sizeFactorMin", XrmoptionSepArg, 0 },
  { "-size_factor_max", ".sizeFactorMax", XrmoptionSepArg, 0 },
  { 0, 0, 0, 0 }
};


/* Each circle is centred on a point on the rim of another circle.
 */
struct tagCircle
{
  long radius;			/* in pixels */
  double w;			/* position (radians ccw from x-axis) */
  double initial_w;		/* starting position */
  double wdot;			/* rotation rate (change in w per iteration) */
  int    divisor;

  struct tagCircle *pchild;
};
typedef struct tagCircle Circle;


struct tagBody			/* a body that moves on a system of circles. */
{
  int x_origin, y_origin;
  int x, y;
  int old_x, old_y;
  int current_color;		/* pixel index into colors[] */
  Circle *epicycles;		/* system of circles on which it moves. */
  struct tagBody *next;		/* next in list. */
};
typedef struct tagBody Body;


struct state {
   Display *dpy;
   Window window;
   GC color0;
   int width, height;
   int x_offset, y_offset;
   int unit_pixels;
   unsigned long bg;
   Colormap cmap;
   int restart;
   double wdot_max;
   XColor *colors;
   int ncolors;
   int color_shift_pos;	/* how far we are towards that. */
   double colour_cycle_rate;
   int harmonics;
   double divisorPoisson;
   double sizeFactorMin;
   double sizeFactorMax;
   int minCircles;
   int maxCircles;

   Bool done;

   long L;
   double T, timestep, circle, timestep_coarse;
   int delay;
   int uncleared;
   int holdtime;
   int xmax, xmin, ymax, ymin;
   Body *pb0;
   double xtime;
   eraser_state *eraser;
};



/* Determine the GCD of two numbers using Euclid's method.  The other
 * possible algorighm is Stein's method, but it's probably only going
 * to be much faster on machines with no divide instruction, like the
 * ARM and the Z80.  The former is very fast anyway and the latter
 * probably won't run X clients; in any case, this calculation is not
 * the bulk of the computational expense of the program.  I originally
 * tried using Stein's method, but I wanted to remove the gotos.  Not
 * wanting to introduce possible bugs, I plumped for Euclid's method
 * instead.  Lastly, Euclid's algorithm is preferred to the
 * generalisation for N inputs.
 *
 * See Knuth, section 4.5.2.
 */
static int
gcd(int u, int v)		/* Euclid's Method */
{
  /* If either operand of % is negative, the sign of the result is
   * implementation-defined.  See section 6.3.5 "Multiplicative
   * Operators" of the ANSI C Standard (page 46 [LEFT HAND PAGE!] of
   * "Annotated C Standard", Osborne, ISBN 0-07-881952-0).
   */
  if (u < 0) u = -u;
  if (v < 0) v = -v;
  
  while (0 != v)
    {
      int r;
      r = u % v;
      u = v;
      v = r;
    }
  return u;
}

/* Determine the Lowest Common Multiple of two integers, using
 * Euclid's Proposition 34, as explained in Knuth's The Art of
 * Computer Programming, Vol 2, section 4.5.2.
 */
static int
lcm(int u, int v)
{
  return u / gcd(u,v) * v;
}

static long 
random_radius(struct state *st, double scale)	
{
  long r;

  r = frand(scale) * st->unit_pixels/2; /* for frand() see utils/yarandom.h */
  if (r < MIN_RADIUS)
    r = MIN_RADIUS;
  return r;
}


static long
random_divisor(struct state *st)
{
  int divisor = 1;
  int sign;

  while (frand(1.0) < st->divisorPoisson && divisor <= st->harmonics)
    {
      ++divisor;
    }
  sign = (frand(1.0) < 0.5) ? +1 : -1;
  return sign * divisor;
}


/* Construct a circle or die.
 */
static Circle *
new_circle(struct state *st, double scale)
{
  Circle *p = malloc(sizeof(Circle));
  
  p->radius = random_radius(st, scale);
  p->w = p->initial_w = 0.0;
  p->divisor = random_divisor(st);
  p->wdot = st->wdot_max / p->divisor;
  p->pchild = NULL;
  
  return p;
}

static void delete_circle(Circle *p)
{
  free(p);
}

static void 
delete_circle_chain(Circle *p)
{
  while (p)
    {
      Circle *q = p->pchild;
      delete_circle(p);
      p = q;
    }
}

static Circle *
new_circle_chain(struct state *st)
{
  Circle *head;
  double scale = 1.0, factor;
  int n;

  /* Parent circles are larger than their children by a factor of at
   * least FACTOR_MIN and at most FACTOR_MAX.
   */
  factor = st->sizeFactorMin + frand(st->sizeFactorMax - st->sizeFactorMin);
  
  /* There are between minCircles and maxCircles in each figure.
   */
  if (st->maxCircles == st->minCircles)
    n = st->minCircles;            /* Avoid division by zero. */
  else
    n = st->minCircles + random() % (st->maxCircles - st->minCircles);
  
  head = NULL;
  while (n--)
    {
      Circle *p = new_circle(st, scale);
      p->pchild = head;
      head = p;

      scale /= factor;
    }
  return head;
}

static void
assign_random_common_w(Circle *p)
{
  double w_common = frand(FULLCIRCLE);	/* anywhere on the circle */
  while (p)
    {
      p->initial_w = w_common;
      p = p->pchild;
    }
}

static Body *
new_body(struct state *st)
{
  Body *p = malloc(sizeof(Body));
  if (!p) abort();
  p->epicycles = new_circle_chain(st);
  p->current_color = 0;		/* ?? start them all on different colors? */
  p->next = NULL;
  p->x = p->y = 0;
  p->old_x = p->old_y = 0;
  p->x_origin = p->y_origin = 0;

  /* Start all the epicycles at the same w value to make it easier to
   * figure out at what T value the cycle is closed.   We don't just fix
   * the initial W value because that makes all the patterns tend to 
   * be symmetrical about the X axis.
   */
  assign_random_common_w(p->epicycles);
  return p;
}

static void
delete_body(Body *p)
{
  delete_circle_chain(p->epicycles);
  free(p);
}


static void 
draw_body(struct state *st, Body *pb, GC gc)
{
  XDrawLine(st->dpy, st->window, gc, pb->old_x, pb->old_y, pb->x, pb->y);
}

static long
compute_divisor_lcm(Circle *p)
{
  long l = 1;
  
  while (p)
    {
      l = lcm(l, p->divisor);
      p = p->pchild;
    }
  return l;
}

	      
/* move_body()
 *
 * Calculate the position for the body at time T.  We work in double 
 * rather than int to avoid the cumulative errors that would be caused
 * by the rounding implicit in an assignment to int.
 */
static void
move_body(Body *pb, double t)
{
  Circle *p;
  double x, y;

  pb->old_x = pb->x;
  pb->old_y = pb->y;
  
  x = pb->x_origin;
  y = pb->y_origin;
  
  for (p=pb->epicycles; NULL != p; p=p->pchild)
    {
      /* angular pos = initial_pos + time * angular speed */
      /* but this is an angular position, so modulo FULLCIRCLE. */
      p->w = fmod(p->initial_w + (t * p->wdot), FULLCIRCLE);
      
      x += (p->radius * cos(p->w));
      y += (p->radius * sin(p->w));
    }
  
  pb->x = (int)x;
  pb->y = (int)y;
}

static int
colour_init(struct state *st, XWindowAttributes *pxgwa)
{
  XGCValues gcv;

#if 0
  int H = random() % 360;	/* colour choice from attraction.c. */
  double S1 = 0.25;
  double S2 = 1.00;
  double V = frand(0.25) + 0.75;
  int line_width = 0;
#endif

  int retval = 1;
  unsigned long valuemask = 0L;
  unsigned long fg;
  
  /* Free any already allocated colors...
   */
  if (st->colors)
    {
      free_colors(pxgwa->screen, st->cmap, st->colors, st->ncolors);
      st->colors = 0;
      st->ncolors = 0;
    }
	
  st->ncolors = get_integer_resource (st->dpy, "colors", "Colors");
  if (0 == st->ncolors)		/* English spelling? */
    st->ncolors = get_integer_resource (st->dpy, "colours", "Colors");
  
  if (st->ncolors < 2)
    st->ncolors = 2;
  if (st->ncolors <= 2)
    mono_p = True;
  st->colors = 0;

  if (!mono_p)
    {
      st->colors = (XColor *) malloc(sizeof(*st->colors) * (st->ncolors+1));
      if (!st->colors) abort();
	  
      make_smooth_colormap (pxgwa->screen, pxgwa->visual, st->cmap,
                            st->colors, &st->ncolors,
			    True, /* allocate */
			    False, /* not writable */
			    True); /* verbose (complain about failure) */
      if (st->ncolors <= 2)
	{
	  if (st->colors)
	    free (st->colors);
	  st->colors = 0;
	  mono_p = True;
	}
    }

  
  st->bg = get_pixel_resource (st->dpy, st->cmap, "background", "Background");

  /* Set the line width
   */
  gcv.line_width = get_integer_resource (st->dpy, "lineWidth", "Integer");
  if (gcv.line_width)
    {
      valuemask |= GCLineWidth;

      gcv.join_style = JoinRound;
      gcv.cap_style = CapRound;
	  
      valuemask |= (GCCapStyle | GCJoinStyle);
    }
  

  /* Set the drawing function.
   */
  gcv.function = GXcopy;
  valuemask |= GCFunction;
  
  /* Set the foreground.
   */
/*  if (mono_p)*/
    fg = get_pixel_resource (st->dpy, st->cmap, "foreground", "Foreground");
/* WTF?
else
    fg = st->bg ^ get_pixel_resource (st->dpy, st->cmap, ("color0"), "Foreground"); 
*/
  gcv.foreground = fg;
  valuemask |= GCForeground;

  /* Actually create the GC.
   */
  st->color0 = XCreateGC (st->dpy, st->window, valuemask, &gcv);
  
  return retval;
}




static void
setup(struct state *st)
{
  XWindowAttributes xgwa;
  
  XGetWindowAttributes (st->dpy, st->window, &xgwa);
  st->cmap = xgwa.colormap;

  st->width = xgwa.width;
  st->height = xgwa.height;
  st->x_offset = st->width / 2;
  st->y_offset = st->height / 2;
  st->unit_pixels = st->width < st->height ? st->width : st->height;

  {
    if (!st->done)
      {
	colour_init(st, &xgwa);
	st->done = True;
      }
  }
}


static void
color_step(struct state *st, Body *pb, double frac)
{
  if (!mono_p)
    {
      int newshift = st->ncolors * fmod(frac * st->colour_cycle_rate, 1.0);
      if (newshift != st->color_shift_pos)
	{
	  pb->current_color = newshift;
	  XSetForeground (st->dpy, st->color0, st->colors[pb->current_color].pixel);
	  st->color_shift_pos = newshift;
	}
    }
}


#if 0
static long
distance(long x1, long y1, long x2, long y2)
{
  long dx, dy;

  dx = x2 - x1;
  dy = y2 - y1;
  return dx*dx + dy*dy;
}

static int poisson_irand(double p)
{
  int r = 1;
  while (fabs(frand(1.0)) < p)
    ++r;
  return r < 1 ? 1 : r;
}
#endif

static void
precalculate_figure(Body *pb,
		    double this_xtime, double step,
		    int *x_max, int *y_max,
		    int *x_min, int *y_min)
{
  double t;
  
  move_body(pb, 0.0); /* move once to avoid initial line from origin */
  *x_min = *x_max = pb->x;
  *y_min = *y_max = pb->y;
  
  for (t=0.0; t<this_xtime; t += step)
    {
      move_body(pb, t); /* move once to avoid initial line from origin */
      if (pb->x > *x_max)
	*x_max = pb->x;
      if (pb->x < *x_min)
	*x_min = pb->x;
      if (pb->y > *y_max)
	*y_max = pb->y;
      if (pb->y < *y_min)
	*y_min = pb->y;
    }
}

static int i_max(int a, int b)
{
  return (a>b) ? a : b;
}

static void rescale_circles(struct state *st, Body *pb,
			    int x_max, int y_max,
			    int x_min, int y_min)
{
  double xscale, yscale, scale;
  double xm, ym;
  
  x_max -= st->x_offset;
  x_min -= st->x_offset;
  y_max -= st->y_offset;
  y_min -= st->y_offset;

  x_max = i_max(x_max, -x_min);
  y_max = i_max(y_max, -y_min);


  xm = st->width / 2.0;
  ym = st->height / 2.0;
  if (x_max > xm)
    xscale = xm / x_max;
  else
    xscale = 1.0;
  if (y_max > ym)
    yscale = ym / y_max;
  else
    yscale = 1.0;

  if (xscale < yscale)		/* wider than tall */
    scale = xscale;		/* ensure width fits onscreen */
  else
    scale = yscale;		/* ensure height fits onscreen */


  scale *= FILL_PROPORTION;	/* only fill FILL_PROPORTION of screen */
  if (scale < 1.0)		/* only reduce, don't enlarge. */
    {
      Circle *p;
      for (p=pb->epicycles; p; p=p->pchild)
	{
	  p->radius *= scale;
	}
    }
  else
    {
      printf("enlarge by x%.2f skipped...\n", scale);
    }

  if (st->width > st->height * 5 ||  /* window has weird aspect */
      st->height > st->width * 5)
    {
      Circle *p;
      double r = (st->width > st->height
                  ? st->width / (double) st->height
                  : st->height / (double) st->width);
      for (p=pb->epicycles; p; p=p->pchild)
        p->radius *= r;
    }
}


/* angular speeds of the circles are harmonics of a fundamental
 * value.  That should please the Pythagoreans among you... :-)
 */
static double 
random_wdot_max(struct state *st)
{
  /* Maximum and minimum values for the choice of wdot_max.  Possible
   * epicycle speeds vary from wdot_max to (wdot_max * harmonics).
   */
  double minspeed, maxspeed;
  minspeed = get_float_resource(st->dpy, "minSpeed", "Double");
  maxspeed = get_float_resource(st->dpy, "maxSpeed", "Double");
  return st->harmonics * (minspeed + FULLCIRCLE * frand(maxspeed-minspeed));
}


static void *
epicycle_init (Display *disp, Window win)
{
  struct state *st = (struct state *) calloc (1, sizeof(*st));
  st->dpy = disp;
  st->window = win;

  st->holdtime = get_integer_resource (st->dpy, "holdtime", "Integer");

  st->circle = FULLCIRCLE;
  
  XClearWindow(st->dpy, st->window);
  st->uncleared = 0;
  st->restart = 1;
  
  st->delay = get_integer_resource (st->dpy, "delay", "Integer");
  st->harmonics = get_integer_resource(st->dpy, "harmonics", "Integer");
  st->divisorPoisson = get_float_resource(st->dpy, "divisorPoisson", "Double");
  
  st->timestep = get_float_resource(st->dpy, "timestep", "Double");
  st->timestep_coarse = st->timestep *
    get_float_resource(st->dpy, "timestepCoarseFactor", "Double");
  
  st->sizeFactorMin = get_float_resource(st->dpy, "sizeFactorMin", "Double");
  st->sizeFactorMax = get_float_resource(st->dpy, "sizeFactorMax", "Double");

  st->minCircles = get_integer_resource (st->dpy, "minCircles", "Integer");
  st->maxCircles = get_integer_resource (st->dpy, "maxCircles", "Integer");

  st->xtime = 0; /* is this right? */

  return st;
}

static unsigned long
epicycle_draw (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  int this_delay = st->delay;

  if (st->eraser) {
    st->eraser = erase_window (st->dpy, st->window, st->eraser);
    return 10000;
  }

  if (st->restart)
    {
      setup(st);
      st->restart = 0;

      /* Flush any outstanding events; this has the side effect of
       * reducing the number of "false restarts"; resdtarts caused by
       * one event (e.g. ConfigureNotify) followed by another
       * (e.g. Expose).
       */
	  
      st->wdot_max = random_wdot_max(st);
	  
      if (st->pb0)
        {
          delete_body(st->pb0);
          st->pb0 = NULL;
        }
      st->pb0 = new_body(st);
      st->pb0->x_origin = st->pb0->x = st->x_offset;
      st->pb0->y_origin = st->pb0->y = st->y_offset;

      if (st->uncleared)
        {
          st->eraser = erase_window (st->dpy, st->window, st->eraser);
          st->uncleared = 0;
        }

      precalculate_figure(st->pb0, st->xtime, st->timestep_coarse,
                          &st->xmax, &st->ymax, &st->xmin, &st->ymin);

      rescale_circles(st, st->pb0, st->xmax, st->ymax, st->xmin, st->ymin);
      
      move_body(st->pb0, 0.0); /* move once to avoid initial line from origin */
      move_body(st->pb0, 0.0); /* move once to avoid initial line from origin */

      
      st->T = 0.0;			/* start at time zero. */

      st->L = compute_divisor_lcm(st->pb0->epicycles);
      
      st->colour_cycle_rate = labs(st->L);
      
      st->xtime = fabs(st->L * st->circle / st->wdot_max);

      if (st->colors)				/* (colors==NULL) if mono_p */
        XSetForeground (st->dpy, st->color0, st->colors[st->pb0->current_color].pixel);
    }


  color_step(st, st->pb0, st->T/st->xtime );
  draw_body(st, st->pb0, st->color0);
  st->uncleared = 1;

	  
  /* Check if the figure is complete...*/
  if (st->T > st->xtime)
    {
      this_delay = st->holdtime * 1000000;
      st->restart = 1;	/* begin new figure. */
    }
	  


  st->T += st->timestep;
  move_body(st->pb0, st->T);

  return this_delay;
}

static void
epicycle_reshape (Display *dpy, Window window, void *closure, 
                 unsigned int w, unsigned int h)
{
  struct state *st = (struct state *) closure;
  st->restart = 1;
}

static Bool
epicycle_event (Display *dpy, Window window, void *closure, XEvent *e)
{
  struct state *st = (struct state *) closure;
  if (screenhack_event_helper (dpy, window, e))
    {
      st->restart = 1;
      return True;
    }

  return False;
}

static void
epicycle_free (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  if (st->eraser) eraser_free (st->eraser);  
  if (st->pb0) delete_body (st->pb0);
  XFreeGC (dpy, st->color0);
  free (st->colors);
  free (st);
}

XSCREENSAVER_MODULE ("Epicycle", epicycle)