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
+
1
16
def lineGenerator (x1 , y1 , x2 , y2 ):
2
17
dx = x2 - x1
3
18
dy = y2 - y1
@@ -26,25 +41,25 @@ def lineGenerator(x1, y1, x2, y2):
26
41
slope = slope
27
42
28
43
29
- lineGenerator (3 , 2 , 15 , 5 )
44
+ # lineGenerator(3, 2, 15, 5)
30
45
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]
39
54
40
- # di = 2*dy - dx
55
+ # di = 2*dy - dx
41
56
42
- # currX = P1[0]
43
- # currY = P1[1]
57
+ # currX = P1[0]
58
+ # currY = P1[1]
44
59
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)
49
64
50
- # return lineGenerator(P1, P2)
65
+ # return lineGenerator(P1, P2)
0 commit comments