C program to implement Bresenham's line integer algorithm

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,x2,y1,y2,dx,dy,e,m,k;
clrscr();
initgraph (&gd,&gm,"C:\\TC\\BGI");
printf("Enter the co-ordinates of the first point \n");
printf("x1= ");
scanf("%d/n",&x1);
printf("y1= ");
scanf("%d/n",&y1);
printf("Enter the co-ordinates of the second point \n");
printf("x2= ");
scanf("%d/n",&x2);
printf("y2= ");
scanf("%d/n",&y2);
clrscr();
dx= x2-x1;
dy= y2-y1;
m=(float)dy/dx;
e=m-0.5;
x=x1;
y=y1;
for(k=1;k<=dx;k++)
{
putpixel (x,y,RED);
while(e>0)
{y++;
e--;
}
x++;
e=e+m;
}
getch();
closegraph();
}
Previous
Next Post »