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

#include<stdio.h>
#include<graphics.h>

void drawline(int x0, int y0, int x1, int y1)
{
    int dx, dy, p, x, y;

	dx=x1-x0;
	dy=y1-y0;

	x=x0;
	y=y0;

	p=2*dy-dx;

	while(x<x1)
	{
		if(p>=0)
		{
			putpixel(x,y,7);
			y=y+1;
			p=p+2*dy-2*dx;
		}
		else
		{
			putpixel(x,y,7);
			p=p+2*dy;
		}
		x=x+1;
	}
}

int main()
{
	int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
	initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

	printf("Enter co-ordinates of first point: ");
	scanf("%d%d", &x0, &y0);

	printf("Enter co-ordinates of second point: ");
	scanf("%d%d", &x1, &y1);
	drawline(x0, y0, x1, y1);

	return 0;
}

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

#include<iostream.h>
#include<graphics.h>

void drawline(int x0, int y0, int x1, int y1)
{
    int dx, dy, p, x, y;

	dx=x1-x0;
	dy=y1-y0;

	x=x0;
	y=y0;

	p=2*dy-dx;

	while(x<x1)
	{
		if(p>=0)
		{
			putpixel(x,y,7);
			y=y+1;
			p=p+2*dy-2*dx;
		}
		else
		{
			putpixel(x,y,7);
			p=p+2*dy;
		}
		x=x+1;
	}
}

int main()
{
	int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
	initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

	cout<<"Enter co-ordinates of first point: ";
	cin>>x0>>y0;

	cout<<"Enter co-ordinates of second point: ";
	cin>>x1>>y1;
	drawline(x0, y0, x1, y1);

	return 0;
}

Output

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

28 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 Codeblocks.codecutter.org which can run graphic lib easily.

          regards
          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: 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++/

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

    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:

    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.

  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 *