Table of contents
1.
Introduction
2.
What is Line Drawing Algorithm?
3.
What is the Bresenham Line Drawing Algorithm?
4.
Deriving the Bresenham Line Drawing Algorithm
4.1.
Step Wise Algorithm
5.
Code Implementation of Bresenham Line Drawing Algorithm
5.1.
C++
5.2.
Java
5.3.
Python
5.4.
JS
5.5.
C#
6.
Advantages of the Bresenham Line Drawing Algorithm
7.
Disadvantages of the Bresenham Line Drawing Algorithm
8.
Difference between DDA Algorithm and Bresenham's Line Algorithm
9.
Frequently Asked Questions
9.1.
What is the Bresenham line drawing algorithm?
9.2.
Why is Bresenham better than DDA?
9.3.
What is Bresenham's circle drawing algorithm?
10.
Conclusion
Last Updated: Feb 15, 2025
Medium

Bresenham Line Drawing Algorithm

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Bresenham Line drawing algorithm is used for scan converting a line. It is an efficient method because it involves integer addition, subtraction, and multiplication operations. Because these actions may be completed quickly, lines can be generated quickly. It selects points on a grid to create a close approximation of a straight line between two points.

Bresenham's Line Drawing Algorithm stands as a hallmark in computer graphics, revered for its efficiency and elegance in rendering straight lines on raster displays. In this blog, we will understand this algorithm, exploring its origins, principles, and applications. 

bresenham line drawing algorithm

One of the most commonly used line drawing algorithms, Bresenham's algorithm, will be covered in this article.

What is Line Drawing Algorithm?

A line drawing algorithm is a technique for predicting a line segment on discrete visual media, like computer graphics printers and screens with pixel-based resolution. The following algorithms are used to draw lines:

  • DDA (Digital Differential Analyzer) Line Drawing Algorithm: A digital differential algorithm is a technique for gradual conversion. Another feature that sets this method apart is the use of the results from the previous stage in each computation
  • Bresenham Line Drawing Algorithm: This algorithm helps with line scan conversion. It's a reliable, practical, and effective approach. Additionally, we use incremental integer calculations to draw lines. Integer computations include addition, subtraction, and multiplication

What is the Bresenham Line Drawing Algorithm?

The Bresenham Line Drawing Algorithm is a fundamental algorithm used to draw straight lines on a pixel-based display such as a computer screen. It determines the points of an n-dimensional raster that should be selected to form a close approximation to a straight line between two points. It was developed by Jack E. Bresenham in 1962 and is widely used due to its efficiency and simplicity.

The Bresenham line algorithm is one of the most effective incremental techniques for scan-converting lines. It is an algorithm that chooses the locations of the points in an n-dimensional raster that should be plotted to approximate a straight line between two points. It is a raster line-generating algorithm that is accurate and effective and employs just incremental integer calculations.

Lines, circles, and other curves can be displayed using the Bresenham algorithm. The horizontal axes identify pixel columns, while the vertical axes show scan-line positions, demonstrating how the Bresenham technique can be used to draw lines on a computer screen, subtract data, and shift bits.

Deriving the Bresenham Line Drawing Algorithm

Deriving the Bresenham Line Drawing Algorithm

The vertical separations from the mathematical line at sample location xk +1 are denoted by the letters d_upper and d_lower. The y coordinate on the line at xk +1 is:

y = m * (xk + 1) + b

So, d_upper and d_lower are given as follows:

d_lower = y - yk

               = m * (xk + 1) + b - yk

d_upper = (yk + 1) - y

                = yk + 1 - m * (xk +1) - b
 

These can be used to make a simple decision on which pixel is nearer the mathematical line.

The difference between the two-pixel places served as the basis for this straightforward decision:

d_lower - d_upper = 2m * (xk +1) - 2yk + 2b -1
 

Let’s substitute m with ∆y/∆x where ∆x and ∆y are the differences between the endpoints:

∆x * (d_lower - d_upper) = ∆x * ( (2∆y / ∆x) * (xk + 1) - 2yk) + (2b - 1)

                                            = 2∆y.xk - 2∆x.yk + 2∆y + ∆x * (2b - 1)

                                            = 2∆y.xk - 2∆x.yk + c
 

