-
Notifications
You must be signed in to change notification settings - Fork 1
/
line.py
137 lines (114 loc) · 3.6 KB
/
line.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
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import math
class MidpointLine:
"""
This class is designed to display the last two digits of a student id.
User can input his/her student id and the last two digits will be displayed in the window.
Here Mid Point Line (MPL) with 8-Way Symmetry is used.
Author- Priom Deb
http://priomdeb.com, [email protected]
"""
def __init__(self):
self.__midpoint_points = []
def find_zone(self, x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
if abs(dx) > abs(dy):
if dx >= 0 and dy >= 0:
return 0
elif dx <= 0 and dy >= 0:
return 3
elif dx <= 0 and dy <= 0:
return 4
elif dx >= 0 and dy <= 0:
return 7
else:
if dx >= 0 and dy >= 0:
return 1
elif dx <= 0 and dy >= 0:
return 2
elif dx <= 0 and dy <= 0:
return 5
elif dx >= 0 and dy <= 0:
return 6
def convert_to_zone0(self, x1, y1, zone):
"""
Converting to Zone 0 from any zone.
\nFrom Zone 0 -> x = x, y = y
\nFrom Zone 1 -> x = y, y = x
\nFrom Zone 2 -> x = y, y = -x
\nFrom Zone 3 -> x = -x, y = y
\nFrom Zone 4 -> x = -x, y = -y
\nFrom Zone 5 -> x = -y, y = -x
\nFrom Zone 6 -> x = -y, y = x
\nFrom Zone 7 -> x = x, y = -y
:return: x1, y1, x2, y2
"""
if zone == 0:
return x1, y1
elif zone == 1:
return y1, x1
elif zone == 2:
return y1, -x1
elif zone == 3:
return -x1, y1
elif zone == 4:
return -x1, -y1
elif zone == 5:
return -y1, -x1
elif zone == 6:
return -y1, x1
elif zone == 7:
return x1, -y1
def convert_to_original_zone(self, x1, y1, zone):
"""
Converting to original zone from zone 0.
The method is as same as converting to Zone 0 from any zone except for zone 2 and zone 6
\nFrom Zone 2 -> x = -y, y = x
\nFrom Zone 6 -> x = y, y = -x
:return: x1, y1, x2, y2
"""
if zone == 0:
return x1, y1
elif zone == 1:
return y1, x1
elif zone == 2:
return -y1, x1
elif zone == 3:
return -x1, y1
elif zone == 4:
return -x1, -y1
elif zone == 5:
return -y1, -x1
elif zone == 6:
return y1, -x1
elif zone == 7:
return x1, -y1
def midpoint(self, x1, y1, x2, y2):
glBegin(GL_POINTS)
zone = self.find_zone(x1, y1, x2, y2)
x1_to_z0, y1_to_z0 = self.convert_to_zone0(x1, y1, zone)
x2_to_z0, y2_to_z0 = self.convert_to_zone0(x2, y2, zone)
dy = y2_to_z0 - y1_to_z0
dx = x2_to_z0 - x1_to_z0
d = 2 * dy - dx
d_E = 2 * dy
d_NE = 2 * (dy - dx)
x = x1_to_z0
y = y1_to_z0
original_x, original_y = self.convert_to_original_zone(x, y, zone)
glVertex2f(original_x, original_y)
while x <= x2_to_z0:
self.__midpoint_points.append((original_x, original_y))
if d < 0:
x = x + 1
d = d + d_E
else:
x = x + 1
y = y + 1
d = d + d_NE
original_x, original_y = self.convert_to_original_zone(x, y, zone)
glVertex2f(original_x, original_y)
glEnd()