-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclipping.py
More file actions
190 lines (141 loc) · 4.11 KB
/
clipping.py
File metadata and controls
190 lines (141 loc) · 4.11 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
from OpenGL.GL import*
from OpenGL.GLU import*
from OpenGL.GLUT import*
def clearscreen():
glClearColor(0.0, 0.0, 0.0, 0.0)
gluOrtho2D(-100, 100, -100, 100)
glClear(GL_COLOR_BUFFER_BIT)
glPointSize(5.0)
def setpixel(edges, points, rgb):
glColor3f(rgb[0], rgb[1], rgb[2])
glBegin(GL_LINES)
for e in edges:
for v in e:
glVertex2fv(points[v])
glEnd()
glFlush()
def rectangle(x1, y1, x2, y2):
edges = [
[0, 1],
[1, 2],
[2, 3],
[3, 0]
]
points = [
[x1, y2],
[x2, y2],
[x2, y1],
[x1, y1]
]
rgb = (0.0, 0.0, 1.0)
setpixel(edges, points, rgb)
# -------line--------------------------------
def line(x1, y1, x2, y2):
edges = [
[0, 1],
]
points = [
[x1, y1],
[x2, y2]
]
rgb = (1.0, 0.0, 0.0)
setpixel(edges, points, rgb)
# ENCODE FUNCTION
INSIDE = 0
LEFT = 1
RIGHT = 2
BOTTOM = 4
TOP = 8
def encode(x, y, xl, xr, yb, yt):
region_code = INSIDE
if x < xl:
region_code = region_code | LEFT
if x > xr:
region_code = region_code | RIGHT
if y < yb:
region_code = region_code | BOTTOM
if y > yt:
region_code = region_code | TOP
return region_code
# Cohen Sutherland
def cohensutherland(x1, y1, x2, y2, xl, xr, yb, yt):
rectangle(xl, yb, xr, yt)
line(x1, y1, x2, y2)
# step1
r_code1 = encode(x1, y1, xl, xr, yb, yt)
r_code2 = encode(x2, y2, xl, xr, yb, yt)
accept = False
while True:
# step2
if r_code1 == 0 and r_code2 == 0:
# to complete accceptance
accept = True
break
# step3
elif r_code1 & r_code2 != 0:
# to complete rejection
break
# step4
else:
if r_code1 != 0:
r_codeout = r_code1
else:
r_codeout = r_code2
# intersection point in xl
if r_codeout & LEFT:
y = (xl-x1)*(y2-y1)/(x2-x1)+y1
x = xl # x1,y1 are the intersecting point of x=xl line
elif r_codeout & RIGHT:
y = (xr-x1)*(y2-y1)/(x2-x1)+y1
x = xr # x1,y1 are the intersecting point of x=xr line
elif r_codeout & BOTTOM:
x = (x2-x1)*(yb-y1)/(y2-y1)+x1
y = yb # x1,y1 are the intersecting point of y=yb line
elif r_codeout & TOP:
x = (x2-x1)*(yt-y1)/(y2-y1)+x1
y = yt # x1,y1 are the intersecting point of y=yt line
if r_codeout == r_code1:
x1, y1 = x, y
r_code1 = encode(x, y, xl, xr, yb, yt)
else:
x2, y2 = x, y
r_code2 = encode(x, y, xl, xr, yb, yt)
# Here x1,y1 and x2,y2 are the clipped lines and we need to draw this line
if accept:
edges = [
[0, 1]
]
points = [
[x1, y1],
[x2, y2]
]
rgb = (0.0, 1.0, 0.0)
setpixel(edges, points, rgb)
else:
print("The given line cannot be clipped!")
def main():
choice = 0
while(choice != 2):
choice = int(input("Enter\n\t1.Line Clipping \n\t2.exit\n"))
if choice == 1:
print("ENTER THE LINE DETAILS")
x1 = float(input('x1 Coordinate:'))
y1 = float(input('y1 Coordinate:'))
x2 = float(input("x2 Coordinate:"))
y2 = float(input("y2 Coordinate:"))
print("ENTER THE WINDOW DETAILS")
xl = float(input('x_Wmin='))
xr = float(input('x_Wmax='))
yb = float(input("y_Wmin="))
yt = float(input("y_Wmax="))
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize(700, 700)
glutInitWindowPosition(0, 0)
glutCreateWindow("Line Clipping")
glutDisplayFunc(lambda: cohensutherland( x1, y1, x2, y2, xl, xr, yb, yt))
clearscreen()
glutMainLoop()
else:
print("Invalid Choice")
main()