Repository navigation
Expand file tree
/
Copy pathClipping.cpp
More file actions
246 lines (215 loc) · 7.14 KB
/
Copy pathClipping.cpp
File metadata and controls
246 lines (215 loc) · 7.14 KB
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
#include <bits/stdc++.h>
#include <graphics.h>
#include <gtk/gtk.h>
#include "clipping.h"
using namespace std;
// Global Variables
int xmin, xmax, ymin, ymax;
// This will return the sign required.
int sign(int x)
{
if (x > 0)
return 1;
else
return 0;
}
// CohenSutherLand LineClipping Algorith As Described in theory.
// This will clip the lines as per window boundries.
void clip(struct lines mylines)
{
// arrays will store bits
// Here bits impiles initial Point whereas bite implies end points
int bits[4], bite[4], i, var;
// setting color of graphics to be RED
setcolor(RED);
// Finding Bits
bits[0] = sign(xmin - mylines.x1);
bite[0] = sign(xmin - mylines.x2);
bits[1] = sign(mylines.x1 - xmax);
bite[1] = sign(mylines.x2 - xmax);
bits[2] = sign(ymin - mylines.y1);
bite[2] = sign(ymin - mylines.y2);
bits[3] = sign(mylines.y1 - ymax);
bite[3] = sign(mylines.y2 - ymax);
// initial will used for initial coordinates and end for final
string initial = "", end = "", temp = "";
// convert bits to string
for (i = 0; i < 4; i++) {
if (bits[i] == 0)
initial += '0';
else
initial += '1';
}
for (i = 0; i < 4; i++) {
if (bite[i] == 0)
end += '0';
else
end += '1';
}
// finding slope of line y=mx+c as (y-y1)=m(x-x1)+c
// where m is slope m=dy/dx;
float m = (mylines.y2 - mylines.y1) / (float)(mylines.x2 - mylines.x1);
float c = mylines.y1 - m * mylines.x1;
// if both points are inside the Accept the line and draw
if (initial == end && end == "0000") {
// inbuild function to draw the line from(x1, y1) to (x2, y2)
line(mylines.x1, mylines.y1, mylines.x2, mylines.y2);
return;
}
// this will contain cases where line maybe totally outside for partially inside
else {
// taking bitwise end of every value
for (i = 0; i < 4; i++) {
int val = (bits[i] & bite[i]);
if (val == 0)
temp += '0';
else
temp += '1';
}
// as per algo if AND is not 0000 means line is completely outside hene draw nothing and retrurn
if (temp != "0000")
return;
// Here contain cases of partial inside or outside
// So check for every boundary one by one
for (i = 0; i < 4; i++) {
// if boths bit are same hence we cannot find any intersection with boundary so continue
if (bits[i] == bite[i])
continue;
// Otherwise there exist a intersection
// Case when initial point is in left xmin
if (i == 0 && bits[i] == 1) {
var = round(m * xmin + c);
mylines.y1 = var;
mylines.x1 = xmin;
}
// Case when final point is in left xmin
if (i == 0 && bite[i] == 1) {
var = round(m * xmin + c);
mylines.y2 = var;
mylines.x2 = xmin;
}
// Case when initial point is in right of xmax
if (i == 1 && bits[i] == 1) {
var = round(m * xmax + c);
mylines.y1 = var;
mylines.x1 = xmax;
}
// Case when final point is in right of xmax
if (i == 1 && bite[i] == 1) {
var = round(m * xmax + c);
mylines.y2 = var;
mylines.x2 = xmax;
}
// Case when initial point is in top of ymin
if (i == 2 && bits[i] == 1) {
var = round((float)(ymin - c) / m);
mylines.y1 = ymin;
mylines.x1 = var;
}
// Case when final point is in top of ymin
if (i == 2 && bite[i] == 1) {
var = round((float)(ymin - c) / m);
mylines.y2 = ymin;
mylines.x2 = var;
}
// Case when initial point is in bottom of ymax
if (i == 3 && bits[i] == 1) {
var = round((float)(ymax - c) / m);
mylines.y1 = ymax;
mylines.x1 = var;
}
// Case when final point is in bottom of ymax
if (i == 3 && bite[i] == 1) {
var = round((float)(ymax - c) / m);
mylines.y2 = ymax;
mylines.x2 = var;
}
// Updating Bits at every point
bits[0] = sign(xmin - mylines.x1);
bite[0] = sign(xmin - mylines.x2);
bits[1] = sign(mylines.x1 - xmax);
bite[1] = sign(mylines.x2 - xmax);
bits[2] = sign(ymin - mylines.y1);
bite[2] = sign(ymin - mylines.y2);
bits[3] = sign(mylines.y1 - ymax);
bite[3] = sign(mylines.y2 - ymax);
} // end of for loop
// Inialize initial and end to NULL
initial = "", end = "";
// Updating strings again by bit
for (i = 0; i < 4; i++) {
if (bits[i] == 0)
initial += '0';
else
initial += '1';
}
for (i = 0; i < 4; i++) {
if (bite[i] == 0)
end += '0';
else
end += '1';
}
// If now both points lie inside or on boundary then simply draw the updated line
if (initial == end && end == "0000") {
line(mylines.x1, mylines.y1, mylines.x2, mylines.y2);
return;
}
// else line was completely outside hence rejected
else
return;
}
}
// Driver Function
void clipping(GtkButton* btn, GtkWidget* win_menu)
{
/// hide the menu window
gtk_widget_hide(win_menu);
int gd = DETECT, gm;
// Setting values of Clipping window
printf("Donner les dimensions de la fenetre: \n");
printf("xmin : ");
scanf("%d",&xmin);
ymin = xmin;
printf("xmax : ");
scanf("%d",&xmax);
ymax = xmax;
// intialize the graph
initgraph(&gd, &gm, NULL);
// Drawing Window using Lines
line(xmin, ymin, xmax, ymin);
line(xmax, ymin, xmax, ymax);
line(xmax, ymax, xmin, ymax);
line(xmin, ymax, xmin, ymin);
// Assume 4 lines to be clipped
int n;
printf("Donner le nombre des lignes a tracer: ");
scanf("%d",&n);
struct lines mylines[n];
// Setting the coordinated of 4 lines
for (int i = 0; i < n; i++) {
printf("Ligne %d\n", i);
printf("Point 1 :\n");
printf("x y :");
scanf("%d %d", &mylines[i].x1,&mylines[i].y1 );
printf("\nPoint 2 : \n");
printf("x y :");
scanf("%d %d", &mylines[i].x2,&mylines[i].y2 );
printf("\n");
}
// Drwaing Initial Lines without clipping
for (int i = 0; i < n; i++) {
line(mylines[i].x1, mylines[i].y1,
mylines[i].x2, mylines[i].y2);
delay(1000);
}
// Drwaing clipped Line
for (int i = 0; i < n; i++) {
// Calling clip() which in term clip the line as per window and draw it
clip(mylines[i]);
delay(1000);
}
delay(4000);
getch();
// For Closing the graph.
closegraph();
}