home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 505a.lha / GrapicsGems / BinRec.c < prev    next >
C/C++ Source or Header  |  1991-05-01  |  1KB  |  57 lines

  1. /*
  2.  * Recording Animation in Binary Order for Progressive Temporal Refinement
  3.  * by Paul Heckbert
  4.  * from "Graphics Gems", Academic Press, 1990
  5.  */
  6.  
  7. /*
  8.  * binrec.c: demonstrate binary recording order
  9.  *
  10.  * Paul Heckbert    Jan 90
  11.  */
  12.  
  13. #include <stdio.h>
  14.  
  15. main(ac, av)
  16. int ac;
  17. char **av;
  18. {
  19.     int nframes, i, start_frame, repeat_count;
  20.     if (ac!=2) {
  21.     fprintf(stderr, "Usage: binrec <nframes>\n");
  22.     exit(1);
  23.     }
  24.     nframes = atoi(av[1]);
  25.  
  26.     printf("step startframe repeatcount\n");
  27.     for (i=0; i<nframes; i++) {
  28.     inside_out(nframes, i, &start_frame, &repeat_count);
  29.     printf(" %2d     %2d          %2d\n", i, start_frame, repeat_count);
  30.     }
  31. }
  32.  
  33. /*
  34.  * inside_out: turn a number "inside-out": a generalization of bit-reversal.
  35.  * For n = power of two, this is equivalent to bit-reversal.
  36.  *
  37.  * Turn the number a inside-out, yielding b.  If 0<=a<n then 0<=b<n.
  38.  * Also return r = min(n-b, largest power of 2 dividing b)
  39.  */
  40.  
  41. inside_out(n, a, b, r)
  42. int n, a, *b, *r;
  43. {
  44.     int k, m;
  45.  
  46.     *r = m = n;
  47.     for (*b=0, k=1; k<n; k<<=1)
  48.     if (a<<1>=m) {
  49.         if (*b==0) *r = k;
  50.         *b += k;
  51.         a -= m+1>>1;
  52.         m >>= 1;
  53.     }
  54.     else m = m+1>>1;
  55.     if (*r>n-*b) *r = n-*b;
  56. }
  57.