-
Notifications
You must be signed in to change notification settings - Fork 1
/
2. Polygon Filling (Scan Line).cpp
207 lines (172 loc) · 3.8 KB
/
2. Polygon Filling (Scan Line).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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include<bits/stdc++.h>
#include<windows.h>
#include<graphics.h>
using namespace std;
struct PT
{
double x, y;
PT(){}
PT(double a, double b)
{
x = a;
y = b;
}
bool operator < (const PT &p)const
{
return x < p.x;
}
};
struct EDGE
{
double y_min, y_max,x_with_y_min,m_inv;
EDGE(){}
EDGE(double a, double b, double c, double d)
{
y_min = a;
y_max = b;
x_with_y_min = c;
m_inv = d;
}
bool operator < (const EDGE &p)const
{
return y_min < p.y_min;
}
};
const int WINDOW_W = 800, WINDOW_H = 600;
void drawAxis()
{
for(int i=0; i<WINDOW_H; i++) putpixel(WINDOW_W/2, i, WHITE);
for(int i=0; i<WINDOW_W; i++) putpixel(i, WINDOW_H/2, WHITE);
}
PT convertPixel(PT p)
{
p.x += WINDOW_W/2;
p.y = -p.y;
p.y += WINDOW_H/2;
return p;
}
void drawPixel(PT p, int color)
{
delay(0.1);
p = convertPixel(p);
putpixel((int)(p.x+0.5), (int)(p.y+0.5), color);
}
void drawLine(PT a, PT b,int col)
{
if(a.y == b.y)
{
if(a.x>b.x) swap(a,b);
for(int x = a.x; x <= b.x; x++)
{
drawPixel(PT(x, a.y), col);
}
}
///vertical
else if(a.x == b.x)
{
if(a.y > b.y) swap(a, b);
for(int y = a.y; y <= b.y; y++)
{
drawPixel(PT(a.x, y), col);
}
}
else
{
double m = (double)(b.y-a.y)/(double)(b.x-a.x);
double m_inv = 1.0/m;
if(fabs(m)<=1.0)
{
if(a.x>b.x) swap(a,b);
while(a.x<=b.x)
{
drawPixel(a,col);
a.x+=1;
a.y+=m;
}
}
else
{
if(a.y>b.y) swap(a,b);
while(a.y<=b.y)
{
drawPixel(a,col);
a.x += m_inv;
a.y+=1;
}
}
}
}
void drawPolygon(vector<PT> points,int col)
{
int n = points.size();
for(int i=0; i<n; i++)
{
drawLine(points[i], points[(i+1)%n],col);
}
}
void scanLine(vector<PT> points,int color)
{
vector<EDGE> edges;
int n=points.size();
double st = 1e15, en = -1e15;
unordered_map<double, int>mp;
for(int i=0;i<n;i++)
{
PT a = points[i];
PT b = points[(i+1)%n];
if(a.y==b.y) continue;
EDGE temp;
temp.y_min = min(a.y,b.y);
temp.y_max = max(a.y,b.y);
temp.x_with_y_min = (a.y<b.y)? a.x : b.x;
temp.m_inv = (b.x-a.x)/(b.y-a.y);
st = min(st, temp.y_min);
en = max(en, temp.y_max);
mp[temp.y_min] = 1;
edges.push_back(temp);
}
sort(edges.begin(),edges.end());
n = edges.size();
for(int i=0;i<n;i++)
{
if(mp[edges[i].y_max]) edges[i].y_max--;
}
for(double y = st; y<= en;y++)
{
vector<PT> int_points;
for(int i=0;i<n;i++)
{
if(y>= edges[i].y_min && y <= edges[i].y_max)
{
PT tmp;
tmp.x = edges[i].x_with_y_min;
tmp.y = y;
edges[i].x_with_y_min += edges[i].m_inv;
int_points.push_back(tmp);
}
}
sort(int_points.begin(),int_points.end());
for(int i=0;i<int_points.size()-1;i+=2)
{
drawLine(int_points[i],int_points[i+1],RED);
}
}
}
int main()
{
initwindow(WINDOW_W, WINDOW_H);
drawAxis();
vector<PT> poly;
poly.push_back(PT(-140, -40));
poly.push_back(PT(-60, 60));
poly.push_back(PT(0, 20));
poly.push_back(PT(-40, -60));
poly.push_back(PT(100, -80));
poly.push_back(PT(120, 40));
poly.push_back(PT(160, 100));
poly.push_back(PT(-160, 160));
drawPolygon(poly,YELLOW);
scanLine(poly,RED);
getchar();
return 0;
}