Skip to content

Commit 144ab33

Browse files
committed
Bresenham Algorithm
1 parent 010f349 commit 144ab33

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

bresenham_line_algorithm.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
def lineGenerator(x1, y1, x2, y2):
2+
dx = x2 - x1
3+
dy = y2 - y1
4+
5+
slope = 2*dy - dx
6+
7+
x = x1
8+
y = y1
9+
while x < x2:
10+
11+
#Print current coordinates
12+
print(x, y)
13+
14+
#X increases any ways
15+
x+= 1
16+
17+
# 2dy is always added in the slope. Do it.
18+
slope += 2*dy
19+
#Check for the current slope
20+
if slope >= 0:
21+
y += 1
22+
slope -= 2 * (x2-x1)
23+
24+
elif slope <=0:
25+
#No changes are made.
26+
slope = slope
27+
28+
29+
lineGenerator(3, 2, 15, 5)
30+
31+
# if P1[0]==P2[0] and P1[1]==P2[1]:
32+
# return 0
33+
# #P1 is the point given. Initial point
34+
# #P2 is the point to reach. Final point
35+
# print(P1)
36+
# #Check if the point is above or below the line.
37+
# dx = P2[0]-P1[0]
38+
# dy = P2[1]-P1[0]
39+
40+
# di = 2*dy - dx
41+
42+
# currX = P1[0]
43+
# currY = P1[1]
44+
45+
# if di > 0:
46+
# P1 = (currX+1, currY+1)
47+
# else:
48+
# P1 = (currX+1, currY)
49+
50+
# return lineGenerator(P1, P2)

0 commit comments

Comments
 (0)