-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab_6_sutherlandHodgeman.py
186 lines (143 loc) · 4.46 KB
/
Lab_6_sutherlandHodgeman.py
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
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys
original_points = [[0, 5.5], [2.5, 3.5], [-2.5, 1.5], [-4, 3.5]]
x_min, y_min = -2, 2
x_max, y_max = 2, 5
def drawAxes():
glColor(0, 0, 1, 0)
glBegin(GL_LINES)
glVertex2i(1, 0)
glVertex2i(-1, 0)
glVertex2i(0, 1)
glVertex2i(0, -1)
glEnd()
glColor(1, 1, 1, 1)
def drawClippingWindow(div):
glBegin(GL_LINE_LOOP)
glVertex2f(x_min / div, y_min / div)
glVertex2f(x_max / div, y_min / div)
glVertex2f(x_max / div, y_max / div)
glVertex2f(x_min / div, y_max / div)
glEnd()
def leftClipper(start, end):
clipped = list()
if end[0] != start[0]:
m = (end[1] - start[1]) / (end[0] - start[0])
else:
m = float('inf')
if start[0] < x_min and x_min <= end[0]:
y = end[1] - m * (end[0] - x_min)
clipped.append([x_min, y])
clipped.append(end)
elif start[0] >= x_min and end[0] >= x_min:
clipped.append(end)
elif start[0] >= x_min and end[0] < x_min:
y = end[1] - m * (end[0] - x_min)
clipped.append([x_min, y])
return clipped
def rightClipper(start, end):
clipped = list()
if end[0] != start[0]:
m = (end[1] - start[1]) / (end[0] - start[0])
else:
m = float('inf')
if start[0] > x_max and end[0] <= x_max:
y = end[1] - m * (end[0] - x_max)
clipped.append([x_max, y])
clipped.append(end)
elif start[0] <= x_max and end[0] <= x_max:
clipped.append(end)
elif start[0] <= x_max and end[0] > x_max:
y = end[1] - m * (end[0] - x_max)
clipped.append([x_max, y])
return clipped
def bottomClipper(start, end):
clipped = list()
if end[0] != start[0]:
m = (end[1] - start[1]) / (end[0] - start[0])
else:
m = float('inf')
if start[1] < y_min and end[1] >= y_min:
x = end[0] + (y_min - end[1]) / m
clipped.append([x, y_min])
clipped.append(end)
elif start[1] >= y_min and end[1] >= y_min:
clipped.append(end)
elif start[1] >= y_min and end[1] < y_min:
x = end[0] + (y_min - end[1]) / m
clipped.append([x, y_min])
return clipped
def topClipper(start, end):
clipped = list()
if end[0] != start[0]:
m = (end[1] - start[1]) / (end[0] - start[0])
else:
m = float('inf')
if start[1] > y_max and end[1] <= y_max:
x = end[0] + (y_max - end[1]) / m
clipped.append([x, y_max])
clipped.append(end)
elif start[1] <= y_max and end[1] <= y_max:
clipped.append(end)
elif start[1] <= y_max and end[1] > y_max:
x = end[0] + (y_max - end[1]) / m
clipped.append([x, y_max])
return clipped
def calcDiv(points):
div = max(abs(x_min), abs(x_max), abs(y_min), abs(y_max))
for i in points:
for j in i:
div = max(div, abs(j))
return (div + 1)
def drawPoints(points, div, color='r'):
glBegin(GL_POLYGON)
glColor(1, 0, 0, 0)
if color == 'g':
glColor(0, 1, 0, 0)
for point in points:
glVertex2f(point[0] / div, point[1] / div)
glColor(1, 1, 1, 0)
glEnd()
def sutherlandHodgeman():
drawAxes()
plotted = list()
points = original_points
div = calcDiv(points)
drawPoints(points, div)
temp_points = list()
for i in range(len(points)):
clipped = leftClipper(points[i], points[(i + 1) % len(points)])
temp_points.extend(clipped)
points = temp_points
temp_points = list()
for i in range(len(points)):
clipped = rightClipper(points[i], points[(i + 1) % len(points)])
temp_points.extend(clipped)
points = temp_points
temp_points = list()
for i in range(len(points)):
clipped = bottomClipper(points[i], points[(i + 1) % len(points)])
temp_points.extend(clipped)
points = temp_points
temp_points = list()
for i in range(len(points)):
clipped = topClipper(points[i], points[(i + 1) % len(points)])
temp_points.extend(clipped)
points = temp_points
drawPoints(points, div, color='g')
drawClippingWindow(div)
print(points)
glutSwapBuffers()
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE)
glutInitWindowSize(500, 500)
glutInitWindowPosition(350, 150)
glutCreateWindow(b'Polygon Clipping')
# gluOrtho2D(-50, 50, -50, 50)
glutDisplayFunc(sutherlandHodgeman)
glutMainLoop()
if __name__ == '__main__':
main()