So, at the kth step along a line, a decision parameter pk is given by:

pk = ∆x * (d_upper - d_lower)

     = 2∆y.xk - 2∆x.yk + c
 

The sign of the decision parameter pk and that of (d_lower – d_upper) is the same.

If pk is negative, we select the lower pixel; otherwise, we select the upper pixel.

Because coordinate changes along the x-axis occur in unit steps, we can handle everything with integer calculations.

The decision parameter is given at step k+1 as:

pk+1 = 2∆y.(xk+1) - 2∆x.(yk+1) + c
 

Subtracting pk from this, we get:

pk+1 - pk = 2∆y.(xk+1 - xk) - 2∆x * (yk+1 - yk)
 

But, xk+1 is the same as xk +1, so:

pk+1 = pk +2∆y - 2∆x * (yk+1 - yk)
 

Where yk+1 - yk is either 0 or 1 depending on the sign of pk

The initial decision parameter p0 is evaluated at (x0, y0) and is written as follows:

p0 = 2∆y - ∆x

Step Wise Algorithm

The step-wise Bresenham’s line drawing algorithm is given below:

  • Input the two endpoints of the line, save the left endpoint in (x0 , y0)
     
  • Plot the point (x0 , y0)
     
  • Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and obtain the first value for the decision parameter as:
    p0 = 2Δy - Δx
     
  • Perform the following test at each xk along the line, beginning at k = 0. If pk < 0, then the next point to the plot is (xk+1 ,  yk ) and:
    pk+1 = pk + 2Δy
    Otherwise, the next point to plot is (xk+1 , yk+1) and: 
    pk+1 = pk + 2Δy - 2Δx
     
  •  Repeat step 4, (Δx – 1) times

Code Implementation of Bresenham Line Drawing Algorithm

The C++ code implementation of the Bresenham Line Drawing Algorithm is given below:

  • C++
  • Java
  • Python
  • JS
  • C#

C++

#include <bits/stdc++.h>
using namespace std;

// function for printing the points needed for line generation
void bresenham_line_algo(int a1, int b1, int a2, int b2)
{
int m_new = 2 * (b2 - b1);
int slope = m_new - (a2 - a1);
for (int x = a1, y = b1; x <= a2; x++) {
cout << "(" << x << "," << y << ")\n";
// Adding slope to increment angle
slope += m_new;
//Slope error has reached its limit; it is time to increase y and update the slope error
if (slope >= 0) {
y++;
slope -= 2 * (a2 - a1);
}
}
}

