-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbressenham_and_DDA.py
170 lines (148 loc) · 3.63 KB
/
bressenham_and_DDA.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
from graphics import *
import time
window_w = 1200
window_h = 800
def DrawAxis(win):
for i in range(window_h):
pt = Point(window_w/2,i)
pt.draw(win)
for i in range(window_w):
pt = Point(i,window_h/2)
pt.draw(win)
def ConvertPixel(xx,yy):
xx+=window_w/2
yy = -yy
yy+=window_h/2
return xx,yy
def DDA(x1,y1,x2,y2):
x, y = x1, y1
win = GraphWin('DDA Line Drawing Algorithm', window_w, window_h)
DrawAxis(win)
if x1==x2 and y1==y2 :
PutPixle(win,x1,y1)
elif y1==y2:
if x1<x2:
while x != x2 :
PutPixle(win,x,y)
x+=1
else:
while x != x2 :
PutPixle(win,x,y)
x-=1
elif (x1==x2):
if y1<y2:
while y!=y2:
PutPixle(win,x,y)
y+=1
else:
while y!=y2:
PutPixle(win,x,y)
y-=1
else:
steps = 0
dx = abs(x2 - x1)
dy = abs(y2 - y1)
slope = dy/float(dx)
if dx>dy:
steps = dx
else:
steps = dy
xIncrement = (x2-x1)/float(steps)
yIncrement = (y2-y1)/float(steps)
x=x1
y=y1
PutPixle(win,x,y)
for i in range(steps):
x+=xIncrement
y+=yIncrement
x = round(x)
y = round(y)
PutPixle(win,x,y)
PutPixle(win,x2,y2)
win.getMouse()
win.close()
def BresenhamLine(x1,y1,x2,y2):
x, y = x1, y1
win = GraphWin('Brasenham Line Drawing Algorithm', window_w, window_h)
DrawAxis(win)
dx = abs(x2 - x1)
dy = abs(y2 - y1)
if x1==x2 and y1==y2 :
PutPixle(win,x1,y1)
elif y1==y2:
if x1<x2:
while x != x2 :
PutPixle(win,x,y)
x+=1
else:
while x != x2 :
PutPixle(win,x,y)
x-=1
elif (x1==x2):
if y1<y2:
while y!=y2:
PutPixle(win,x,y)
y+=1
else:
while y!=y2:
PutPixle(win,x,y)
y-=1
elif dy<dx:
xIncrement = 0
yIncrement = 0
if x1<x2:
xIncrement = 1
else:
xIncrement = -1
if y1<y2:
yIncrement = 1
else:
yIncrement = -1
p = 2*dy-dx
while x != x2:
if p>0:
p=p+2*(dy-dx)
y+=yIncrement
else:
p = p+2*dy
x+=xIncrement
PutPixle(win,x,y)
else:
xIncrement = 0
yIncrement = 0
if x1<x2:
xIncrement = 1
else:
xIncrement = -1
if y1<y2:
yIncrement = 1
else:
yIncrement = -1
p = 2*dx-dy
while y != y2:
if p>0:
p=p+2*(dx-dy)
x+=xIncrement
else:
p = p+2*dx
y+=yIncrement
PutPixle(win,x,y)
PutPixle(win,x2,y2)
win.getMouse()
win.close()
def PutPixle(win, x, y):
x, y = ConvertPixel(x,y)
pt = Point(x,y)
pt.draw(win)
def main():
x1 = int(input("Enter Start X: "))
y1 = int(input("Enter Start Y: "))
x2 = int(input("Enter End X: "))
y2 = int(input("Enter End Y: "))
option = int(input("Enter 1 for BressenhamLine Drawing Algorithm or 2 for DDA \n"))
if option==1:
BresenhamLine(x1, y1, x2, y2)
elif option==2:
DDA(x1,y1,x2,y2)
if __name__ == "__main__":
main()