home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / graphuti / polyops.zip / POLY_OUT.C < prev    next >
Text File  |  1991-03-18  |  7KB  |  235 lines

  1.  
  2. /* Output routines for poly_ops.c */
  3.  
  4. #include <stdio.h>
  5. #include <graphics.h>
  6. #include <string.h>
  7.  
  8. #define MAXPTS   100    /* maximum size of ANY polygon used, not just input */
  9. #define MAXX     640    /* greater than any permissible x-coordinate */
  10. #define MAXCOMPS  20    /* maximum number of output polygons allowed */
  11. #define MAXCHOICE 12    /* menu choices */
  12.  
  13. #define ESC       27
  14. #define PLUS      43
  15. #define MINUS     45
  16. #define INS      -82
  17. #define DEL      -83
  18. #define UP       -72
  19. #define DN       -80
  20. #define LEFT     -75
  21. #define RIGHT    -77
  22. #define HOME     -71
  23. #define END      -79
  24. #define PGUP     -73
  25. #define PGDN     -81
  26. #define SHFT_HOME 55
  27.  
  28. typedef struct {double x; double y;} point;
  29. typedef struct {point first; point second;} segment;
  30. typedef struct {int nverts; double x[MAXPTS]; double y[MAXPTS];} polygon;
  31. typedef struct {point v; int class; int next;} ventry; /* for vertex rings */
  32. typedef struct {point p1; point p2; int del;} fentry;  /* for frags list */
  33.  
  34. /* function prototypes */
  35. void
  36. draw_poly(polygon,int),
  37. graf_out(void),
  38. listoutput(void),
  39. setpage(int);
  40.  
  41. extern void
  42.   clearline(int x, int y),
  43.   gprintf(int, int, int, char *,...),
  44.   pause(void);
  45.  
  46. extern int normcolr, oper, vi[], xb, yb, sxmax, symax;
  47.  
  48. char *name[] = {"Intersection","Union       ","A-B         ", "B-A         "};
  49. extern char filename[], fname[];
  50.  
  51. extern polygon outpoly[MAXCOMPS];         /* program output */
  52. extern int ptype[MAXCOMPS];               /* types of the output polygons */
  53. extern int numcomps;                      /* number of output polygons */
  54.  
  55. extern polygon A,B;                       /* input polygons */
  56.  
  57. extern int ax1[MAXPTS],ay1[MAXPTS];        /* screen vertices of A */
  58. extern int ax2[MAXPTS],ay2[MAXPTS];        /* screen vertices of B */
  59. extern int nv1,nv2;                        /* sizes of A and B */
  60. extern int xx,yy;                          /* query location, menu */
  61.  
  62. /* ----------------------- draw polygon pgn in color c --------------------- */
  63. void
  64. draw_poly(polygon pgn, int c)
  65. {
  66.    int i,n = pgn.nverts;
  67.  
  68.    setcolor(c);
  69.    for (i=0; i<n-1; i++)
  70.      line(sx(pgn.x[i]),sy(pgn.y[i]),sx(pgn.x[i+1]),sy(pgn.y[i+1]));
  71.    line(sx(pgn.x[n-1]),sy(pgn.y[n-1]),sx(pgn.x[0]),sy(pgn.y[0]));
  72.    setcolor(normcolr);
  73. }
  74.  
  75. /* ----------------- graphics output of polygons ------------------------- */
  76. /* graf_out() draws the current result polygons on page 1. Output 'islands'
  77.    are yellow, 'holes' are blue. Provision is made for toggling between the
  78.    output and the original polygons. */
  79.  
  80. void
  81. graf_out(void)
  82. {
  83.    int i,n,m;
  84.    int colour[] = {14,9};      /* colour[0] for islands, colour[1] for holes */
  85.    int output;
  86.  
  87.    setpage(1);                       /* draw polygons on page 1 */
  88.    setfillstyle(1,7);
  89.    setcolor(15);
  90.    bar3d(0,yb,sxmax,symax,0,0);
  91.    m = textwidth(fname)+16;
  92.    n = m+textwidth(name[oper])+24;
  93.    gprintf(xb,yb+3,1,"%s",strupr(fname));
  94.    gprintf(n,yb+3,1,"Enter toggles graphs, any other key returns");
  95. a:
  96.    output = 0;
  97.    setviewport(0,0,sxmax,yb-1,1);
  98.    clearviewport();
  99.    setviewport(0,0,sxmax,symax,1);
  100.    for (i=0; i<numcomps; i++)
  101.      draw_poly(outpoly[i],colour[ptype[i]]); /* ptype = 0,1 for isles,holes */
  102.    bar(m,yb+3,n-1,symax-1);        /* clear part of message */
  103.    gprintf(m,yb+3,1,"%s",name[oper]);
  104.  
  105.    while ((i=getch()) == 13)
  106.    if (output)
  107.       goto a;                           /* draw output polygons */
  108.    else                                 /* draw original polygons */
  109.      {
  110.       setviewport(0,0,sxmax,yb-1,1);
  111.       clearviewport();
  112.       setviewport(0,0,sxmax,symax,1);
  113.       output=1;
  114.       draw_poly(A,10);
  115.       draw_poly(B,12);
  116.       bar(m,yb+3,n-1,symax-1);        /* clear part of message */
  117.       gprintf(m,yb+3,1,"Original     ");
  118.       gprintf(n,yb+3,1,"Enter toggles graphs, any other key returns");
  119.      }
  120.    cleardevice();
  121.    setfillstyle(0,0);
  122.    setcolor(normcolr);
  123.    setpage(0);                      /* restore page 0 before return */
  124. }
  125.  
  126. /* ----------------- text listing of output polygons ----------------------- */
  127. void
  128. listoutput(void)
  129. {
  130.    int i,j,row;
  131.  
  132.    printf(strupr(filename));
  133.    printf("  %s : ",name[oper]);
  134.    if (numcomps==1) printf("One output polygon.\n");
  135.    else printf("%d output polygons.\n",numcomps);
  136.  
  137.    for (i=0; i<numcomps; i++)
  138.      {
  139.       printf("\nP%d :",i);
  140.       if (ptype[i] == 0)
  141.          printf(" island :");
  142.       else
  143.          printf(" hole :");
  144.       printf(" %d vertices\n",outpoly[i].nverts);
  145.       for (j=0; j<outpoly[i].nverts; j++)
  146.         {
  147.          row = wherey();
  148.          if (row > 23)
  149.             pause();
  150.          printf("(%.1f,%.1f) ",outpoly[i].x[j],outpoly[i].y[j]);
  151.         }
  152.      }
  153.    printf("\n Press any key to continue");
  154.    getch();
  155. }
  156.  
  157. /* ---------------------------------------------------------------------- */
  158. /* screen full - wait for keypress to continue */
  159. void
  160. pause(void)
  161. {
  162.    gotoxy(wherey()+1,70);
  163.    cputs("More...");
  164.    getch();
  165.    clrscr();
  166. }
  167.  
  168.  
  169. /* --------------------- write output to disk file ----------------------- */
  170. int
  171. save_output(char *s)
  172. {
  173.    FILE *fp;
  174.    int i,j;
  175.  
  176.    if ((fp = fopen(s,"wt")) == NULL)
  177.       return 0;
  178.  
  179.  
  180.    fprintf(fp,strupr(filename));
  181.    fprintf(fp," %s : ",name[oper]);
  182.    if (numcomps==1) fprintf(fp,"One output polygon found:\n");
  183.    else fprintf(fp,"%d output polygons found.\n",numcomps);
  184.  
  185.    for (i=0; i<numcomps; i++)
  186.      {
  187.       fprintf(fp,"\nP%d :",i);
  188.       if (ptype[i] == 0)
  189.          fprintf(fp," island :");
  190.       else
  191.          fprintf(fp," hole :");
  192.       fprintf(fp," %d vertices\n",outpoly[i].nverts);
  193.       for (j=0; j<outpoly[i].nverts; j++)
  194.          fprintf(fp,"(%.4f,%.4f) ",outpoly[i].x[j],outpoly[i].y[j]);
  195.      }
  196.     fclose(fp);
  197.     return 1;
  198. }
  199.  
  200. /* --------------- write polygon data to disk file ------------------- */
  201. int
  202. save_data(char *s)
  203. {
  204.    FILE *fp;
  205.    int i;
  206.  
  207.    if ((fp = fopen(s,"wt")) == NULL)
  208.       return 0;
  209.    fprintf(fp,"%d\n",nv1);
  210.    for (i=0; i<nv1; i++)
  211.       fprintf(fp,"%.4f ",A.x[i]);
  212.    fprintf(fp,"\n");
  213.    for (i=0; i<nv1; i++)
  214.       fprintf(fp,"%.4f ",A.y[i]);
  215.    fprintf(fp,"\n");
  216.    fprintf(fp,"%d\n",nv2);
  217.    for (i=0; i<nv2; i++)
  218.       fprintf(fp,"%.4f ",B.x[i]);
  219.    fprintf(fp,"\n");
  220.    for (i=0; i<nv2; i++)
  221.       fprintf(fp,"%.4f ",B.y[i]);
  222.    fprintf(fp,"\n");
  223.    fclose(fp);
  224.    return 1;
  225. }
  226.  
  227. /* ------------------------------------------------------------------------- */
  228. void
  229. setpage(int page)
  230. {
  231.    setactivepage(page);
  232.    setvisualpage(page);
  233. }
  234.  
  235.