aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/plot/libplot/fill.c
blob: a33294e50a991ac26519c727710835d33627b324 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * fill -- polygon tiler
 * Updating the edgelist from scanline to scanline could be quicker if no
 * edges cross:  we can just merge the incoming edges.  If the scan-line
 * filling routine were a parameter, we could do textured
 * polygons, polyblt, and other such stuff.
 */
#include "mplot.h"
typedef enum{
	Odd=1,
	Nonzero=~0
}Windrule;
typedef struct edge Edge;
struct edge{
	Point p;	/* point of crossing current scan-line */
	int maxy;	/* scan line at which to discard edge */
	int dx;		/* x increment if x fraction<1 */
	int dx1;	/* x increment if x fraction>=1 */
	int x;		/* x fraction, scaled by den */
	int num;	/* x fraction increment for unit y change, scaled by den */
	int den;	/* x fraction increment for unit x change, scaled by num */
	int dwind;	/* increment of winding number on passing this edge */
	Edge *next;	/* next edge on current scanline */
	Edge *prev;	/* previous edge on current scanline */
};
static void insert(Edge *ep, Edge **yp){
	while(*yp && (*yp)->p.x<ep->p.x) yp=&(*yp)->next;
	ep->next=*yp;
	*yp=ep;
	if(ep->next){
		ep->prev=ep->next->prev;
		ep->next->prev=ep;
		if(ep->prev)
			ep->prev->next=ep;
	}
	else
		ep->prev=0;
}
static void polygon(int cnt[], double *pts[], Windrule w, int v){
	Edge *edges, *ep, *nextep, **ylist, **eylist, **yp;
	Point p, q, p0, p1, p10;
	int i, dy, nbig, y, left, right, wind, nwind, nvert;
	int *cntp;
	double **ptsp, *xp;
	nvert=0;
	for(cntp=cnt;*cntp;cntp++) nvert+=*cntp;
	edges=(Edge *)malloc(nvert*sizeof(Edge));
	if(edges==0){
	NoSpace:
		fprintf(stderr, "polygon: no space\n");
		exits("malloc failed");
	}
	ylist=(Edge **)malloc(Dy(screen->r)*sizeof(Edge *));
	if(ylist==0) goto NoSpace;
	eylist=ylist+Dy(screen->r);
	for(yp=ylist;yp!=eylist;yp++) *yp=0;
	ep=edges;
	for(cntp=cnt,ptsp=pts;*cntp;cntp++,ptsp++){
		p.x=SCX((*ptsp)[*cntp*2-2]);
		p.y=SCY((*ptsp)[*cntp*2-1]);
		nvert=*cntp;
		for(xp=*ptsp,i=0;i!=nvert;xp+=2,i++){
			q=p;
			p.x=SCX(xp[0]);
			p.y=SCY(xp[1]);
			if(p.y==q.y) continue;
			if(p.y<q.y){
				p0=p;
				p1=q;
				ep->dwind=1;
			}
			else{
				p0=q;
				p1=p;
				ep->dwind=-1;
			}
			if(p1.y<=screen->r.min.y) continue;
			if(p0.y>=screen->r.max.y) continue;
			ep->p=p0;
			if(p1.y>screen->r.max.y)
				ep->maxy=screen->r.max.y;
			else
				ep->maxy=p1.y;
			p10=subpt(p1, p0);
			if(p10.x>=0){
				ep->dx=p10.x/p10.y;
				ep->dx1=ep->dx+1;
			}
			else{
				p10.x=-p10.x;
				ep->dx=-(p10.x/p10.y); /* this nonsense rounds toward zero */
				ep->dx1=ep->dx-1;
			}
			ep->x=0;
			ep->num=p10.x%p10.y;
			ep->den=p10.y;
			if(ep->p.y<screen->r.min.y){
				dy=screen->r.min.y-ep->p.y;
				ep->x+=dy*ep->num;
				nbig=ep->x/ep->den;
				ep->p.x+=ep->dx1*nbig+ep->dx*(dy-nbig);
				ep->x%=ep->den;
				ep->p.y=screen->r.min.y;
			}
			insert(ep, ylist+(ep->p.y-screen->r.min.y));
			ep++;
		}
	}
	left = 0;
	for(yp=ylist,y=screen->r.min.y;yp!=eylist;yp++,y++){
		wind=0;
		for(ep=*yp;ep;ep=nextep){
			nwind=wind+ep->dwind;
			if(nwind&w){	/* inside */
				if(!(wind&w)){
					left=ep->p.x;
					if(left<screen->r.min.x) left=screen->r.min.x;
				}
			}
			else if(wind&w){
				right=ep->p.x;
				if(right>=screen->r.max.x) right=screen->r.max.x;
#define BART_BUG_FIXED	/* what goes on here?? -rob */
#ifdef BART_BUG_FIXED
				if(right>left)
					line(screen, Pt(left, y), Pt(right, y), Endsquare, Endsquare, 0, getcolor(v), ZP);
#else
				if(right>left){
					switch(v){
					default:
						segment(&screen, Pt(left, y), Pt(right, y),
							~0, D&~S);
						segment(&screen, Pt(left, y), Pt(right, y),
							v, f);
						break;
					case 0:
						segment(&screen, Pt(left, y), Pt(right, y),
							~0, D&~S);
						break;
					case 3:
						segment(&screen, Pt(left, y), Pt(right, y),
							v, f);
						break;
					}
				}
#endif
			}
			wind=nwind;
			nextep=ep->next;
			if(++ep->p.y!=ep->maxy){
				ep->x+=ep->num;
				if(ep->x>=ep->den){
					ep->x-=ep->den;
					ep->p.x+=ep->dx1;
				}
				else
					ep->p.x+=ep->dx;
				insert(ep, yp+1);
			}
		}
	}
	free((char *)edges);
	free((char *)ylist);
}
void fill(int num[], double *ff[]){
	polygon(num, ff, Odd, e1->foregr);
}