100% found this document useful (1 vote)
51 views3 pages

Program On Bresenham Line Drawing Algorithm

A simple c program illustrating original and generalized bresenham line drawing algorithm. Originnal bresenham line drawing algorithm have some limitations for that generalised line drawing algorithm is used .

Uploaded by

Hitesh Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
51 views3 pages

Program On Bresenham Line Drawing Algorithm

A simple c program illustrating original and generalized bresenham line drawing algorithm. Originnal bresenham line drawing algorithm have some limitations for that generalised line drawing algorithm is used .

Uploaded by

Hitesh Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

//bresenham algorithm

/*plotLine(x0,y0, x1,y1)
dx=x1-x0
dy=y1-y0
D = 2*dy - dx
plot(x0,y0)
y=y0
for x from x0+1 to x1
if D > 0
y = y+1
plot(x,y)
D = D + (2*dy-2*dx)
else
plot(x,y)
D = D + (2*dy)
*/
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#define sign(x) ((x>0)?1:((x<0)?-1:0))
//int sign (int);
void bresenham_original ( int, int, int, int);
void bresenham_generalised ( int, int, int, int);
void initilizegraphics (void);
int main (void)
{
int xi,yi,xf,yf,choice;
printf("Enter coordinates of initial point: \n");
scanf("%d%d",&xi,&yi);
printf("Enter coordinates of final point: \n");
scanf("%d%d",&xf,&yf);
printf("Enter 1 for bresenham_original or 2 for bresenham_generalised\n"
);
scanf("%d",&choice);
initilizegraphics();
if(choice==1)
bresenham_original(xi,yi,xf,yf);
else if(choice==2)
bresenham_generalised(xi,yi,xf,yf);
else
printf("Please enter correct choice\n");
getch();
closegraph();
}
/*int sign (int s)
{
int x;
s=((x>0)?1:((x<0)?-1:0));
return s;
}*/
void initilizegraphics(void)

{
//printf("initilizegraph function called\n");
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("Graphics error: %s\n",grapherrormsg(errorcode));
printf("Press any key to hault");
getch();
exit(1);
}
//printf("initilizegraph function called success\n");
}
void bresenham_original (int x1, int y1, int x2, int y2)
{
//printf("linebres function called\nint x1-%d, int y1-%d, int x2-%d, int
y2-%d\n",x1,y1,x2,y2);
int dif,deltax,deltay;
deltay=y2-y1;
deltax=x2-x1;
//printf("%d___%d\n",deltax,deltay);
dif=2*deltay+(-deltax);
putpixel(x1,y1,WHITE);
for(x1=x1+1;x1<=x2;x1++)
{
//printf("dif=%d\n",dif);
if(dif>0)
{
y1+=1;
//printf("%d___%d\n",x1,y1);
putpixel( x1, y1, WHITE);
dif=dif+(2*deltay+2*(-deltax));
//printf("dif>0=%d\n",dif);
}
else
{
//printf("%d___%d\n",x1,y1);
putpixel( x1, y1, WHITE);
dif=dif+2*deltay;
//
printf("dif<0=%d\n",dif);
}
}
}
void bresenham_generalised (int x0, int y0, int xn, int yn)
{
int dx,dy,s1,s2,swap=0,temp,difference,i;
dx=abs(xn-x0);//printf("dx:%d\n",dx);
dy=abs(yn-y0);//printf("dx:%d\n",dy);
s1=sign(xn-x0);//printf("s1:%d\n",s1);
s2=sign(yn-y0);//printf("s2:%d\n",s2);
if(dy>dx)
{
//printf("dy>dx\n");
//interchange dy and dx
temp=dx;dx=dy;dy=temp;swap=1;

}
difference=2*dy+(-dx);
//printf("difference:%d\n",difference);
for(i=0;i<dx;i++)
{
//printf("inside for loop i:%d\n",i);
//printf("inside for loop x0,y0:%d,%d\n",x0,y0);
putpixel(x0,y0,WHITE);
while(difference>=0)
{
//printf("inside while difference>=0 difference=%d\n",di
fference);
difference=difference+2*(-dx);
if(swap)
x0+=s1;
else
y0+=s2;
}
difference=difference+2*dy;
if(swap)
y0+=s2;
else
x0+=s1;
//printf("end of for new x0 y0:%d,%d \n",x0,y0);
}
}

You might also like