8 NSTEP = 10, /* number of steps between throws */
9 RBALL = 10, /* radius of ball images */
16 int delay=20; /* ms delay between steps */
24 #define inset insetrect
28 * pattern lists the heights of a repeating sequence of throws.
29 * At time t, hand t%nhand throws. At that time, it must
30 * hold exactly one ball, unless it executes a 0 throw,
31 * in which case it must hold no ball. A throw of height h
32 * at time t lands at time t+h in hand (t+h)%nhand.
34 typedef struct Ball Ball;
36 int oldhand; /* hand that previously held the ball */
37 int hgt; /* how high the throw from oldhand was */
38 int time; /* time at which ball will arrive */
39 int hand; /* hand in which ball will rest on arrival */
42 void throw(int t, int hgt){
46 for(i=0;i!=nball;i++) if(ball[i].hand==hand && ball[i].time<=t){
52 print("bad zero throw at t=%d, nball=%d\n", t, n);
57 print("bad ball count at t=%d, nball=%d\n", t, n);
64 ball[b].hand=(hand+hgt)%nhand;
67 Point bpos(int b, int step, int t){
69 double dt=t-1+(step+1.)/NSTEP-(bp->time-bp->hgt);
70 double hgt=(bp->hgt*dt-dt*dt)*4./(maxhgt*maxhgt);
71 double alpha=(bp->oldhand+(bp->hand-bp->oldhand)*dt/bp->hgt)/(nhand-1);
72 return (Point){win.min.x+(win.max.x-win.min.x)*alpha,
73 win.max.y-1+(win.min.y-win.max.y)*hgt};
78 for(i=0;i!=NSTEP;i++){
79 while(ecanmouse()) emouse();
80 draw(image, inset(image->r, 3), display->white, nil, ZP);
82 fillellipse(image, bpos(j, i, t), RBALL, RBALL, disk[j%ndisk], ZP);
83 draw(screen, screen->r, image, nil, image->r.min);
84 flushimage(display, 1);
94 disk = realloc(disk, (ndisk+1)*sizeof(Image*));
95 col=allocimage(display, Rect(0,0,1,1), CMAP8, 1, c);
103 /* colors taken from /sys/src/cmd/stats.c */
106 adddisk(DPalegreygreen);
107 adddisk(DDarkyellow);
113 adddisk(DPurpleblue);
114 adddisk(DYellowgreen);
123 fprint(2, "usage: %s [start] pattern\n", name);
129 if(new && getwindow(display, Refnone) < 0) {
130 sysfatal("can't reattach to window");
132 if(image) freeimage(image);
133 image=allocimage(display, screen->r, screen->chan, 0, DNofill);
134 draw(image, image->r, display->black, nil, ZP);
135 win=inset(screen->r, 4+2*RBALL);
138 main(int argc, char *argv[]){
139 int sum, i, t, hgt, nstart, npattern;
140 char *s, *start = nil, *pattern = nil;
149 delay = strtol(argv[0], &s, 0);
150 if(delay < 0 || s == argv[0] || *s != '\0')
157 nhand = strtol(argv[0], &s, 0);
158 if(nhand <= 0 || s == argv[0] || *s != '\0')
177 for(s=pattern;*s;s++){
180 if(maxhgt<hgt) maxhgt=hgt;
185 if(maxhgt<hgt) maxhgt=hgt;
188 print("%s: non-integral ball count\n",argv[0]);
189 exits("partial ball");
192 for(i=0;i!=nball;i++){
193 ball[i].oldhand=(i-nball)%nhand;
194 if(ball[i].oldhand<0) ball[i].oldhand+=nhand;
197 ball[i].hand=i%nhand;
199 if(initdraw(nil, nil, "juggle") < 0)
200 sysfatal("initdraw failed: %r");
205 print("can't allocate bitmap");
208 for(t=0;start[t];t++){
210 throw(t, start[t]-'0');
215 throw(t, pattern[(t-nstart)%npattern]-'0');