-
Notifications
You must be signed in to change notification settings - Fork 2
/
integerBresenham.py
185 lines (163 loc) · 5.52 KB
/
integerBresenham.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
#!/usr/bin/env python
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
'''----------------------------------------------------------------------------
Global Variables
----------------------------------------------------------------------------'''
#(x0,y0), (x1,y1)
initialX = initialY= terminalX = terminalY = 0
#square model
linePoints=[]
#number of clicks
numberOfClicks = 0
#view dimentions
width = 500
height = 600
#coordinates of the view for gluOrtho2D
viewCoordinates = [0, width, 0 ,height]
'''----------------------------------------------------------------------------
viewSetting:
settings for display
params: none
return: none
----------------------------------------------------------------------------'''
def viewSetting():
glMatrixMode(GL_PROJECTION)
gluOrtho2D( viewCoordinates[0],
viewCoordinates[1],
viewCoordinates[2],
viewCoordinates[3]
)
'''----------------------------------------------------------------------------
drawLine:
draws pixel for a line
params: none
return: none
----------------------------------------------------------------------------'''
def drawLine():
glClear(GL_COLOR_BUFFER_BIT)
glBegin(GL_POINTS)
for point in linePoints:
glVertex2fv(point) #map points according to the coordinates they belong to
glEnd()
glFlush()
'''----------------------------------------------------------------------------
bresenhamAlg:
Calculates the points for a line on a grid according to
Bresenham's Algorithm
params:
initialX = x0, initial x coordinate of the line
initialY = y0, initial y coordinate of the line
terminalX = x1, final x coordinate of the line
terminalY = y1, final y coordinate of the line
return: none
----------------------------------------------------------------------------'''
def bresenhamAlg(initialX,initialY,terminalX,terminalY):
xstep = 1
ystep = 1
#Calculate dx and dy
dx = abs(terminalX - initialX)
dy = abs(terminalY - initialY)
# X as the driving axis
if dx >= dy:
E = 2* dy - dx
# swap points if x0 > x1
if initialX > terminalX :
initialX,terminalX = terminalX, initialX
initialY,terminalY = terminalY, initialY
# to support 4nd and 8th octant
if initialY > terminalY :
ystep = ystep * -1
for i in range(initialX, terminalX, xstep):
linePoints.append((initialX,initialY))
if E >= 0:
initialY = initialY + ystep
E = E - 2*dx
E = E + 2* dy
initialX = initialX + xstep
# Y as the driving axis
else:
E = 2* dx - dy
# swap points if y0 > y1
if initialY > terminalY :
initialX,terminalX = terminalX, initialX
initialY,terminalY = terminalY, initialY
# to support 3nd and 7th octant
if initialX > terminalX :
xstep = xstep * -1
for i in range(initialY, terminalY, ystep):
linePoints.append((initialX,initialY))
if E >= 0:
initialX = initialX + xstep
E = E - 2* dy
E = E + 2 * dx
initialY = initialY + ystep
def parametricLine(initialX,initialY,terminalX,terminalY):
x = initialX
while x < terminalX:
t = ( x - initialX)/(terminalX - initialX)
y = initialY + t * (terminalY - initialY)
print(x , t, y)
linePoints.append((x,math.floor(y)))
'''----------------------------------------------------------------------------
mouse:
callback to listen mouse clicks
params:
button = checks for b
state = check the type of event emited by button
x = x coordinate based on cursor position on window
y = y coordinate based on cursor position on window
return: none
----------------------------------------------------------------------------'''
def mouse( button, state, x, y):
global linePoints
global numberOfClicks
global initialX
global initialY
global terminalX
global terminalY
linePoints = []
#check for left click on relese
if button == GLUT_LEFT_BUTTON and state == GLUT_UP:
#print numberOfClicks
if numberOfClicks == 0:
initialX = x
initialY = height - y
numberOfClicks = 1 + numberOfClicks
else :
terminalX = x
terminalY = height - y
#print initialX,initialY,terminalX,terminalY
bresenhamAlg(initialX, initialY, terminalX, terminalY)
drawLine()
print ( linePoints)
numberOfClicks = 0
'''----------------------------------------------------------------------------
display:
display seetings for line
params: none
return: none
----------------------------------------------------------------------------'''
def display():
glClearColor(0.0,0.0,0.0,0.0)
glColor3f(255, 255, 255)
glClear(GL_COLOR_BUFFER_BIT)
glFlush()
'''----------------------------------------------------------------------------
main:
open GL configuration
params: none
return: none
----------------------------------------------------------------------------'''
def main():
glutInit()
glutInitWindowSize(width,height)
glutCreateWindow("Integer Bresenham Algorithm")
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
viewSetting()
glutMouseFunc(mouse)
glutDisplayFunc(display)
glutMainLoop()
if __name__ == '__main__':
main()