-
Notifications
You must be signed in to change notification settings - Fork 1
/
polyfill.cpp
136 lines (112 loc) · 2.43 KB
/
polyfill.cpp
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
#include <math.h>
#include "GL/glut.h"
#include <assert.h>
#include "polyfill.h"
#include "Point.h"
void ScanX(Point&l,Point & r, float y,void (*drawPoint)(GLint x,GLint y))
{
int lx = ceil(l.x), rx = ceil(r.x);
for(int x= lx;x < rx; x++)
drawPoint(x,y);
}
void DiffY(Point&b, Point&t,Point&m,Point&dy,int y)
{
dy = (t-b)/(t.y-b.y);
m = b+dy*(y-b.y);
}
int mod(int x,int y)
{
if (x < 0)
return mod(x+y,y);
else
return x%y;
// or: MOD(x,y) is x - y.*floor(x./y) if y ~= 0. By convention, MOD(x,0) is x.
}
void ScanY(Point v[], int nv, int bot,void (*drawPoint)(GLint x,GLint y))
{
// v: array of vertecies in counterclockwise order
// nv: number of vertecies
// bot: number of the Point with the min y coordinate
int li = bot, ri = li;
int y = ceil(v[bot].y), ly = y, ry = y;
Point l, dl, r, dr;
for(int rem = nv; rem > 0; )
{
// find left boundary edge of tile
while (ly <= y && rem -- > 0)
{
int i = li;
li = mod(li-1,nv);
assert(li >= 0);
ly = ceil(v[li].y);
if (ly > y)
DiffY(v[i],v[li],l,dl,y);
}
// find right boundary edge of atile
while (ry <= y && rem -- > 0)
{
int i= ri;
ri = mod(ri+1,nv);
ry = ceil(v[ri].y);
if (ry> y)
DiffY(v[i],v[ri],r,dr,y);
}
for(; y < ly && y < ry; y++)
{
ScanX(l,r,y,drawPoint);
l+=dl;
r+= dr;
}
}
}
void drawPoly(Point v[],int nv)
{
assert(ccw(v[0],v[1],v[2]));
int bot = 0;
float miny = v[bot].y;
for(int i=1;i<nv;i++)
if (v[i].y < miny)
{
miny = v[i].y;
bot = i;
}
// printf("POLYGON\n");
// printf("v[%d] = (%f,%f)\n",bot,v[bot].x,v[bot].y);
// for(i=(bot+1)%nv;i!=bot;i=(i+1)%nv)
// printf("v[%d] = (%f,%f)\n",i,v[i].x,v[i].y);
// printf("det = %f\n",det2(v[1]-v[0],v[2]-v[0]));
glPointSize(1);
glBegin(GL_POINTS);
ScanY(v,nv,bot,glVertex2i);
glEnd();
}
void drawTriangle(Point v1,Point v2,Point v3)
{
if(ccw(v1,v2,v3))
{
// assert(ccw(v1,v2,v3));
// printf("noflip\n");
Point v[] = {v1,v2,v3};
drawPoly(v,3);
}
else
{
// assert(ccw(v1,v3,v2));
// printf("flip\n");
Point v[] = {v1,v3,v2};
drawPoly(v,3);
}
}
void drawQuad(Point v1,Point v2,Point v3,Point v4)
{
if (ccw(v1,v2,v3))
{
Point v[] = {v1,v2,v3,v4};
drawPoly(v,4);
}
else
{
Point v[] = {v4,v3,v2,v1};
drawPoly(v,4);
}
}