int main()
{
int a1 = 5, b1 = 10, a2 = 15, b2 = 8;
// calling Bresenham Line Algorithm function
bresenham_line_algo(a1, b1, a2, b2);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Java

public class BresenhamLine {
public static void bresenhamLineAlgo(int a1, int b1, int a2, int b2) {
int m_new = 2 * (b2 - b1);
int slope = m_new - (a2 - a1);

for (int x = a1, y = b1; x <= a2; x++) {
System.out.println("(" + x + "," + y + ")");
slope += m_new;

if (slope >= 0) {
y++;
slope -= 2 * (a2 - a1);
}
}
}

public static void main(String[] args) {
int a1 = 5, b1 = 10, a2 = 15, b2 = 8;
bresenhamLineAlgo(a1, b1, a2, b2);
}
}
You can also try this code with Online Java Compiler
Run Code

Python

def bresenham_line_algo(a1, b1, a2, b2):
m_new = 2 * (b2 - b1)
slope = m_new - (a2 - a1)

x, y = a1, b1
while x <= a2:
print(f"({x},{y})")
slope += m_new

if slope >= 0:
y += 1
slope -= 2 * (a2 - a1)

x += 1

# Driver code
a1, b1, a2, b2 = 5, 10, 15, 8
bresenham_line_algo(a1, b1, a2, b2)
You can also try this code with Online Python Compiler
Run Code

JS

function bresenhamLineAlgo(a1, b1, a2, b2) {
let m_new = 2 * (b2 - b1);
let slope = m_new - (a2 - a1);

for (let x = a1, y = b1; x <= a2; x++) {
console.log(`(${x},${y})`);
slope += m_new;

if (slope >= 0) {
y++;
slope -= 2 * (a2 - a1);
}
}
}

// Driver code
let a1 = 5, b1 = 10, a2 = 15, b2 = 8;
bresenhamLineAlgo(a1, b1, a2, b2);
You can also try this code with Online Javascript Compiler
Run Code

C#

using System;

class BresenhamLine
{
static void BresenhamLineAlgo(int a1, int b1, int a2, int b2)
{
int m_new = 2 * (b2 - b1);
int slope = m_new - (a2 - a1);

for (int x = a1, y = b1; x <= a2; x++)
{
Console.WriteLine($"({x},{y})");
slope += m_new;

if (slope >= 0)
{
y++;
slope -= 2 * (a2 - a1);
}
}
}

static void Main()
{
int a1 = 5, b1 = 10, a2 = 15, b2 = 8;
BresenhamLineAlgo(a1, b1, a2, b2);
}
}

In the above code, we assume that the line is drawn from left to right, and a1<a2 and b1<b2. The time complexity of the code is O(a2-a1) and the space complexity is O(1).

Output:

(5, 10)
(6, 11)
(7, 12)
(8, 13)
(9, 14)
(10, 15)
(11, 16)
(12, 17)
(13, 18)
(14, 19)
(15, 20)

Advantages of the Bresenham Line Drawing Algorithm

The benefits of the Bresenham Line Drawing Algorithm are as follows:

  • The implementation of the Bresenham algorithm is simple and does not require a large amount of buffer memory
  • The working of this algorithm is faster and more stepwise, so it's easy to understand
  • You can draw the line in any direction in the program by using the Bresenham algorithm
  • It can be implemented using hardware because it does not require multiplication and division

Disadvantages of the Bresenham Line Drawing Algorithm

Some of the disadvantages of the Bresenham algorithm are given below:

  • Even if it enhances the precision of the generated points, the resulting line could be smoother
  • This algorithm does simple line drawing, which means it is unable to draw curves and other complex shapes
  • The algorithm can only draw one-pixel-thick lines. This constraint can be an issue when thicker lines are required

Difference between DDA Algorithm and Bresenham's Line Algorithm

S.No.DDA AlgorithmBresenham's Algorithm
1The only operations used in the DDA algorithm are division and multiplication.In Bresenham's algorithm, only addition and subtraction are used.
2DDA Algorithm doesn't have a high level of precision or accuracy.Bresenham's Algorithm is highly precise and accurate.
3DDA Algorithm relies on complicated calculations to fulfill its tasks.Bresenham's Algorithm uses basic calculations to do its tasks.
4The calculation speed of the DDA method is slower than that of the Bresenham line algorithm.The computation speed of the Bresenham line algorithm is faster than that of the DDA algorithm.
5DDA Algorithm is less efficient compared to the Bresenham line algorithm.Bresenham's Algorithm is more efficient than DDA algorithm.
6DDA Algorithm is much costly.Bresenham's Algorithm is not much costly.
7Optimization is not available in the DDA algorithm.Optimization is available in Bresenham's algorithm.

Frequently Asked Questions

What is the Bresenham line drawing algorithm?

Bresenham's line algorithm is an efficient raster graphics algorithm that uses only integer calculations to determine points for a straight-line path.

Why is Bresenham better than DDA?

Bresenham’s algorithm is faster and more accurate than DDA as it avoids floating-point operations, using only integer arithmetic for better performance and precision.

What is Bresenham's circle drawing algorithm?

Bresenham’s circle algorithm efficiently plots a circle using integer calculations and symmetry, minimizing computations while ensuring accurate pixel placements on a raster display.

Conclusion

We have discussed the Bresenham line drawing algorithm. This algorithm stands as a testament to the ingenuity and efficiency of computer graphics algorithms. Its elegance lies in its ability to render straight lines with precision and speed, making it a cornerstone in the field of raster graphics

Live masterclass