-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolygon.cpp
executable file
·368 lines (331 loc) · 7.78 KB
/
polygon.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "polygon.h"
#include <cmath>
#include <stdio.h>
#include <algorithm>
Polygon::Polygon()
{
}
//slope return type:
//0: point
//1: vertical
//2: horizal
//3,4,5,6: normal slope exits
void Polygon::updateFillPoint(int dx, int dy)
{
QList<QPoint> list;
foreach(QPoint p, colorPoint)
{
QPoint temp;
temp.setX(p.x()+dx*8);
temp.setY(p.y()+dy*8);
list.append(temp);
colorPoint.removeFirst();
}
colorPoint.clear();
foreach(QPoint p, list)
{
QPoint temp = p;
colorPoint.append(temp);
}
}
bool operate(const Node &n1, const Node &n2)
{
/* if(n1.x==n2.x)
return n1.slop_t<n2.slop_t;
else*/
return n1.x<n2.x;
}
void Polygon::fillShape(QPainter &painter)
{
if(colorPoint.length()!=0)
return;
//计算最高y值
int maxy = 0;
int miny = 500;
for(int i =0; i<pointList.length();i++)
{
if(pointList[i].y() > maxy)
maxy = pointList[i].y();
if(pointList[i].y() < miny)
miny = pointList[i].y();
}
for(int i=0;i<1000;i++)
{
net[i].clear();
}
aet.clear();
//建立NET有序边表
int len = pointList.length();
for(int i = 0; i < len;i++)
{
// QPoint a0 = pointList[(i-1+len)%len];
QPoint a = pointList[i];
QPoint b = pointList[(i+1)%len];
// QPoint b0 = pointList[(i+2)%len];
if(a.y()==b.y())
continue;
//一条边的上下端y值
int ymin = a.y()<b.y()?a.y():b.y();
int ymax = a.y()>b.y()?a.y():b.y();
float x = a.y()>b.y()?b.x():a.x();
float dx = (a.x()-b.x())*1.0/(a.y()-b.y());
/* if((a.y()<b.y() && a.y()>a0.y()) ||
(b.y()<a.y() && b.y()>b0.y()) )
{
ymin++;
x+=dx;
}*/
//创建边,加入有序边表
Node t;
t.ymax = ymax;
t.x = x;
t.slop_t = dx;
net[ymin].append(t);
}
//扫描线
for(int i = 0; i < maxy; i++)
{
// if(net[i].length()!=0)
// printf("start point edges num %d\n", net[i].length());
for(int j = 0; j < net[i].length();j++)
{
Node t = net[i][j];
aet.append(t);
}
std::sort(aet.begin(),aet.end(), operate);
//pop ymax==i
for(int j = 0; j < aet.length();j++)
{
if(aet[j].ymax == i)
{
aet.removeAt(j);
//BUG,删除之后后面的整体往前移动,如果j不减一,会丢失对一个节点的检查
j=j-1;
}
}
// printf("cross node num %d\n", aet.length());
//draw
int k = 0;
while(k<aet.length())
{
int x1 = aet[k].x;
int x2;
if(k+1<aet.length())
x2 = aet[k+1].x;
else
break;
for(int ll = x1+1;ll<=x2;ll+=1)
{
painter.drawPoint(ll,i);
QPoint temp;
temp.setX(ll);
temp.setY(i);
colorPoint.append(temp);
}
k+=2;
}
for(int j = 0; j < aet.length();j++)
{
aet[j].x+=aet[j].slop_t;
}
}
}
int Polygon::slope(QPoint a, QPoint b)
{
int x1 = a.x();
int y1 = a.y();
int x2 = b.x();
int y2 = b.y();
int dx = x2-x1;
int dy = y2-y1;
if(x1==x2 && y1==y2)
return 0;
else if(x1==x2)
return 1;
else if(y1==y2)
return 2;
else
{
double s = (double)dy/(double)dx;
if(s>0 && s<=1)
return 3;
else if(s>=-1 && s<0)
return 4;
else if(s>1)
return 5;
else
return 6;
}
}
void Polygon::draw(QPainter &painter, QPoint s, QPoint e)
{
int xa = s.x();
int ya = s.y();
int xb = e.x();
int yb = e.y();
int dx = abs(xa-xb);
int dy = abs(ya-yb);
int slope_type = slope(s,e);
switch(slope_type)
{
case 0:
{
painter.drawPoint(xa,ya);
break;
}
case 1:
{
int y1 = ya<yb?ya:yb;
int y2 = ya>yb?ya:yb;
for (int k=y1;k<=y2;k++)
painter.drawPoint(xa,k);
break;
}
case 2:
{
int x1 = xa<xb?xa:xb;
int x2 = xa>xb?xa:xb;
for (int k=x1;k<=x2;k++)
painter.drawPoint(k,ya);
break;
}
case 3:
{
int x1 = xa<xb?xa:xb;
int y1 = ya<yb?ya:yb;
int p0 = 2*dy-dx;
painter.drawPoint(x1,y1);
int xn = x1;
int yn = y1;
for (int i=1;i<=dx;i++)
{
if(p0<0)
{
xn+=1;
yn=yn;
painter.drawPoint(xn,yn);
p0 +=2*dy;
}
else
{
xn+=1;
yn+=1;
painter.drawPoint(xn,yn);
p0+=2*dy-2*dx;
}
}
break;
}
case 4:
{
int x1=xa<xb?xa:xb;
int y1=ya>yb?ya:yb;
int p0 = 2*dy-dx;
painter.drawPoint(x1,y1);
int xn = x1;
int yn = y1;
for (int i=1;i<=dx;i++)
{
if(p0<0)
{
xn+=1;
yn=yn;
painter.drawPoint(xn,yn);
p0 +=2*dy;
}
else
{
xn+=1;
yn-=1;
painter.drawPoint(xn,yn);
p0+=2*dy-2*dx;
}
}
break;
}
case 5:
{
int x1 = xa<xb?xa:xb;
int y1 = ya<yb?ya:yb;
int p0 = 2*dx-dy;
painter.drawPoint(x1,y1);
int xn = x1;
int yn = y1;
for (int i=1;i<=dy;i++)
{
if(p0<0)
{
yn+=1;
xn=xn;
painter.drawPoint(xn,yn);
p0 +=2*dx;
}
else
{
yn+=1;
xn+=1;
painter.drawPoint(xn,yn);
p0+=2*dx-2*dy;
}
}
break;
}
case 6:
{
int x1=xa<xb?xa:xb;
int y1=ya>yb?ya:yb;
int p0 = 2*dx-dy;
painter.drawPoint(x1,y1);
int xn = x1;
int yn = y1;
for (int i=1;i<=dy;i++)
{
if(p0<0)
{
yn-=1;
xn=xn;
painter.drawPoint(xn,yn);
p0 +=2*dx;
}
else
{
yn-=1;
xn+=1;
painter.drawPoint(xn,yn);
p0+=2*dx-2*dy;
}
}
break;
}
}
}
void Polygon::paint(QPainter &painter)
{
painter.setPen(Qt::cyan);
foreach (QPoint p, pointList) {
painter.drawRect(p.x()-2, p.y()-2, 4, 4);
}
int n = 0;
if ((n=pointList.length())<2)
return;
int len = pointList.length();
if(len < 3)
{
return;
}
//将边的信息提取出来
QPoint s = pointList[0];
QPoint e = pointList[1];
draw(painter, s, e);
for(int i = 2; i < len; i++)
{
s = e; e = pointList[i];
draw(painter, s, e);
}
s = e; e = pointList[0];
draw(painter, s, e);
foreach(QPoint p, colorPoint)
{
painter.drawPoint(p.x(),p.y());
}
}