-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBresenham Line Drawing Algo.py
More file actions
52 lines (39 loc) · 1.1 KB
/
Bresenham Line Drawing Algo.py
File metadata and controls
52 lines (39 loc) · 1.1 KB
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
import matplotlib.pyplot as plt
plt.title("Bresenham Algorithm")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
def bres(x1, y1, x2, y2):
x, y = x1, y1
dx = abs(x2 - x1)
dy = abs(y2 - y1)
gradient = dy/float(dx)
if gradient > 1:
dx, dy = dy, dx
x, y = y, x
x1, y1 = y1, x1
x2, y2 = y2, x2
p = 2 * dy - dx
print('x = %s, y = %s' % (x, y))
# initialize the plotting points
xcoordinates = [x]
ycoordinates = [y]
for k in range(dx):
if p > 0:
y = y + 1 if y < y2 else y - 1
p = p + 2 * (dy - dx)
else:
p = p + 2 * dy
x = x + 1 if x < x2 else x - 1
print('x = %s, y = %s' % (x, y))
xcoordinates.append(x)
ycoordinates.append(y)
plt.plot(xcoordinates, ycoordinates)
plt.show()
def main():
x1 = int(input("Enter the starting point of x: "))
y1 = int(input("Enter the starting point of y: "))
x2 = int(input("Enter the end point of x: "))
y2 = int(input("Enter the end point of y: "))
bres(x1, y1, x2, y2)
if __name__ == "__main__":
main()