-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBresenham.py
56 lines (40 loc) · 1.09 KB
/
Bresenham.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
from graphics import *
win = GraphWin("Bresenham",1000,1000)
# function for line generation
def bresenham(x1,y1,x2, y2):
m_new = 2 * (y2 - y1)
slope_error_new = m_new - (x2 - x1)
y=y1
for x in range(x1,x2+1):
#print("(",x ,",",y ,")\n")
pt1 = Point(x,y)
pt1.setFill('blue')
pt1.draw(win)
slope_error_new =slope_error_new + m_new
if (slope_error_new >= 0):
y=y+1
slope_error_new =slope_error_new - 2 * (x2 - x1)
def DDA(x1, y1, x2, y2):
x = x1
y = y1
dx = x2-x1
dy = y2-y1
n = max(abs(dx), abs(dy))
dt = n
dxdt = dx//dt
dydt = dy//dt
x = x1
y = y1
while n != 0:
pt1 = Point(round(x), round(y))
pt1.setFill('red')
pt1.draw(win)
n = n - 1
x = x + dxdt
y = y + dydt
def main():
bresenham(100,100,200,100)
bresenham(100,200,200,200)
DDA(100,100,100,200)
DDA(200,100,200,200)
main()