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++

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

        1. 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. 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. 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.

  1. 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. yes.
      this code works for only limited inputs:
      only when x1 and y1 both are greater than x0 and y0 respectively.

  2. 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.

  3. 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

  4. Kiiza Trevour

    Someone to help, my program has issues it just blinks on running, i have tried to add getch() as recommended above but all in vain, it says function getch() should have a prototype

Leave a Comment

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