Implementation of Bresenhams algorithm
1.a.) Implementation of Bresenhams algorithm -
Program :
// This program will work for all input .... 0<m<1 and m>1
// where 'm' is slope
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
main()
{
int gd=DETECT,gm;
int xa,xb,ya,yb;
int dx,dy,x,y,xend,p,dxt,dyt,slope_pos,yend;
initgraph(&gd,&gm,"H:\\TC\\BGI");
printf("Enter the First End points(xa,ya):\n");
scanf("%d%d",&xa,&ya);
printf("Enter the Second End points(xb,yb):\n");
scanf("%d%d",&xb,&yb);
dxt=xb-xa;
dyt=yb-ya;
dx=abs(xb-xa);
dy=abs(yb-ya);
slope_pos=(dxt>0);
if(dyt<0)
slope_pos=!slope_pos;
if(dx>dy) // slope => 0<m<1
{
p=2*dy-dx;
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
while(x<xend)
{
x=x+1;
if(p<0)
{
p=p+2*dy;
}
else
{
if(slope_pos)
y=y+1;
else
y=y-1;
p=p+2*(dy-dx);
}
putpixel(x,y,6);
}
}
else // slope => m>1
{
p=2*dx-dy;
if(ya>yb)
{
x=xb;
y=yb;
yend=ya;
}
else
{
x=xa;
y=ya;
yend=yb;
}
while(y<yend)
{
y=y+1;
if(p<0)
{
p=p+2*dx;
}
else
{
if(slope_pos)
x=x+1;
else
x=x-1;
p=p+2*(dx-dy);
}
putpixel(x,y,6);
}
}
putpixel(xa,ya,4);
putpixel(xb,yb,4);
getch();
return(0);
}
Output :
Enter The Two Left Endpoints(xa,ya): 50 60
Enter The Two Right Endpoints(xb,yb): 150 360
No comments:
Post a Comment