Bresenham’s Line Drawing Algorithm
Bresenham’s Line Drawing Algorithm is one of the most important algorithms in computer graphics for drawing a straight line between two given points. In a graphical display system, a line is mathematically continuous, but the screen is made up of discrete pixels. Therefore, the primary objective of this algorithm is to convert a continuous line into a sequence of discrete pixel positions that best represent the actual line.
The fundamental idea behind Bresenham’s algorithm is to determine, at each step of the drawing process, which pixel lies closest to the true mathematical line. Instead of calculating exact real-number positions, the algorithm incrementally selects the most appropriate pixel based on a decision parameter that measures the deviation from the ideal line.
A key characteristic of Bresenham’s algorithm is that it uses only integer calculations throughout the process. This eliminates the need for floating-point arithmetic, making the algorithm faster and more efficient compared to methods like the DDA algorithm. Because of this efficiency and accuracy, Bresenham’s algorithm is widely used in raster graphics systems for line generation.
When drawing a line:
- We move step-by-step along x-direction (for gentle slope)
- At each step, we decide:
Should we go:
- Straight (E → x+1, y) OR
- Diagonal (NE → x+1, y+1)
This decision is made using a decision parameter Pk
Algorithm (Standard Case: 0 ≤ m ≤ 1)
Step 1:
Calculate ΔX and ΔY from the given input.
These parameters are calculated as:
- ΔX = Xn – X0
- ΔY =Yn – Y0
Step 2:
Calculate the decision parameter Pk.
It is calculated as: Pk = 2ΔY – ΔX
Step 3:
Suppose the current point is (Xk, Yk) and the next point is (Xk+1, Yk+1).
Find the next point depending on the value of decision parameter Pk.
Case 1: If Pk < 0
Choose E (East)
- Xk+1 = Xk+1
- Yk+1 = Yk
- Pk+1 = Pk + 2ΔY
Case 2: If Pk ≥ 0
Choose NE (North-East)
- Xk+1 = Xk+1
- Yk+1 = Yk+1
- Pk+1 = Pk + 2ΔY − 2ΔX
Step 6: Repeat
Repeat until End point reached or Iterations = ΔX - 1.
Numerical Example
Problem 1
Calculate the points between the starting coordinates (9, 18) and ending coordinates (14, 22).
Solution:
Given,
- Starting coordinates = (X0, Y0) = (9, 18)
- Ending coordinates = (Xn, Yn) = (14, 22)
Step 1:
Calculate ΔX and ΔY from the given input.
- ΔX = Xn – X0 = 14 – 9 = 5
- ΔY =Yn – Y0 = 22 – 18 = 4
Step 2:
Calculate the decision parameter.
- Pk = 2ΔY – ΔX
- Pk = 2 x 4 – 5
- Pk = 3
So, decision parameter Pk = 3
Step 3:
As Pk >= 0, so Case: 2 is satisfied.
Thus,
- Pk+1 = Pk + 2ΔY – 2ΔX = 3 + (2 x 4) – (2 x 5) = 1
- Xk+1 = Xk + 1 = 9 + 1 = 10
- Yk+1 = Yk + 1 = 18 + 1 = 19
Similarly, Step: 3 is executed until the end point is reached or number of iterations equals to 4 times.
(Number of iterations = ΔX – 1 = 5 – 1 = 4)
Table:
| Step | Pk | Pk+1 | XK+1 | YK+1 |
|---|---|---|---|---|
| Start | — | — | 9 | 18 |
| 1 | 3 | 1 | 10 | 19 |
| 2 | 1 | -1 | 11 | 20 |
| 3 | -1 | 7 | 12 | 20 |
| 4 | 7 | 5 | 13 | 21 |
| 5 | 5 | 3 | 14 | 22 |
Final points: (9, 18), (10, 19), (11, 20), (12, 20), (13, 21), (14, 22)
Problem 2:
Calculate the points between the starting coordinates (20, 10) and ending coordinates (30, 18).
Solution:
Given,
- Starting coordinates = (X0, Y0) = (20, 10)
- Ending coordinates = (Xn, Yn) = (30, 18)
Step 1:
Calculate ΔX and ΔY from the given input.
- ΔX = Xn – X0 = 30 – 20 = 10
- ΔY =Yn – Y0 = 18 – 10 = 8
Step 2:
Calculate the decision parameter.
- Pk = 2ΔY – ΔX
- Pk = 2 x 8 – 10
- Pk = 6
So, decision parameter Pk = 6
Step 3:
As Pk >= 0, so case-02 is satisfied.
Thus,
- Pk+1 = Pk + 2ΔY – 2ΔX = 6 + (2 x 8) – (2 x 10) = 2
- Xk+1 = Xk + 1 = 20 + 1 = 21
- Yk+1 = Yk + 1 = 10 + 1 = 11
Similarly, Step 3 is executed until the end point is reached or number of iterations equals to 9 times.
(Number of iterations = ΔX – 1 = 10 – 1 = 9)
Table:
| Step | Pk | Pk+1 | XK+1 | YK+1 |
|---|---|---|---|---|
| Start | — | — | 20 | 10 |
| 1 | 6 | 2 | 21 | 11 |
| 2 | 2 | -2 | 22 | 12 |
| 3 | -2 | 14 | 23 | 12 |
| 4 | 14 | 10 | 24 | 13 |
| 5 | 10 | 6 | 25 | 14 |
| 6 | 6 | 2 | 26 | 15 |
| 7 | 2 | -2 | 27 | 16 |
| 8 | -2 | 14 | 28 | 16 |
| 9 | 14 | 10 | 29 | 17 |
| 10 | 10 | 6 | 30 | 18 |
Problem 3:
To solve the line from (0,0) to (-8,-4) using Bresenham’s Line Drawing Algorithm, we first observe that both x and y are decreasing, so this is a negative direction line.
The slope is:
m = −4−0 / −8−0 = −4 / −8 = 0.5
Since: 0 ≤ m ≤ 1
we can use the standard Bresenham logic, but instead of increasing x and y, we will decrease them.
Step 1: Given Points
Starting Point: (x1, y1) = (0,0)
Ending Point: (x2, y2) = (−8,−4)
Step 2: Calculate Δx and Δy
Δx = |x2 − x1| = |−8 − 0| = 8
Δy = |y2 − y1| = |−4 − 0| = 4
Step 3: Initial Decision Parameter
P0 = 2Δy − Δx = 2(4) − 8 = 2(4) - 8 = 8−8 = 0
Step 4: Apply Bresenham Iterations
Since the line moves toward negative direction:
- x decreases by 1
- y decreases only when Pk ≥ 0
Iteration Table
| Step | PkP_kPk | Condition | Next Point | Pk+1 |
|---|---|---|---|---|
| Start | 0 | — | (0, 0) | — |
| 1 | 0 | Pk ≥ 0 | (-1, -1) | 0 + 8 − 16 = −8 |
| 2 | -8 | Pk < 0 | (-2, -1) | −8 + 8 = 0 |
| 3 | 0 | Pk ≥ 0 | (-3, -2) | −8 |
| 4 | -8 | Pk < 0 | (-4, -2) | 0 |
| 5 | 0 | Pk ≥ 0 | (-5, -3) | −8 |
| 6 | -8 | Pk < 0 | (-6, -3) | 0 |
| 7 | 0 | Pk ≥ 0 | (-7, -4) | −8 |
| 8 | -8 | Pk < 0 | (-8, -4) | End |
Final Plotted Points:
(0,0) (−1,−1) (−2,−1) (−3,−2) (−4,−2) (−5,−3) (−6,−3) (−7,−4) (−8,−4)
Important Concept
The standard Bresenham algorithm normally assumes:
- x increases toward right
But here:
- x decreases toward left
- y decreases downward
So we simply:
- decrement x instead of incrementing
- decrement y when diagonal movement is needed
The decision parameter logic remains exactly the same.
Practice Problems
Try solving these:
- Draw a line from (2, 3) to (9, 6) using Bresenham’s algorithm.
- Consider the line from (5, 5) to (13, 9), use the Bresenham's algorithm to rasterize the line.
- Draw a line from (4, 6) to (15, 10) using Bresenham’s algorithm.
- Draw a line from (20, 10) to (30, 18).
- Draw a line from (10, 6) to (3, 2).
- Draw a line from (2, 2) to (6, 12).
- Consider the line from (0, 0) to (-8, -4), use the Bresenham's algorithm to rasterize the line.
Advantages
- Uses integer arithmetic only
- Faster execution
- More accurate than DDA
- Incremental approach
Disadvantages
- Only basic version handles 0 ≤ m ≤ 1
- Not smooth (aliasing problem)
- Needs modification for other slopes