-
Notifications
You must be signed in to change notification settings - Fork 1
/
sutherland-hodgman-polygon-clipping.cpp
261 lines (236 loc) · 7.3 KB
/
sutherland-hodgman-polygon-clipping.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
#include<iostream>
#include<graphics.h>
using namespace std;
int xmin, xmax, ymin, ymax;
class Coordinate{
public:
int X;
int Y;
Coordinate() {}
Coordinate(float x, float y) {
X = x;
Y = y;
}
};
Coordinate pointOfIntersection(Coordinate s1, Coordinate e1, Coordinate s2, Coordinate e2)
{
float a1 = e1.Y - s1.Y;
float b1 = s1.X - e1.X;
float c1 = a1 * s1.X + b1 * s1.Y;
float a2 = e2.Y - s2.Y;
float b2 = s2.X - e2.X;
float c2 = a2 * s2.X + b2 * s2.Y;
float delta = a1 * b2 - a2 * b1;
return Coordinate((b2 * c1 - b1 * c2) / delta, (a1 * c2 - a2 * c1) / delta);
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int i, j, choice=1;
cout<<"Enter co-ordinates for window ABCD:\n\n";
cout<<"xmin, xmax, ymin, ymax: ";
cin>>xmin>>xmax>>ymin>>ymax;
rectangle(xmin, ymin, xmax, ymax);
do{
int n;
cout<<"\n\nEnter number of vertices (max 10): ";
cin>>n;
Coordinate V[n];
cout<<"Enter co-ordinates of vertices\n";
int drawPolygon[n*2], newPolygon[100];
for(i=0, j=0; i<n; i++) {
cout<<"Enter x"<<i+1<<" and y"<<i+1<<": ";
cin>>V[i].X>>V[i].Y;
drawPolygon[j++] = V[i].X;
drawPolygon[j++] = V[i].Y;
}
drawPolygon[j++] = V[0].X;
drawPolygon[j] = V[0].Y;
setcolor(MAGENTA);
drawpoly(n+1, drawPolygon);
Coordinate top_left(xmin, ymin), top_right(xmax, ymin), bot_left(xmin, ymax), bot_right(xmax, ymax);
int newN;
cout<<endl;
for(i=0, j=0, newN=0; i<n; i++) {
Coordinate temp;
if(V[i].X<xmin) {
if(V[(i+1)%n].X<xmin)
cout<<"\nOut->Out";
else {
temp = pointOfIntersection(V[i], V[(i+1)%n], top_left, bot_left);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nOut->In";
}
}
else {
if(V[(i+1)%n].X<xmin) {
temp = pointOfIntersection(V[i], V[(i+1)%n], top_left, bot_left);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
cout<<"\nIn->Out";
}
else {
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nIn->In";
}
}
}
newPolygon[j++] = newPolygon[0];
newPolygon[j] = newPolygon[1];
setcolor(RED);
cout<<"\nRed coordinates: ";
for(i=0; i<=j; i++)
cout<<newPolygon[i]<<" ";
drawpoly(newN+1, newPolygon);
for(i=0, j=0; i<newN; i++) {
V[i].X = newPolygon[j++];
V[i].Y = newPolygon[j++];
}
n = newN;
cout<<endl;
for(i=0, j=0, newN=0; i<n; i++) {
Coordinate temp;
if(V[i].X>xmax) {
if(V[(i+1)%n].X>xmax)
cout<<"\nOut->Out";
else {
temp = pointOfIntersection(V[i], V[(i+1)%n], top_right, bot_right);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nOut->In";
}
}
else {
if(V[(i+1)%n].X>xmax) {
temp = pointOfIntersection(V[i], V[(i+1)%n], top_right, bot_right);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
cout<<"\nIn->Out";
}
else {
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nIn->In";
}
}
}
newPolygon[j++] = newPolygon[0];
newPolygon[j] = newPolygon[1];
setcolor(GREEN);
cout<<"\nGreen coordinates: ";
for(i=0; i<=j; i++)
cout<<newPolygon[i]<<" ";
drawpoly(newN+1, newPolygon);
for(i=0, j=0; i<newN; i++) {
V[i].X = newPolygon[j++];
V[i].Y = newPolygon[j++];
}
n = newN;
cout<<endl;
for(i=0, j=0, newN=0; i<n; i++) {
Coordinate temp;
if(V[i].Y>ymax) {
if(V[(i+1)%n].Y>ymax)
cout<<"\nOut->Out";
else {
temp = pointOfIntersection(V[i], V[(i+1)%n], bot_left, bot_right);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nOut->In";
}
}
else {
if(V[(i+1)%n].Y>ymax) {
temp = pointOfIntersection(V[i], V[(i+1)%n], bot_left, bot_right);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
cout<<"\nIn->Out";
}
else {
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nIn->In";
}
}
}
newPolygon[j++] = newPolygon[0];
newPolygon[j] = newPolygon[1];
setcolor(BLUE);
cout<<"\nBlue coordinates: ";
for(i=0; i<=j; i++)
cout<<newPolygon[i]<<" ";
drawpoly(newN+1, newPolygon);
for(i=0, j=0; i<newN; i++) {
V[i].X = newPolygon[j++];
V[i].Y = newPolygon[j++];
}
n = newN;
cout<<endl;
for(i=0, j=0, newN=0; i<n; i++) {
Coordinate temp;
if(V[i].Y<ymin) {
if(V[(i+1)%n].Y<ymin)
cout<<"\nOut->Out";
else {
temp = pointOfIntersection(V[i], V[(i+1)%n], top_left, top_right);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nOut->In";
}
}
else {
if(V[(i+1)%n].Y<ymin) {
temp = pointOfIntersection(V[i], V[(i+1)%n], top_left, top_right);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
cout<<"\nIn->Out";
}
else {
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nIn->In";
}
}
}
newPolygon[j++] = newPolygon[0];
newPolygon[j] = newPolygon[1];
for(i=0, j=0; i<newN; i++) {
V[i].X = newPolygon[j++];
V[i].Y = newPolygon[j++];
}
n = newN;
setcolor(YELLOW);
drawpoly(newN+1, newPolygon);
cout<<"\n\nContinue?\t1.Yes\t0.No:\t";
cin>>choice;
}while(choice!=0);
getch();
closegraph();
}