Monday , December 18 2017
Home / C / C Program for Drawing a Line using Bresenham Line Algorithm

C Program for Drawing a Line using Bresenham Line Algorithm

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x,y,dy,dx,p;
float m;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\");

printf("Enter first co-ords of line\n");
scanf("%d",&x1);
scanf("%d",&y1);
printf("Enter second co-ords of line\n");
scanf("%d",&x2);
scanf("%d",&y2);
dy=y2-y1;
dx=x2-x1;
m=(float)(y2-y1)/(x2-x1);
x=x1;y=y1;
if(m>1.0)
{
printf("This algorithm is not applicable!!! press Enter to exit\n");
}else
{
p=2*dy-dx;
while(x<x2)
{
if(p>=0)
{
putpixel(x,y,5);

y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,5);
p=p+2*dy;
}
x=x+1;
}
}
getch();
}

Output

Also See:   C program for priority scheduling

Check Also

How to Solve Linear Equation in One Variable In C Programming?

C Program to Solve any Linear Equation in One Variable Find complete C program to ...

Leave a Reply

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