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

Output

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

17 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..!!!

    Reply
      1. Code Wizard

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

        Reply
        1. Rekha Singla

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

          regards
          your dad

          Reply
    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: http://www.codeblocks.org/downloads/5). 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:
      -lbbgi
      -lgdi32
      -lcomdlg32
      -lole32
      -loleaut32
      -luuid

      …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> https://www.cs.colorado.edu/~main/bgi/dev-c++/

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

      Reply
  2. abhijit

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

    Reply
  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!
    Thanks!

    Reply
  4. Mike

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

    while(x<x1)

    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.

    Reply
  5. Harsh Pandit

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

    Reply
  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

    Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.