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

Here you will get the program for Brenham’s line attracting calculation C and C++.

This calculation is utilized in PC designs for drawing the line.

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

Make a point to change the way of BGI envelope inside initgraph() work as indicated by your framework. Generally, the program won’t work.

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

Leave a Comment

error: Alert: Content is protected!!