Bresenham Line
Bresenham Line
SALIM DIWANI
Pseudocode for Bresenham
algorithm:
Bresenham's Line Drawing Algorithm is an efficient algorithm used in
computer graphics to draw straight lines on a raster grid.
The key advantage of this algorithm is that it uses only integer arithmetic
(no floating-point operations), which makes it faster and more efficient,
especially in early computer graphics systems.
2
Overview of Bresenham's :
The algorithm calculates the points along the line between two
endpoints and decides which pixel should be plotted based on the
decision variable. This decision is made using only integer addition
and subtraction.
3
The Basic Idea:
The goal of Bresenham's algorithm is to determine, step-by-step, whether
the next pixel in the line should be horizontally or vertically adjacent, or
diagonally adjacent to the previous pixel.
4
Bresenham algorithm:
5
Bresenham algorithm:
Algorithm Steps
Initialize the starting point:
Let (x1,y1) be the starting point of the line, and (x2,y2) be the ending point.
Calculate the differences: Δx = x2 − x1, Δy = y2 − y1
Set the initial decision parameter p:
PK = 2Δy − Δx
Choose the initial point (x1 , y1)
Plot the first point (x1, y1)
6
Bresenham algorithm:
Case 1
If PK < 0 then
PK + 1 = PK + 2Δy
Xk + 1 = Xk + 1
Yk + 1 = yk
7
Bresenham algorithm:
Case 2
If PK > = 0 then
PK + 1 = PK + 2Δy - 2Δx
Xk + 1 = Xk + 1
Yk + 1 = yk + 1
8
Bresenham algorithm:
Iteratively determine the next point: For each subsequent point along the line:
If PK is negative, the next point is (x+1,y) (horizontal movement).
If PK is positive or zero, the next point is (x+1,y+1) (diagonal movement).
After each step:
If horizontal, update the decision parameter:
PK = PK + 2Δy
If diagonal, update the decision parameter:
PK= PK + 2Δy - 2Δx
9
Bresenham algorithm:
Example: Drawing a Line from (2,3) to (8,6)
Let’s walk through an example where the starting point is (2,3) and the endpoint is (8,6).
10
Bresenham algorithm:
Step 1: Initialize the values
Starting point: (x1,y1) = (2,3)
Ending point: (x2, y2)=(8, 6)
Calculate Δx and Δy
Δx = 8−2 = 6
Δy = 6−3 = 3
Initial decision parameter PK:
PK = 2Δy − Δx = 2(3) − 6 = 6 − 6 = 0
Step 2: Plot the first point
The first point is (2,3).
11
Bresenham algorithm:
Step 3: Iterative calculations
Now, we proceed with the next points.
First iteration: PK = 0
Since PK ≥ 0, we move diagonally to (3,4).
If PK > = 0 then
PK + 1 = PK + 2Δy - 2Δx
Xk + 1 = Xk + 1
Yk + 1 = yk + 1
Update p:
PK = pK + 2(Δy−Δx) =0+2(3−6) =0 − 6 = −6
12
Bresenham algorithm:
Second iteration: p = −6
Since PK < 0 , we move horizontally to (4,4).
If PK < 0 then
PK + 1 = PK + 2Δy
Xk + 1 = Xk + 1
Yk + 1 = yk
Update p: p = p + 2Δy = −6 + 6 = 0
Third iteration: p = 0
Since p ≥ 0, we move diagonally to (5,5).
Update p: p = p + 2(Δy−Δx) = 0+2(3−6) = 0−6 = −6
13
Bresenham algorithm:
Fourth iteration: p = −6
Since PK < 0, we move horizontally to (6,5).
If PK < 0 then
PK + 1 = PK + 2Δy
Xk + 1 = Xk + 1
Yk + 1 = yk
Update PK:
PK = PK + 2Δy =−6 + 6 = 0
14
Bresenham algorithm:
Fifth iteration: p = 0
Since PK ≥ 0, we move diagonally to (7,6).
If PK > = 0 then
PK + 1 = PK + 2Δy - 2Δx
Xk + 1 = Xk + 1
Yk + 1 = yk + 1
Update p: p = p + 2(Δy−Δx) = 0 + 2(3−6) = 0−6 = −6
Sixth iteration: p = −6
Since p < 0, we move horizontally to (8,6).
Update p: p = p + 2Δy = −6 + 6 = 0
15
Bresenham algorithm:
Step 4: Plot all the points
The points we have plotted along the line are:
(2,3)
(3,4)
(4,4)
(5,5)
(6,5)
(7,6)
(8,6)
Thus, the line from (2,3) to (8,6) is successfully drawn using Bresenham’s algorithm.
16
QUIZ
1. Consider a two point, starting (20, 10) and ending points (30, 18),
and generate a Digital Bresenham line drawing algorithm.
2. Consider a two point, starting (9, 18) and ending points (14, 22), and
generate a Digital Bresenham line drawing algorithm.
3. Consider a two point, starting (10, 5) and ending points
(20, 15), and generate a Digital Bresenham line drawing algorithm.
17