BRESENHAMS LINE DRAWING ALGORITHM IN C LANGUAGE - COMPUTER GRAPHICS PROGRAME - Anna University Multiple Choice Questions

BRESENHAMS LINE DRAWING ALGORITHM IN C LANGUAGE - COMPUTER GRAPHICS PROGRAME

AIM
      To write a C Program to draw a line using Bresenham's Line Drawing algorithm.

PROGRAM
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
clrscr();
printf("\nEnter the co-ordinates of first point:\n");
scanf("%d %d",&x1,&y1);
printf("\nEnter the co-ordinates of second point:\n");
scanf("%d %d",&x2,&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}

OUTPUT
Enter the co-ordinates of first point:
100
200
Enter the co-ordinates of second point:
300
400
                  \
                    \
                      \
                        \
                          \
                            \
Note : Output line will not be a dotted line as like this....you will get filled line in the o/p..

No comments:

Post a Comment