PROGRAM NAME:IMPLEMENTATION OF BRESENHAM’S LINE
PROGRAM LANGUAGE:C
PROGRAM SOFTWARE:C Complier
PROGRAM DESCRIPTION:The Bresenham line algorithm is an algorithm which determines which points in an n-dimensional raster should be plotted in order to form a close approximation to a straight line between two given points. The endpoints of the line are the pixels at (x0, y0) and (x1, y1), where the first coordinate of the pair is the column and the second is the row
PROGRAM LANGUAGE:C
PROGRAM SOFTWARE:C Complier
PROGRAM DESCRIPTION:The Bresenham line algorithm is an algorithm which determines which points in an n-dimensional raster should be plotted in order to form a close approximation to a straight line between two given points. The endpoints of the line are the pixels at (x0, y0) and (x1, y1), where the first coordinate of the pair is the column and the second is the row
Code:
#include <graphics.h> /* include the necessary header files*/
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void draw(int xa, int ya, int xb, int yb);
void main()
{
int xa, ya, xb, yb;
clrscr();
printf("Bresenhnams algorithm"); /* get the coordinates of the line*/
printf("\n Enter the value of xa, ya:");
scanf("%d%d",&xa,&ya);
printf("\n Enter the value of xb, yb:");
scanf("%d%d",&xb,&yb);
draw(xa,ya,xb,yb);
}
void draw(int xa, int ya, int xb, int yb)
{
int x,y,dx,dy,xend,p; /* request auto detection */
int gdriver=DETECT,gmode,errorcode; /* initialize graphics and local variables */
initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); /* read result of initialization */
errorcode=graphresult(); /* an error occurred */
if(errorcode!=grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
dx=xb-xa;
dy=yb-ya;
p=2*dy-dx; /* calculate the value of the condition variable*/
if(xa>xb) /* depending on the position of the coordinates*/
{
x=xb; /* assign the values for (x,y)*/
y=yb;
xend=xa;
}
else if(xb>xa)
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,1); /* draw the pixel on the screen*/
while(x<xend) /* depending on the control condition draw the pixels*/
{
x=x+1;
if(p<0)
{
p=p+2*dy;
}
else
{
y=y+1;
p=p+2*dy;
}
putpixel(x,y,1);
} /* clean up */
getch();
closegraph();
}