home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 2 / LSD and 17bit Compendium Deluxe - Volume II.iso / a / prog / cprog / voronoi.lha / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-19  |  2.0 KB  |  107 lines

  1. #
  2. #include <stdio.h>
  3. #include "defs.h"
  4. struct Site *readone();
  5. struct Site *nextone();
  6.  
  7. main(argc,argv) 
  8. char **argv; 
  9. int argc;
  10. {    
  11. int c;
  12. struct Site *(*next)();
  13.  
  14. sorted = 0; triangulate = 0; plot = 0; debug = 0;
  15. while((c=getopt(argc,argv,"dpst")) != EOF)
  16.     switch(c) {
  17.     case 'd': debug = 1;
  18.           break;
  19.     case 's': sorted = 1;
  20.           break;
  21.     case 't': triangulate = 1;
  22.           break;
  23.     case 'p': plot = 1;
  24.           break;
  25.           };
  26.  
  27. freeinit(&sfl, sizeof *sites);
  28. if(sorted)
  29. {    scanf("%d %f %f %f %f", &nsites, &xmin, &xmax, &ymin, &ymax);
  30.     next = readone;
  31. }
  32. else 
  33. {    readsites();
  34.     next = nextone;
  35. };
  36.  
  37. siteidx = 0;
  38. geominit();
  39. if(plot) plotinit();
  40.  
  41. voronoi(triangulate, next); 
  42.  
  43. }
  44.  
  45. /* sort sites on y, then x, coord */
  46. int scomp(s1,s2)
  47. struct Point *s1,*s2;
  48. {
  49.     if(s1 -> y < s2 -> y) return(-1);
  50.     if(s1 -> y > s2 -> y) return(1);
  51.     if(s1 -> x < s2 -> x) return(-1);
  52.     if(s1 -> x > s2 -> x) return(1);
  53.     return(0);
  54. }
  55.  
  56. /* return a single in-storage site */
  57. struct Site *nextone()
  58. {
  59. struct Site *s;
  60. if(siteidx < nsites)
  61. {    s = &sites[siteidx];
  62.     siteidx += 1;
  63.     return(s);
  64. }
  65. else    return( (struct Site *)NULL);
  66. }
  67.  
  68.  
  69. /* read all sites, sort, and compute xmin, xmax, ymin, ymax */
  70. readsites()
  71. {
  72. int i;
  73.  
  74. nsites=0;
  75. sites = (struct Site *) myalloc(4000*sizeof *sites);
  76. while(scanf("%f %f", &sites[nsites].coord.x, &sites[nsites].coord.y)!=EOF)
  77. {    sites[nsites].sitenbr = nsites;
  78.     sites[nsites].refcnt = 0;
  79.     nsites += 1;
  80.     if (nsites % 4000 == 0) 
  81.         sites = (struct Site *) realloc(sites,(nsites+4000)*sizeof*sites);
  82. };
  83. qsort(sites, nsites, sizeof *sites, scomp);
  84. xmin=sites[0].coord.x; 
  85. xmax=sites[0].coord.x;
  86. for(i=1; i<nsites; i+=1)
  87. {    if(sites[i].coord.x < xmin) xmin = sites[i].coord.x;
  88.     if(sites[i].coord.x > xmax) xmax = sites[i].coord.x;
  89. };
  90. ymin = sites[0].coord.y;
  91. ymax = sites[nsites-1].coord.y;
  92. }
  93.  
  94. /* read one site */
  95. struct Site *readone()
  96. {
  97. struct Site *s;
  98.  
  99. s = (struct Site *) getfree(&sfl);
  100. s -> refcnt = 0;
  101. s -> sitenbr = siteidx;
  102. siteidx += 1;
  103. if(scanf("%f %f", &(s->coord.x), &(s->coord.y)) == EOF)
  104.     return ((struct Site *) NULL );
  105. return(s);
  106. }
  107.