-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlineClipping.cpp
More file actions
executable file
·248 lines (143 loc) · 3.79 KB
/
Copy pathlineClipping.cpp
File metadata and controls
executable file
·248 lines (143 loc) · 3.79 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
#include<iostream>
using namespace std;
class wcPt2D {
public:
GLfloat x, y;
};
inline GLint round(const GLfloat a) { return GLint ( a + 0.5); }
const GLint winLeftBitCode = 0x1;
const GLint winRightBitCode = 0x2;
const GLint winBottomBitCode = 0x4;
const GLint winTopBitCode = 0x8;
inline GLint inside(GLint code) { return GLint(!code); }
/*
any line that has a region-code value of 1 in the same bit position for each endpoint is completely outside the clipping rectangle, we eliminate that line segment
any line that are completely inside the clip window and which are completely outside
we can perform the inside-outside test for segments using logical operators. when the or operation between two endpoint region code for a line segment is false (0000)
the line is inside of the clipping window
*/
inline GLint reject(GLint code1, GLint code2) { return GLint( code1 & code2); }
inline GLint accept(GLint code1, GLint code2) { return GLint(!(code1 | code2)); }
GLubyte encode(wcPt2D pt, wcPt2D winMin, wcPt2D winMax)
{
GLubyte code = 0X00;
if(pt.x < winMin.x)
{
code = code | winLeftBitCode;
}
if(pt.x > winMax.x)
{
code = code | winRightBitCode;
}
if(pt.y < winMin.y)
{
code = code | winBottomBitCode;
}
if(pt.y > winMax.y)
{
code = code | winTopBitCode;
}
return code;
}
void swapPts(wcPt2D *pt1, wcPt2D* pt2)
{
wcPt2D tmp;
tmp = *pt1; *pt1 = *pt2; *pt2 = tmp;
}
void swapCodes(GLubyte * c1, GLubyte *c2)
{
GLubyte tmp;
tmp = *c1;
*c1 = *c2;
*c2 = tmp;
}
void lineClip(wcPt2D winMin, wcPt2D winMax, wcPt2D p1, wcPt2D p2)
{
GLubyte code1, code2;
GLint done = false, plotLine = false;
GLfloat m;
while(!done)
{
code1 = encode(p1,winMin,winMax);
code2 = encode(p2,winMin,winMax);
if(accept(code1, code2))
{
done = true;
plotLine = true;
}
else
{
if(reject(code1,code2))
{
done = false;
}
else
{
if(inside(code1))
{
swapPts(&p1,&p2);
swapCodes(&code1,&code2);
}
if( p2.x != p1.x)
{
m = (p2.y - p1.y) / (p2.x - p1.x);
}
if( code1 & winLeftBitCode)
{
p1.y += m * (winMin.x - p1.x);
p1.x = winMin.x;
}
else
{
if( code1 & winRightBitCode)
{
p1.y += (winMax.x - p1.x) * m;
p1.x = winMax.x;
}
else
{
if( code1 & winBottomBitCode)
{
if(p2.x != p1.x)
{
p1.x += (winMin.y - p1.y) / m;
}
p1.y = winMin.y;
}
else
{
if(code1 & winTopBitCode)
{
if(p2.x != p1.x)
{
p1.x += (winMax.y - p1.y)/m;
}
p1.y = winMax.y;
}
}
}
}
}
}
}
if(plotLine)
{
cout << p1.x << " " << p1.y << " " << p2.x << " " << p2.y << endl;
}
}
int main(int argc ,char** argv)
{
wcPt2D winMin;
winMin.x = 1;
winMin.y = 1;
wcPt2D winMax;
winMax.x = 7;
winMax.y = 5;
wcPt2D p1;
p1.x = 4;
p1.y = 9;
wcPt2D p2;
p2.x = 11;
p2.y = 2;
lineClip(winMin,winMax,p1,p2);
}