2 * fill -- polygon tiler
3 * Updating the edgelist from scanline to scanline could be quicker if no
4 * edges cross: we can just merge the incoming edges. If the scan-line
5 * filling routine were a parameter, we could do textured
6 * polygons, polyblt, and other such stuff.
13 typedef struct edge Edge;
15 Point p; /* point of crossing current scan-line */
16 int maxy; /* scan line at which to discard edge */
17 int dx; /* x increment if x fraction<1 */
18 int dx1; /* x increment if x fraction>=1 */
19 int x; /* x fraction, scaled by den */
20 int num; /* x fraction increment for unit y change, scaled by den */
21 int den; /* x fraction increment for unit x change, scaled by num */
22 int dwind; /* increment of winding number on passing this edge */
23 Edge *next; /* next edge on current scanline */
24 Edge *prev; /* previous edge on current scanline */
26 static void insert(Edge *ep, Edge **yp){
27 while(*yp && (*yp)->p.x<ep->p.x) yp=&(*yp)->next;
31 ep->prev=ep->next->prev;
39 static void polygon(int cnt[], double *pts[], Windrule w, int v){
40 Edge *edges, *ep, *nextep, **ylist, **eylist, **yp;
41 Point p, q, p0, p1, p10;
42 int i, dy, nbig, y, left, right, wind, nwind, nvert;
46 for(cntp=cnt;*cntp;cntp++) nvert+=*cntp;
47 edges=(Edge *)malloc(nvert*sizeof(Edge));
50 sysfatal("polygon: no space");
52 ylist=(Edge **)malloc(Dy(screen->r)*sizeof(Edge *));
53 if(ylist==0) goto NoSpace;
54 eylist=ylist+Dy(screen->r);
55 for(yp=ylist;yp!=eylist;yp++) *yp=0;
57 for(cntp=cnt,ptsp=pts;*cntp;cntp++,ptsp++){
58 p.x=SCX((*ptsp)[*cntp*2-2]);
59 p.y=SCY((*ptsp)[*cntp*2-1]);
61 for(xp=*ptsp,i=0;i!=nvert;xp+=2,i++){
65 if(p.y==q.y) continue;
76 if(p1.y<=screen->r.min.y) continue;
77 if(p0.y>=screen->r.max.y) continue;
79 if(p1.y>screen->r.max.y)
80 ep->maxy=screen->r.max.y;
90 ep->dx=-(p10.x/p10.y); /* this nonsense rounds toward zero */
96 if(ep->p.y<screen->r.min.y){
97 dy=screen->r.min.y-ep->p.y;
100 ep->p.x+=ep->dx1*nbig+ep->dx*(dy-nbig);
102 ep->p.y=screen->r.min.y;
104 insert(ep, ylist+(ep->p.y-screen->r.min.y));
109 for(yp=ylist,y=screen->r.min.y;yp!=eylist;yp++,y++){
111 for(ep=*yp;ep;ep=nextep){
112 nwind=wind+ep->dwind;
113 if(nwind&w){ /* inside */
116 if(left<screen->r.min.x) left=screen->r.min.x;
121 if(right>=screen->r.max.x) right=screen->r.max.x;
122 #define BART_BUG_FIXED /* what goes on here?? -rob */
123 #ifdef BART_BUG_FIXED
125 line(screen, Pt(left, y), Pt(right, y), Endsquare, Endsquare, 0, getcolor(v), ZP);
130 segment(&screen, Pt(left, y), Pt(right, y),
132 segment(&screen, Pt(left, y), Pt(right, y),
136 segment(&screen, Pt(left, y), Pt(right, y),
140 segment(&screen, Pt(left, y), Pt(right, y),
149 if(++ep->p.y!=ep->maxy){
164 void fill(int num[], double *ff[]){
165 polygon(num, ff, Odd, e1->foregr);