Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f0d293d

Browse files
author
Amogh Singhal
authored
Update bresenham_line_algorithm.py
1 parent 4fe0e73 commit f0d293d

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

‎bresenham_line_algorithm.py‎

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# Bresenham Line Algorithm (BLA) is one of the earliest algorithms developed
2+
# in computer graphics. It is used for drawing lines. It is an efficient method because
3+
# it involves only integer addition, subtractions, and multiplication operations.
4+
5+
# These operations can be performed very rapidly so lines can be generated quickly.
6+
7+
# Reference: http://floppsie.comp.glam.ac.uk/Southwales/gaius/gametools/6.html
8+
9+
# Algorithm:
10+
# 1. We are given the starting and ending point (x1, y1) and (x2, y2)
11+
# 2. We compute the gradient m, using the formula: m = (y2-y1)/(x2-x1)
12+
# 3. The equation of the straight line is y = m*x+c. So the next thing we need to find is the intercept c
13+
# 4. Intercept can be derived using the formula c = y1 - m*x1
14+
# 5. To get the next point, we add dx to the x-cordinate and dy to the y cordinate
15+
116
def lineGenerator(x1, y1, x2, y2):
217
dx = x2 - x1
318
dy = y2 - y1
@@ -26,25 +41,25 @@ def lineGenerator(x1, y1, x2, y2):
2641
slope = slope
2742

2843

29-
lineGenerator(3, 2, 15, 5)
44+
# lineGenerator(3, 2, 15, 5)
3045

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]
46+
# # P1 is the point given. Initial point
47+
# # P2 is the point to reach. Final point
48+
# if P1[0] == P2[0] and P1[1] == P2[1]:
49+
# return 0
50+
# print(P1)
51+
# #Check if the point is above or below the line.
52+
# dx = P2[0]-P1[0]
53+
# dy = P2[1]-P1[0]
3954

40-
# di = 2*dy - dx
55+
# di = 2*dy - dx
4156

42-
# currX = P1[0]
43-
# currY = P1[1]
57+
# currX = P1[0]
58+
# currY = P1[1]
4459

45-
# if di > 0:
46-
# P1 = (currX+1, currY+1)
47-
# else:
48-
# P1 = (currX+1, currY)
60+
# if di > 0:
61+
# P1 = (currX+1, currY+1)
62+
# else:
63+
# P1 = (currX+1, currY)
4964

50-
# return lineGenerator(P1, P2)
65+
# return lineGenerator(P1, P2)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /