04.09.2019

Bresenham Line Drawing Algorithm In C

Jul 28, 2017  The Bresenham’s line drawing algorithm constructs a straight line using close approximation between the points on an n-dimensional bitmap image. It was actually developed to sketch lines on digital plotters but due to its extensibility and versatility, it was found to be useful for computer graphics as well.

  1. Line Algorithm In Computer Graphics

Given coordinate of two points A(x1, y1) and B(x2, y2). The task to find all the intermediate points required for drawing line AB on the computer screen of pixels. Note that every pixel has integer coordinates.

Examples:

Below are some assumptions to keep algorithm simple.

  1. We draw line from left to right.
  2. x1 < x2 and y1< y2
  3. Slope of the line is between 0 and 1. We draw a line from lower left to upper right.

Let us understand the process by considering the naive way first.

Above algorithm works, but it is slow. The idea of Bresenham’s algorithm is to avoid floating point multiplication and addition to compute mx + c, and then computing round value of (mx + c) in every step. In Bresenham’s algorithm, we move across the x-axis in unit intervals.

  1. We always increase x by 1, and we choose about next y, whether we need to go to y+1 or remain on y. In other words, from any position (Xk, Yk) we need to choose between (Xk + 1, Yk) and (Xk + 1, Yk + 1).
  2. We would like to pick the y value (among Yk + 1 and Yk) corresponding to a point that is closer to the original line.

We need to a decision parameter to decide whether to pick Yk + 1 or Yk as next point. The idea is to keep track of slope error from previous increment to y. If the slope error becomes greater than 0.5, we know that the line has moved upwards one pixel, and that we must increment our y coordinate and readjust the error to represent the distance from the top of the new pixel – which is done by subtracting one from error.

How to avoid floating point arithmetic
The above algorithm still includes floating point arithmetic. To avoid floating point arithmetic, consider the value below value m.

m = (y2 – y1)/(x2 – x1)

We multiply both sides by (x2 – x1)

We also change slope_error to slope_error * (x2 – x1). To avoid comparison with 0.5, we further change it to slope_error * (x2 – x1) * 2.

Also, it is generally preferred to compare with 0 than 1.

The initial value of slope_error_new is 2*(y2 – y1) – (x2 – x1). Refer this for proof of this value

Below is the implementation of above algorithm.

C++

// Assumptions :
// 2) x1 < x2 and y1 < y2
// We draw a line from lower left to upper
#include<bits/stdc++.h>
voidbresenham(intx1, inty1, intx2, inty2)
intm_new = 2 * (y2 - y1);
for(intx = x1, y = y1; x <= x2; x++)
cout << '(' << x << ',' << y << ')n';
// Add slope to increment angle formed
// increment y and update slope error.
{
slope_error_new -= 2 * (x2 - x1);
}
intmain()
intx1 = 3, y1 = 2, x2 = 15, y2 = 5;
return0;

Java

// Assumptions :
// 2) x1 < x2 and y1 < y2
// We draw a line from lower left to upper
classGFG
// function for line generation
inty2)
intm_new = 2* (y2 - y1);
{
slope_error_new += m_new;
// Slope error reached limit, time to
if(slope_error_new >= 0)
y++;
}
}
// Driver code
{
bresenham(x1, y1, x2, y2);
}
// This code is contributed by Anant Agarwal.

Python3

# Python 3 program for Bresenham’s Line Generation
# 1) Line is drawn from left to right.
# 3) Slope of the line is between 0 and 1.
# right.
defbresenham(x1,y1,x2, y2):
m_new =2*(y2 -y1)
forx inrange(x1,x2+1):
print('(',x ,',',y ,')n')
# Add slope to increment angle formed
# increment y and update slope error.
y=y+1
# driver function
x1 =3
x2 =15
bresenham(x1, y1, x2, y2)
#This code is contributed by ash264

C#

// Assumptions :
// 2) x1 < x2 and y1< y2
// We draw a line from lower left to upper
usingSystem;
classGFG {
// function for line generation
inty2)
intslope_error_new = m_new - (x2 - x1);
for(intx = x1, y = y1; x <= x2; x++)
Console.Write('(' + x + ',' + y + ')n');
// Add slope to increment angle formed
// increment y and update slope error.
{
slope_error_new -= 2 * (x2 - x1);
}
publicstaticvoidMain ()
intx1 = 3, y1 = 2, x2 = 15, y2 = 5;
bresenham(x1, y1, x2, y2);
}
// This code is contributed by nitin mittal.

PHP

// PHP program for Bresenham’s
// left to right.
// 3) Slope of the line is
// We draw a line from lower
functionbresenham($x1, $y1, $x2, $y2)
$m_new= 2 * ($y2- $y1);
for($x= $x1, $y= $y1; $x<= $x2; $x++)
echo'(',$x, ',', $y, ')n';
// Add slope to increment
$slope_error_new+= $m_new;
// Slope error reached limit,
// update slope error.
{
$slope_error_new-= 2 * ($x2- $x1);
}
$x1= 3; $y1= 2; $x2= 15; $y2= 5;
?>

Output :

The above explanation is to provides a rough idea behind the algorithm. For detailed explanation and proof, readers can refer below references.

Related Articles:

Reference :
https://csustan.csustan.edu/~tom/Lecture-Notes/Graphics/Bresenham-Line/Bresenham-Line.pdf
https://en.wikipedia.org/wiki/Bresenham’s_line_algorithm
http://graphics.idav.ucdavis.edu/education/GraphicsNotes/Bresenhams-Algorithm.pdf

Line Algorithm In Computer Graphics

This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Recommended Posts:


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.

The method works as follows:

Assume a pixel P1'(x1',y1'),then select subsequent pixels as we work our may to the night, one pixel position at a time in the horizontal direction toward P2'(x2',y2').

Once a pixel in choose at any step

The next pixel is

  1. Either the one to its right (lower-bound for the line)
  2. One top its right and up (upper-bound for the line)

The line is best approximated by those pixels that fall the least distance from the path between P1',P2'.


To chooses the next one between the bottom pixel S and top pixel T.
If S is chosen
We have xi+1=xi+1 and yi+1=yi
If T is chosen
We have xi+1=xi+1 and yi+1=yi+1

The actual y coordinates of the line at x = xi+1is
y=mxi+1+b


The distance from S to the actual line in y direction
s = y-yi

The distance from T to the actual line in y direction
t = (yi+1)-y

Now consider the difference between these 2 distance values
s - t

When (s-t) <0 ⟹ s < t

The closest pixel is S

When (s-t) ≥0 ⟹ s < t

The closest pixel is T

This difference is
s-t = (y-yi)-[(yi+1)-y]
= 2y - 2yi -1


Substituting m by and introducing decision variable
di=△x (s-t)
di=△x (2 (xi+1)+2b-2yi-1)
=2△xyi-2△y-1△x.2b-2yi△x-△x
di=2△y.xi-2△x.yi+c

Where c= 2△y+△x (2b-1)

We can write the decision variable di+1 for the next slip on
di+1=2△y.xi+1-2△x.yi+1+c
di+1-di=2△y.(xi+1-xi)- 2△x(yi+1-yi)

Since x_(i+1)=xi+1,we have
di+1+di=2△y.(xi+1-xi)- 2△x(yi+1-yi)

Special Cases

If chosen pixel is at the top pixel T (i.e., di≥0)⟹ yi+1=yi+1
di+1=di+2△y-2△x

If chosen pixel is at the bottom pixel T (i.e., di<0)⟹ yi+1=yi
di+1=di+2△y

Finally, we calculate d1
d1=△x[2m(x1+1)+2b-2y1-1]
d1=△x[2(mx1+b-y1)+2m-1]

Bresenham Line Drawing Algorithm In C

Since mx1+b-yi=0 and m = , we have
d1=2△y-△x

Advantage:

1. It involves only integer arithmetic, so it is simple.

2. It avoids the generation of duplicate points.

3. It can be implemented using hardware because it does not use multiplication and division.

4. It is faster as compared to DDA (Digital Differential Analyzer) because it does not involve floating point calculations like DDA Algorithm.

Disadvantage:

1. This algorithm is meant for basic line drawing only Initializing is not a part of Bresenham's line algorithm. So to draw smooth lines, you should want to look into a different algorithm.

Bresenham's Line Algorithm:

Step1: Start Algorithm

Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy

Step3: Enter value of x1,y1,x2,y2
Where x1,y1are coordinates of starting point
And x2,y2 are coordinates of Ending point

Step4: Calculate dx = x2-x1
Calculate dy = y2-y1
Calculate i1=2*dy
Calculate i2=2*(dy-dx)
Calculate d=i1-dx

Step5: Consider (x, y) as starting point and xendas maximum possible value of x.
If dx < 0
Then x = x2
y = y2
xend=x1
If dx > 0
Then x = x1
y = y1
xend=x2

Step6: Generate point at (x,y)coordinates.

Step7: Check if whole line is generated.
If x > = xend
Stop.

Step8: Calculate co-ordinates of the next pixel
If d < 0
Then d = d + i1
If d ≥ 0
Then d = d + i2
Increment y = y + 1

Step9: Increment x = x + 1

Step10: Draw a point of latest (x, y) coordinates

Step11: Go to step 7

Step12: End of Algorithm

Example: Starting and Ending position of the line are (1, 1) and (8, 5). Find intermediate points.

Solution: x1=1
y1=1
x2=8
y2=5
dx= x2-x1=8-1=7
dy=y2-y1=5-1=4
I1=2* ∆y=2*4=8
I2=2*(∆y-∆x)=2*(4-7)=-6
d = I1-∆x=8-7=1

xyd=d+I1 or I2
11d+I2=1+(-6)=-5
22d+I1=-5+8=3
32d+I2=3+(-6)=-3
43d+I1=-3+8=5
53d+I2=5+(-6)=-1
64d+I1=-1+8=7
74d+I2=7+(-6)=1
85

Program to implement Bresenham's Line Drawing Algorithm:

Output:

Differentiate between DDA Algorithm and Bresenham's Line Algorithm:

DDA AlgorithmBresenham's Line Algorithm
1. DDA Algorithm use floating point, i.e., Real Arithmetic.1. Bresenham's Line Algorithm use fixed point, i.e., Integer Arithmetic
2. DDA Algorithms uses multiplication & division its operation2.Bresenham's Line Algorithm uses only subtraction and addition its operation
3. DDA Algorithm is slowly than Bresenham's Line Algorithm in line drawing because it uses real arithmetic (Floating Point operation)3. Bresenham's Algorithm is faster than DDA Algorithm in line because it involves only addition & subtraction in its calculation and uses only integer arithmetic.
4. DDA Algorithm is not accurate and efficient as Bresenham's Line Algorithm.4. Bresenham's Line Algorithm is more accurate and efficient at DDA Algorithm.
5.DDA Algorithm can draw circle and curves but are not accurate as Bresenham's Line Algorithm5. Bresenham's Line Algorithm can draw circle and curves with more accurate than DDA Algorithm.