Friday, 6 September 2013

Program for Line Drawing using Bresenham’s algorithm

#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd,gm,i;
float x,y,x1,y1,x2,y2,dx,dy,length,e;

printf("enter the value of x1:\n");
scanf("%f",&x1);
printf("enter the value of y1:\n");
scanf("%f",&y1);
printf("enter the value of x2:\n");
scanf("%f",&x2);
printf("enter the value of y2:\n");
scanf("%f",&y2);

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");

dx=x2-x1;
dy=y2-y1;

x=x1;
y=y1;
e=2*dy-dx;

       for(i=0;i<dx;i++)
       {
putpixel(x,y,RED);

while(e>=0)
{
y++;
e=e-2*dx;
}
x++;
e=e+2*dy;
}
getch();
closegraph();

}

No comments:

Post a Comment