Bresenham’s Line Drawing Algorithm in C and C++

Here you will get program for bresenham’s line drawing algorithm in C and C++.

This algorithm is used in computer graphics for drawing line.

The program will work in Turbo C or Turbo C++ compiler as it uses graphics.h header file.

Make sure to change the path of BGI folder inside initgraph() function according to your system. Otherwise the program will not work.

Also Read: Bresenham’s Midpoint Circle Algorithm in C and C++

Program for Bresenham’s Line Drawing Algorithm in C

Program for Bresenham’s Line Drawing Algorithm in C++


Bresenham's Line Drawing Algorithm in C and C++

24 thoughts on “Bresenham’s Line Drawing Algorithm in C and C++

  1. Vishal

    Thank you … I did not understand this the way how it was taught in college..!!!

      1. Code Wizard

        yes It will Compile . you have to give getch() before return 0;

        1. Rekha Singla

          codeblocks doesn’t have graphics.h library
          so it will not compile.
          However you can download which can run graphic lib easily.

          your dad

    1. Jadrankoo

      The answer on james question is: YES, you can compile it under CodeBlocks.. but, let’s read some facts..
      Well, first of all, CodeBlocks is just an IDE (kind of “smart” editor”) – compile process is automaticaly done calling Mingw compiler (in fact GCC) – very powerfull one…
      Graphics in this and similar examples is so called BGI graphics (Borland Graphics Interface), produced in times of DOS…
      Today, in the world of 32/64 bit operating systems and applications you have to install Code Blocks 10.05 (you can find it on: This is most simple way, because it is 32 bit system (very important: you can’t use 64 bit compiler for BGI).
      Then you have to copy file:
      1. graphics.h into folder \CodeBlocks\MinGW\include
      2. libbgi.a into folder \CodeBlocks\MinGW\lib

      And also put this in CodeBlocks Settings – Compiler settings – Linker settings, into write window (Other linker settings) (type it strictly as it is here:

      …and every code you save, writen in C (not C++) – save with extension .cpp (will not work as .c)
      Don’t forget #include …
      ..for more details see>

    1. Koichi

      It has been solved only. Just used up conio on header files and get ch() at endings since the above programmer used int function not void.

  2. abhijit

    please any one help me out in performing data communication lab manual programs of solapur university

  3. Anonymous

    You haven’t put the code for case x1 > x ?
    i.e for example if we give input where x0 is greater than x1.
    Please update the code for above case!

    1. Lakshay Sood

      this code works for only limited inputs:
      only when x1 and y1 both are greater than x0 and y0 respectively.

    1. Wayne Cochran

      The usual fix is to switch the roles of x and y when the slope of the line is more vertical than horizontal.

  4. Mike

    Vishnu is right, this doesn’t work for vertical lines, because of this:


    x is initialised to x0, so it means that x is equal to x1 to start with, meaning the while loop exist straight away, and so the code inside the body of the while loop never gets run.

    I presume if x0 == x1, then you need to do the same drawing method, but using y0 and y1 instead.

  5. Harsh Pandit

    Your program is not solve my problem.There have many errpors in your program

  6. Chandan

    This program is incomplete.
    It is only able to plot the lines where both x or y coordinates increases.
    What if i want to plot a line from (200,200) to (400,100).
    In this case x co-ordinate increases and y co-ordinate decreases.
    Please update the code


Leave a Reply

Your email address will not be published. Required fields are marked *