-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClipping.h
281 lines (237 loc) · 5.75 KB
/
Clipping.h
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
#pragma once
#include <vector>
using namespace std;
void swap(double& x1, double& y1, double& x2, double& y2)
{
int tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
}
struct Vertex
{
double x, y;
Vertex(int x1 = 0, int y1 = 0)
{
x = x1;
y = y1;
}
};
typedef vector<Vertex> VertexList;
typedef bool (*IsInFunc)(Vertex& v, int edge);
typedef Vertex(*IntersectFunc)(Vertex& v1, Vertex& v2, int edge);
//Circle Clipping
void PointClipping(HDC hdc, double x, double y, double XC, double YC, double R)
{
OutputDebugStringA("Circle Point Clipping");
COLORREF outColor = RGB(255, 255, 255);
COLORREF inColor = RGB(0, 0, 255);
int newR = sqrt((x - XC) * (x - XC) + (y - YC) * (y - YC));
if (newR < R)
SetPixel(hdc, x, y, inColor);
}
void lineClipping(HDC hdc, double x1, double y1, double x2, double y2, double XC, double YC, double R, COLORREF c)
{
int dx = x2 - x1;
int dy = y2 - y1;
OutputDebugStringA("Circle Line Clipping");
if (abs(dy) <= abs(dx))
{
if (x1 > x2)
swap(x1, y1, x2, y2);
int x = x1;
int y = y1;
dx = x2 - x1;
dy = y2 - y1;
if (dy > 0)
{
while (x < x2)
{
double d = (y + 0.5 - y1) * dx - (x + 1.0 - x1) * dy;
if (d < 0)
y++;
x++;
PointClipping(hdc, x, y, XC, YC, R);
}
}
else
{
while (x < x2)
{
double d = (y - 0.5 - y1) * dx - (x + 1.0 - x1) * dy;
if (d > 0)
y--;
x++;
PointClipping(hdc, x, y, XC, YC, R);
}
}
}
else
{
if (y1 > y2)
swap(x1, y1, x2, y2);
int x = x1;
int y = y1;
dx = x2 - x1;
dy = y2 - y1;
if (dx > 0)
{
while (y < y2)
{
double d = (y + 1.0 - y1) * dx - (x + 0.5 - x1) * dy;
if (d > 0)
x++;
y++;
PointClipping(hdc, x, y, XC, YC, R);
}
}
else
{
while (y < y2)
{
double d = (y + 1.0 - y1) * dx - (x - 0.5 - x1) * dy;
if (d < 0)
x--;
y++;
PointClipping(hdc, x, y, XC, YC, R);
}
}
}
}
//Square-Rectangle Clipping
void PointClipping(HDC hdc, int x, int y, int xleft, int ytop, int xright, int ybottom, COLORREF color)
{
OutputDebugStringA("Square Point Clipping");
if (x >= xleft && x <= xright && y >= ytop && y <= ybottom)
SetPixel(hdc, x, y, color);
}
union OutCode
{
unsigned All : 4;
struct { unsigned left : 1, top : 1, right : 1, bottom : 1; };
};
OutCode GetOutCode(double x, double y, int xleft, int ytop, int xright, int ybottom)
{
OutCode out;
out.All = 0;
if (x < xleft)out.left = 1; else if (x > xright)out.right = 1;
if (y < ytop)out.top = 1; else if (y > ybottom)out.bottom = 1;
return out;
}
void VIntersect(double xs, double ys, double xe, double ye, int x, double* xi, double* yi)
{
*xi = x;
*yi = ys + (x - xs) * (ye - ys) / (xe - xs);
}
void HIntersect(double xs, double ys, double xe, double ye, int y, double* xi, double* yi)
{
*yi = y;
*xi = xs + (y - ys) * (xe - xs) / (ye - ys);
}
void CohenSuth(HDC hdc, int xs, int ys, int xe, int ye, int xleft, int ytop, int xright, int ybottom)
{
OutputDebugStringA("Square Line Clipping");
double x1 = xs, y1 = ys, x2 = xe, y2 = ye;
OutCode out1 = GetOutCode(x1, y1, xleft, ytop, xright, ybottom);
OutCode out2 = GetOutCode(x2, y2, xleft, ytop, xright, ybottom);
while ((out1.All || out2.All) && !(out1.All & out2.All))
{
double xi, yi;
if (out1.All)
{
if (out1.left)VIntersect(x1, y1, x2, y2, xleft, &xi, &yi);
else if (out1.top)HIntersect(x1, y1, x2, y2, ytop, &xi, &yi);
else if (out1.right)VIntersect(x1, y1, x2, y2, xright, &xi, &yi);
else HIntersect(x1, y1, x2, y2, ybottom, &xi, &yi);
x1 = xi;
y1 = yi;
out1 = GetOutCode(x1, y1, xleft, ytop, xright, ybottom);
}
else
{
if (out2.left)VIntersect(x1, y1, x2, y2, xleft, &xi, &yi);
else if (out2.top)HIntersect(x1, y1, x2, y2, ytop, &xi, &yi);
else if (out2.right)VIntersect(x1, y1, x2, y2, xright, &xi, &yi);
else HIntersect(x1, y1, x2, y2, ybottom, &xi, &yi);
x2 = xi;
y2 = yi;
out2 = GetOutCode(x2, y2, xleft, ytop, xright, ybottom);
}
}
if (!out1.All && !out2.All)
{
MoveToEx(hdc, Round(x1), Round(y1), NULL);
LineTo(hdc, Round(x2), Round(y2));
}
}
VertexList ClipWithEdge(VertexList p, int edge, IsInFunc In, IntersectFunc Intersect)
{
VertexList OutList;
Vertex v1 = p[p.size() - 1];
bool v1_in = In(v1, edge);
for (int i = 0; i < (int)p.size(); i++)
{
Vertex v2 = p[i];
bool v2_in = In(v2, edge);
if (!v1_in && v2_in)
{
OutList.push_back(Intersect(v1, v2, edge));
OutList.push_back(v2);
}
else if (v1_in && v2_in) OutList.push_back(v2);
else if (v1_in) OutList.push_back(Intersect(v1, v2, edge));
v1 = v2;
v1_in = v2_in;
}
return OutList;
}
bool InLeft(Vertex& v, int edge)
{
return v.x >= edge;
}
bool InRight(Vertex& v, int edge)
{
return v.x <= edge;
}
bool InTop(Vertex& v, int edge)
{
return v.y >= edge;
}
bool InBottom(Vertex& v, int edge)
{
return v.y <= edge;
}
Vertex VIntersect(Vertex& v1, Vertex& v2, int xedge)
{
Vertex res;
res.x = xedge;
res.y = v1.y + (xedge - v1.x) * (v2.y - v1.y) / (v2.x - v1.x);
return res;
}
Vertex HIntersect(Vertex& v1, Vertex& v2, int yedge)
{
Vertex res;
res.y = yedge;
res.x = v1.x + (yedge - v1.y) * (v2.x - v1.x) / (v2.y - v1.y);
return res;
}
void PolygonClip(HDC hdc, POINT* p, int n, int xleft, int ytop, int xright, int ybottom)
{
OutputDebugStringA("Square PolygonClip");
VertexList vlist;
for (int i = 0; i < n; i++)vlist.push_back(Vertex(p[i].x, p[i].y));
vlist = ClipWithEdge(vlist, xleft, InLeft, VIntersect);
vlist = ClipWithEdge(vlist, ytop, InTop, HIntersect);
vlist = ClipWithEdge(vlist, xright, InRight, VIntersect);
vlist = ClipWithEdge(vlist, ybottom, InBottom, HIntersect);
Vertex v1 = vlist[vlist.size() - 1];
for (int i = 0; i < (int)vlist.size(); i++)
{
Vertex v2 = vlist[i];
MoveToEx(hdc, Round(v1.x), Round(v1.y), NULL);
LineTo(hdc, Round(v2.x), Round(v2.y));
v1 = v2;
}
}