home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Current Shareware 1994 January
/
SHAR194.ISO
/
graphuti
/
polyops.zip
/
POLY_OUT.C
< prev
next >
Wrap
Text File
|
1991-03-18
|
7KB
|
235 lines
/* Output routines for poly_ops.c */
#include <stdio.h>
#include <graphics.h>
#include <string.h>
#define MAXPTS 100 /* maximum size of ANY polygon used, not just input */
#define MAXX 640 /* greater than any permissible x-coordinate */
#define MAXCOMPS 20 /* maximum number of output polygons allowed */
#define MAXCHOICE 12 /* menu choices */
#define ESC 27
#define PLUS 43
#define MINUS 45
#define INS -82
#define DEL -83
#define UP -72
#define DN -80
#define LEFT -75
#define RIGHT -77
#define HOME -71
#define END -79
#define PGUP -73
#define PGDN -81
#define SHFT_HOME 55
typedef struct {double x; double y;} point;
typedef struct {point first; point second;} segment;
typedef struct {int nverts; double x[MAXPTS]; double y[MAXPTS];} polygon;
typedef struct {point v; int class; int next;} ventry; /* for vertex rings */
typedef struct {point p1; point p2; int del;} fentry; /* for frags list */
/* function prototypes */
void
draw_poly(polygon,int),
graf_out(void),
listoutput(void),
setpage(int);
extern void
clearline(int x, int y),
gprintf(int, int, int, char *,...),
pause(void);
extern int normcolr, oper, vi[], xb, yb, sxmax, symax;
char *name[] = {"Intersection","Union ","A-B ", "B-A "};
extern char filename[], fname[];
extern polygon outpoly[MAXCOMPS]; /* program output */
extern int ptype[MAXCOMPS]; /* types of the output polygons */
extern int numcomps; /* number of output polygons */
extern polygon A,B; /* input polygons */
extern int ax1[MAXPTS],ay1[MAXPTS]; /* screen vertices of A */
extern int ax2[MAXPTS],ay2[MAXPTS]; /* screen vertices of B */
extern int nv1,nv2; /* sizes of A and B */
extern int xx,yy; /* query location, menu */
/* ----------------------- draw polygon pgn in color c --------------------- */
void
draw_poly(polygon pgn, int c)
{
int i,n = pgn.nverts;
setcolor(c);
for (i=0; i<n-1; i++)
line(sx(pgn.x[i]),sy(pgn.y[i]),sx(pgn.x[i+1]),sy(pgn.y[i+1]));
line(sx(pgn.x[n-1]),sy(pgn.y[n-1]),sx(pgn.x[0]),sy(pgn.y[0]));
setcolor(normcolr);
}
/* ----------------- graphics output of polygons ------------------------- */
/* graf_out() draws the current result polygons on page 1. Output 'islands'
are yellow, 'holes' are blue. Provision is made for toggling between the
output and the original polygons. */
void
graf_out(void)
{
int i,n,m;
int colour[] = {14,9}; /* colour[0] for islands, colour[1] for holes */
int output;
setpage(1); /* draw polygons on page 1 */
setfillstyle(1,7);
setcolor(15);
bar3d(0,yb,sxmax,symax,0,0);
m = textwidth(fname)+16;
n = m+textwidth(name[oper])+24;
gprintf(xb,yb+3,1,"%s",strupr(fname));
gprintf(n,yb+3,1,"Enter toggles graphs, any other key returns");
a:
output = 0;
setviewport(0,0,sxmax,yb-1,1);
clearviewport();
setviewport(0,0,sxmax,symax,1);
for (i=0; i<numcomps; i++)
draw_poly(outpoly[i],colour[ptype[i]]); /* ptype = 0,1 for isles,holes */
bar(m,yb+3,n-1,symax-1); /* clear part of message */
gprintf(m,yb+3,1,"%s",name[oper]);
while ((i=getch()) == 13)
if (output)
goto a; /* draw output polygons */
else /* draw original polygons */
{
setviewport(0,0,sxmax,yb-1,1);
clearviewport();
setviewport(0,0,sxmax,symax,1);
output=1;
draw_poly(A,10);
draw_poly(B,12);
bar(m,yb+3,n-1,symax-1); /* clear part of message */
gprintf(m,yb+3,1,"Original ");
gprintf(n,yb+3,1,"Enter toggles graphs, any other key returns");
}
cleardevice();
setfillstyle(0,0);
setcolor(normcolr);
setpage(0); /* restore page 0 before return */
}
/* ----------------- text listing of output polygons ----------------------- */
void
listoutput(void)
{
int i,j,row;
printf(strupr(filename));
printf(" %s : ",name[oper]);
if (numcomps==1) printf("One output polygon.\n");
else printf("%d output polygons.\n",numcomps);
for (i=0; i<numcomps; i++)
{
printf("\nP%d :",i);
if (ptype[i] == 0)
printf(" island :");
else
printf(" hole :");
printf(" %d vertices\n",outpoly[i].nverts);
for (j=0; j<outpoly[i].nverts; j++)
{
row = wherey();
if (row > 23)
pause();
printf("(%.1f,%.1f) ",outpoly[i].x[j],outpoly[i].y[j]);
}
}
printf("\n Press any key to continue");
getch();
}
/* ---------------------------------------------------------------------- */
/* screen full - wait for keypress to continue */
void
pause(void)
{
gotoxy(wherey()+1,70);
cputs("More...");
getch();
clrscr();
}
/* --------------------- write output to disk file ----------------------- */
int
save_output(char *s)
{
FILE *fp;
int i,j;
if ((fp = fopen(s,"wt")) == NULL)
return 0;
fprintf(fp,strupr(filename));
fprintf(fp," %s : ",name[oper]);
if (numcomps==1) fprintf(fp,"One output polygon found:\n");
else fprintf(fp,"%d output polygons found.\n",numcomps);
for (i=0; i<numcomps; i++)
{
fprintf(fp,"\nP%d :",i);
if (ptype[i] == 0)
fprintf(fp," island :");
else
fprintf(fp," hole :");
fprintf(fp," %d vertices\n",outpoly[i].nverts);
for (j=0; j<outpoly[i].nverts; j++)
fprintf(fp,"(%.4f,%.4f) ",outpoly[i].x[j],outpoly[i].y[j]);
}
fclose(fp);
return 1;
}
/* --------------- write polygon data to disk file ------------------- */
int
save_data(char *s)
{
FILE *fp;
int i;
if ((fp = fopen(s,"wt")) == NULL)
return 0;
fprintf(fp,"%d\n",nv1);
for (i=0; i<nv1; i++)
fprintf(fp,"%.4f ",A.x[i]);
fprintf(fp,"\n");
for (i=0; i<nv1; i++)
fprintf(fp,"%.4f ",A.y[i]);
fprintf(fp,"\n");
fprintf(fp,"%d\n",nv2);
for (i=0; i<nv2; i++)
fprintf(fp,"%.4f ",B.x[i]);
fprintf(fp,"\n");
for (i=0; i<nv2; i++)
fprintf(fp,"%.4f ",B.y[i]);
fprintf(fp,"\n");
fclose(fp);
return 1;
}
/* ------------------------------------------------------------------------- */
void
setpage(int page)
{
setactivepage(page);
setvisualpage(page);
}