Bresenham's Line Algorithm
This algorithm is used for scan converting a line. It was developed by Bresenham. It is an efficient method because it involves only integer addition, subtractions, and multiplication operations. These operations can be performed very rapidly so lines can be generated quickly.
In this method, next pixel selected is that one who has the least distance from true line.
2. • Digital Differential Analyzer (DDA) Algorithm
a. Working Principle
b. Examples
• Bresenham’s Line Drawing Algorithm (BSA)
a. Working Principle
b. Examples
Contents
AASHISH ADHIKARI [mail@aashishadhikari.info.np]
3. Working Principle of DDA Algorithm
Algorithm
Step 1: Input the line endpoints and store the left endpoint in (x0, y0) and right endpoint in (x1, y1).
Step 2: Calculate the values of Δx and Δy,
Δx = x1 – x0, Δy = y1 – y0
Step 3: Based on the calculated difference in step 2, now we need to identify the number of steps
to put pixel as follows:
If Δx > Δy, then we need more steps in x co-ordinate; otherwise in y
coordinate.
I.e. if (absolute(Δx) > absolute (Δy) ), then steps = absolute(Δx) else steps =
absolute(Δy)
Step 4: We calculate the increment in x coordinate and y coordinate as follows:
xincreament =
Δx
𝑆𝑡𝑒𝑝𝑠 𝑓𝑙𝑜𝑎𝑡
Yincreament =
Δy
𝑆𝑡𝑒𝑝𝑠 𝑓𝑙𝑜𝑎𝑡
Step 5: Perform round off, and plot each calculated (x, y) i.e. Plot(round(x), round(y)).
Step 6: END
Definition: The Digital Differential Analyzer(DDA) is a scan conversion line drawing algorithm based on calculating
either Δx or Δy from the equation m = Δy/Δx.
AASHISH ADHIKARI [mail@aashishadhikari.info.np]
6. Working Principle of Bresenham’s Line Drawing
Algorithm (BSA) [for positive slope questions]
Definition: An accurate and efficient raster line-drawing algorithm, developed by Bresenham‘s. It only uses integer
calculations so can be adapted to display circles and other curves.
AlgorithmAlgorithm
Step 1: Input the line endpoints and store the left
endpoint in (x0, y0) and right endpoint in (x1, y1).
Step 2: Calculate the values of Δx and Δy,
Δx = abs(x1 – x0), Δy = abs(y1 – y0)
Step 3: Calculate initial decision parameter as,
p = 2 Δy- Δx
Step 4: if x1 > x0 then
Set x= x0
Set y=y0
Set xend=x1
Otherwise
Set x= x1
Set y=y1
Set xend=x0
Step 5: Draw pixel at (x,y)
Step 6: while(x<xend)
x++;
if p<0 then
p=p+2Δy
Otherwise
y++
p=p+2Δy-2Δx
Draw pixel at (x,y)
Step 7: END
AASHISH ADHIKARI [mail@aashishadhikari.info.np]
7. Example 1 : ( For m<=1 OR Δx>Δy)
* Digitize a line with end points (10,15) to (15,18) using BSA
Solution:
Given,
(x0, y0) = ( 10,15)
(x1, y1) = ( 15,18)
Δx = x1 – x0 = |15-10| = 5
Δy = y1 – y0 = |18-15| = 3
Since, Slope m=
Δy
Δx
<=1
So, we sample at x direction i.e., at each step we simply increment x-coordinate by 1 and find appropriate y-coordinate.
First pixel to plot is (10,15), which is the starting endpoint.
Now, initial decision parameter, p0 = 2 Δy- Δx = 2*3-5 = 1
We know that,
If pk < 0, then we need to set pk+1 = pk+2△y and plot pixel(xk+1, yk)
And if pk ≥ 0, then we need to set pk+1 = pk + 2 △y – 2 △x then plot pixel(xk+1, yk+1)
Here p0 = 1, we have i.e., pk ≥0
So,
p1 = p0 + 2 △y – 2 △x = 1 + 2 * 3 – 2 x 5 = -3
p2 = p1+ 2 △y = -3+2 * 3 = 3.
p3 = p2 + 2 △y – 2 △x = 3+6-10 = -1.
p4 = p3 + 2 △y = -1+ 6 = 5
Successive pixel positions and decision parameter calculation is shown in the table.
AASHISH ADHIKARI [mail@aashishadhikari.info.np]
8. Working Principle of Bresenham’s Line Drawing
Algorithm (BSA) [for negative slope questions]
Algorithm
Step 1: Input the line endpoints and store the left endpoint in (x0, y0) and right endpoint in (x1, y1).
Step 2: Calculate the values of Δx and Δy,
Δx = abs(x1 – x0),
Δy = abs(y1 – y0)
Step 3: Calculate initial decision parameter as,
pk = 2 Δy- Δx
Step 4: for (i=1 to Δx)
{
set pixel (xs, ys)
while (pk>0)
y0=y0-1;
pk= pk-2 Δx
end while, (pk>0)
x0=x0+1
pk= pk+2 Δy
i++;
}
set pixel (x0, y0)
AASHISH ADHIKARI [mail@aashishadhikari.info.np]
9. Example 2 : ( For m>=1 OR Δx>Δy)
* Digitize a line with end points (6,12) to (10,5) using BSA
Solution:
Given,
(x0, y0) = ( 6,12), (x1, y1) = ( 10,05)
Now, We calculate the Δx and Δy as follows:
Δx = x1 – x0 = |10-6| = 4, Δy = y1 – y0 = |5-12| = 7
Since, Slope m=
Δy
Δx
>=1
So, we sample at x direction i.e., at each step we simply increment x-coordinate by 1 and find appropriate y-coordinate.
First pixel to plot is (10,15), which is the starting endpoint.
Now, initial decision parameter, p0 = 2 Δy- Δx = 2*7-4 = 10
For (i=1 to 4) using the condition we proceed as follows:
For i=1
set pixel (6,12) while (10>0)
y0= 12-1 = 11
pk= 10-2*4 = 2
while (2>0)
y0= 11-1 =10
pk= 2-2*4 = -6
end while
x0= x0+1 = 6+1 = 7
pk = pk+ 2 Δy
= -6+2*7 = 8
Successive pixel positions and decision parameter calculation is shown in the table.
I X0 Y0 Pk Points
1 6 12 10 (7,10)
2 7 10 8 (8,9)
3 8 9 14 (9,7)
4 9 7 12 (10,5)
AASHISH ADHIKARI [mail@aashishadhikari.info.np]
10. Working Principle of Bresenham’s Line Drawing Algorithm (BSA) [for negative
slope questions]
* Digitize a line with end points (6,12) to (10,5) using BSA
( For m>=1 OR Δx>Δy)
Alternative Method
AASHISH ADHIKARI [mail@aashishadhikari.info.np]