//Program to draw a line by DDA approach
Source code
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
#include<dos.h>
int round(float y)
{
int y1;
float diff;
y1=y;
diff=y-y1;
if(diff>0.5)
return(y1+1);
else
return(y1);
}
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;
int x,y,i,a;
float x1,x2,y1,y2,x3,y3,m,m1,dy,dx,t=1;
printf("Enter the value of x1,x2,y1,y2");
scanf("%f%f%f%f",&x1,&x2,&y1,&y2);
printf("enter 1 for left to right and 2 for right to left");
scanf("%d",&a);
clrscr();
/* 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);
}
setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy();
dy=y2-y1;
dx=x2-x1;
m=dy/dx;
x1=x1+xmax/2;
x2=x2+xmax/2;
y1=ymax/2-y1;
y2=ymax/2-y2;
for(i=0;i<ymax;i++)
putpixel(xmax/2,i,15);
for(i=0;i<xmax;i++)
putpixel(i,ymax/2,15);
if(a==1)
{
if((m<1&&m>-1))
{
y3=y1;
for(x=x1;x<x2;x++)
{
putpixel(x,round(y3),15);
delay(100);
y3=y3-m;
}
}
else
{
x3=x1;
m1=t/m;
for(y=y1;y>y2;y--)
{
putpixel(round(x3),y,15);
delay(100);
x3=x3+m1;
}
}
}
else
{
if((m<1&&m>-1))
{
y3=y2;
for(x=x2;x>x1;x--)
{
putpixel(x,round(y3),15);
delay(100);
y3=y3+m;
}
}
else
{
x3=x2;
m1=t/m;
for(y=y2;y<y1;y++)
{
putpixel(round(x3),y,15);
delay(100);
x3=x3-m1;
}
}
}
/* clean up */
getch();
closegraph();
return 0;
}
No comments:
Post a Comment