home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / piemnsrc.sit / plotcircle.c < prev    next >
C/C++ Source or Header  |  1989-09-14  |  6KB  |  280 lines

  1. #include <stdio.h>
  2.  
  3.  
  4. #define    I_RADIUS        40                /* Radius of inner circle */
  5. #define    O_RADIUS        60                /* Radius of outer circle */
  6. #define    C_RADIUS        5                /* Radius of center radius */
  7. #define    CENTER_X        0
  8. #define    CENTER_Y        0
  9. #define    MAX_PTS            1000
  10. #define    X_COORD            1
  11. #define    Y_COORD            2
  12. #define    I_POINTS        1
  13. #define    O_POINTS        2
  14. #define    C_POINTS        3
  15.  
  16.  
  17. /* This is the representation of a point. */
  18. struct    xy_pt {
  19.     int        x;
  20.     int        y;
  21. } pt[MAX_PTS];
  22.  
  23. /* Array index into which to generate the next point. */
  24. int num_pts;
  25.  
  26.  
  27. /* File into which the data is written. */
  28. FILE    *fp;
  29.  
  30.  
  31. /* Local Function prototypes. */
  32. void    circle(int xc, int y_center, int radius);
  33. int        sort(struct xy_pt *, int, int);
  34. int        compare(struct xy_pt, struct xy_pt, int);
  35. int        swap(struct xy_pt *, struct xy_pt *);
  36.  
  37.  
  38.  
  39.  
  40.  
  41. main()
  42. {
  43.     if ((fp = fopen("circledata.h", "w")) != NULL) {
  44.         /* Generate the set of interior points */
  45.         circle(CENTER_X, CENTER_Y, I_RADIUS);        /* Generate the points. */
  46.         sortpoints();                                /* Sort the points by x,y */
  47.         eliminate_duplicates();                        /* Duplicate points aren't needed */
  48.         generate_quadrants();                        /* Flip quadrant 1 three times. */
  49.         dumppoints(I_POINTS);                        /* Print the list of points. */
  50.  
  51.         /* Generate the set of outer points */
  52.         circle(CENTER_X, CENTER_Y, O_RADIUS);        /* Generate the points. */
  53.         sortpoints();                                /* Sort the points by x,y */
  54.         eliminate_duplicates();                        /* Duplicate points aren't needed */
  55.         generate_quadrants();                        /* Flip quadrant 1 three times. */
  56.         dumppoints(O_POINTS);                        /* Print the list of points. */
  57.  
  58.         /* Generate the set of center points */
  59.         circle(CENTER_X, CENTER_Y, C_RADIUS);        /* Generate the points. */
  60.         sortpoints();                                /* Sort the points by x,y */
  61.         eliminate_duplicates();                        /* Duplicate points aren't needed */
  62.         generate_quadrants();                        /* Flip quadrant 1 three times. */
  63.         dumppoints(C_POINTS);                        /* Print the list of points. */
  64.  
  65.         fclose(fp);
  66.     }
  67. }
  68.  
  69.  
  70.  
  71. void
  72. circle(x_center, y_center, radius)
  73.     int x_center, y_center ,radius;
  74. {
  75.     int     x,y,d;
  76.     
  77.     num_pts = 0;
  78.     y = radius;
  79.     d = 3 - 2 * radius;
  80.     
  81.     
  82.     /* Use x,y to control the plotting of the first octant. */
  83.     for (x = 0; x < y;) {
  84.         if (num_pts + 1 >= MAX_PTS)
  85.             return;
  86.         pt[num_pts].x   =  x + x_center;
  87.         pt[num_pts++].y = -y + y_center;
  88.         pt[num_pts].x   =  y + x_center;
  89.         pt[num_pts++].y = -x + y_center;
  90.         if (d < 0)
  91.             d += 4 * x + 6;
  92.         else {
  93.             d += 4 * (x - y) + 10;
  94.             --y;
  95.         }
  96.         ++x;
  97.     }
  98.     
  99.     /* If we happened to land on the last point, add it to the list */
  100.     if (x == y) {
  101.         pt[num_pts].x   =  x + x_center;
  102.         pt[num_pts++].y = -y + y_center;
  103.     }
  104.         
  105.     fprintf(fp,"\n");
  106. }
  107.  
  108.  
  109.  
  110. sortpoints()
  111. {
  112.     sort(pt,num_pts,X_COORD);
  113.     sort(pt,num_pts,Y_COORD);
  114. }
  115.  
  116.  
  117.  
  118. eliminate_duplicates()
  119. {
  120. }
  121.  
  122.  
  123.  
  124. int
  125. generate_quadrants()
  126. {
  127.     int i;
  128.     
  129.     /* 
  130.      * Second quadrant same as first with a positive y coordinates.
  131.      * Note that the end points are not duplicated.
  132.      */
  133.     for (i = num_pts - 1; i >= 0; --i) {
  134.         pt[num_pts].x =  pt[i].x;
  135.         pt[num_pts].y = -pt[i].y;
  136.         ++num_pts;
  137.     }
  138.     
  139.     
  140.     /* 3rd & 4th quadrants same as 1st & 2nd with negative x coordinates */
  141.     for (i = num_pts - 1; i >= 0; --i) {
  142.         pt[num_pts].x = -pt[i].x;
  143.         pt[num_pts].y = pt[i].y;
  144.         ++num_pts;
  145.     }
  146. }
  147.  
  148.  
  149.  
  150. int
  151. dumppoints(type)
  152.     int        type;
  153. {
  154.     int i;
  155.     int cols = 0;
  156.     
  157.     
  158.     /* Print out the title. */
  159.     fprintf(fp, "/*\n");
  160.     fprintf(fp, " * Points of a circle with origin at %d,%d ", CENTER_X, CENTER_Y);
  161.     if (type == I_POINTS) {
  162.         fprintf(fp, " * with a radius of: %d\n", I_RADIUS);
  163.         fprintf(fp, " */\n\n");
  164.         fprintf(fp,"#define\t\tCENTER_X\t\t\t%d\n", CENTER_X);
  165.         fprintf(fp,"#define\t\tCENTER_Y\t\t\t%d\n\n", CENTER_Y);
  166.         fprintf(fp,"#define\t\tI_RADIUS\t\t\t%d\n", I_RADIUS);
  167.         fprintf(fp,"#define\t\tNUM_I_POINTS\t\t%d\n\n",num_pts);
  168.     }
  169.     else if (type == O_POINTS) {
  170.         fprintf(fp, "with a radius of: %d\n", O_RADIUS);
  171.         fprintf(fp, " */\n");
  172.         fprintf(fp,"#define\t\tO_RADIUS\t\t\t%d\n", O_RADIUS);
  173.         fprintf(fp,"#define\t\tNUM_O_POINTS\t\t%d\n\n",num_pts);
  174.     }        
  175.     else {
  176.         fprintf(fp, "with a radius of: %d\n", C_RADIUS);
  177.         fprintf(fp, " */\n");
  178.         fprintf(fp,"#define\t\tC_RADIUS\t\t\t%d\n", C_RADIUS);
  179.         fprintf(fp,"#define\t\tNUM_C_POINTS\t\t%d\n\n",num_pts);
  180.     }        
  181.     
  182.     
  183.     /* Print the structure description we are about to define */
  184.     if (type == I_POINTS) {
  185.         fprintf(fp,"/* This is the representation of a point. */\n");
  186.         fprintf(fp,"struct    xy_pt {\n");
  187.         fprintf(fp,"\tint    x;\n");
  188.         fprintf(fp,"\tint    y;\n");
  189.         fprintf(fp,"};\n\n");
  190.         fprintf(fp,"struct xy_pt i_circle_points[NUM_I_POINTS] = {\n\t");
  191.     }
  192.     else if (type == O_POINTS) {
  193.         fprintf(fp,"struct xy_pt o_circle_points[NUM_O_POINTS] = {\n\t");
  194.     }
  195.     else {
  196.         fprintf(fp,"struct xy_pt c_circle_points[NUM_C_POINTS] = {\n\t");
  197.     }
  198.     
  199.     for (i = 0; i < num_pts; i++) {
  200.         fprintf(fp,"{%5d,%5d},\t", pt[i].x, pt[i].y);
  201.         ++cols;
  202.         if ( (cols % 5) == 0 ) {
  203.             fprintf(fp,"\n\t");
  204.             cols = 0;
  205.         }
  206.     }
  207.     
  208.     if (cols != 0)
  209.         fprintf(fp,"\n");
  210.         
  211.     fprintf(fp,"};\n");
  212. }
  213.  
  214.  
  215.  
  216.  
  217. sort(v, n, sort_field)
  218.     struct xy_pt v[];
  219.     int n;
  220.     int sort_field;
  221. {
  222.     int gap, i, j;
  223.     
  224.     for (gap = n/2; gap > 0; gap /= 2)
  225.         for (i = gap; i < n; i++)
  226.             for (j = i-gap; j >= 0; j -= gap) {
  227.                 if (compare(v[j], v[j+gap], sort_field) <= 0)
  228.                     break;
  229.                 swap(&v[j], &v[j+gap]);
  230.             }
  231. }
  232.  
  233.  
  234.  
  235. int
  236. compare(v1, v2, sort_field)
  237.     struct    xy_pt v1,v2;
  238.     int        sort_field;
  239. {
  240.     int    temp1,temp2;    
  241.     
  242.     /* When sorting Y coordinates, don't swap unless X's are equal */
  243.     if ( (sort_field == Y_COORD) && (v1.x != v2.x) ) {
  244.         return(0);
  245.     }
  246.     
  247.     /* Extract the appropriate values to compare */
  248.     if (sort_field == X_COORD) {
  249.         temp1 = v1.x;
  250.         temp2 = v2.x;
  251.     }
  252.     else {
  253.         temp1 = v1.y;
  254.         temp2 = v2.y;
  255.     }
  256.     
  257.     /* Return the comparison of the two values */
  258.     if (temp1 < temp2)
  259.         return(-1);
  260.     else if (temp1 > temp2)
  261.         return(1);
  262.     else
  263.         return(0);
  264. }
  265.  
  266.  
  267.  
  268.  
  269. int
  270. swap(p1, p2)
  271.     struct xy_pt *p1, *p2;
  272. {
  273.     struct    xy_pt    temp;
  274.     
  275.     temp = *p1;
  276.     *p1 = *p2;
  277.     *p2 = temp;
  278. }
  279.  
  280.