-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDDA.py
94 lines (80 loc) · 2.04 KB
/
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
from graphics import *
import time
window_w = 800
window_h = 800
axis = True
toggle = False
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):
""" Bresenham Line Drawing Algorithm For All Kind Of Slopes Of Line """
x, y = x1, y1
win = GraphWin('Brasenham 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 PutPixle(win, x, y):
""" Plot A Pixle In The Windows At Point (x, y) """
newx, newy = convertPixel(x,y)
print("Converted x and y",newx , newy)
pt = Point(newx,newy)
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: "))
DDA(x1, y1, x2, y2)
if __name__ == "__main__":
main()