Friday, August 17, 2007

Bresenham's Line (for slope 0 to 1)

/* Bresenham's Line Implementation for 0<=m<=1 and drawing from left to right. - Subhranath Chunder */

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

void lineBres(int,int,int,int);
void setPixel(int,int);

void main()
{
int x1,y1,x2,y2;
int driver=DETECT,mode;

clrscr();
printf("Enter the co-ordinates of the first point (a b): ");
scanf("%d %d",&x1,&y1);
printf("Enter the co-ordinates of the second point (a b): ");
scanf("%d %d",&x2,&y2);
printf("<Press any key to continue>");
getch();

initgraph(&driver,&mode,"F:\\TC\\BGI");
lineBres(x1,y1,x2,y2);
getch();
closegraph();
}

void lineBres(int x1,int y1,int x2,int y2)
{
float error,m;
int x,y;
m=(float)(y2-y1)/(x2-x1);
error=0;
y=y1;
x=x1;
setPixel(x,y);
while(x<=x2)
{
error+=m;
if(error>.5)
{
y+=1;
--error;
}
++x;
setPixel(x,y);
}
}

void setPixel(int x,int y)
{
putpixel(x,y,2);
}





No comments: