C Program to Implement DDA Line Drawing algorithm :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
dy=abs(y2-y1);
dx=abs(x2-x1);
m=dy/dx;
if(x1<x2)
{
x=x1;
y=y1;
}
if(x1>x2)
{
x=x2;
y=y2;
}
if(m<=1)
{
putpixel((int)x,(int)y,WHITE);
for(i=x;i<dx;i++)
{
x=x+1;
y=y+m;
putpixel((int)x,(int)y,WHITE);
}
}
else
{
putpixel((int)x,(int)y,WHITE);
for(i=y;i<dy;i++)
{
x=x+(1/m);
y=y+1;
putpixel((int)x,(int)y,WHITE);
}
}
getch();
closegraph();
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
float dy,dx,m,i,x,y,x1,x2,y1,y2;
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n Enter 1st co-ordinates :");
scanf("%d%d",&x1,&y1);
printf("\n Enter 2nd co-ordinates :");
scanf("%d%d",&x2,&y2);
dx=abs(x2-x1);
m=dy/dx;
if(x1<x2)
{
x=x1;
y=y1;
}
if(x1>x2)
{
x=x2;
y=y2;
}
if(m<=1)
{
putpixel((int)x,(int)y,WHITE);
for(i=x;i<dx;i++)
{
x=x+1;
y=y+m;
putpixel((int)x,(int)y,WHITE);
}
}
else
{
putpixel((int)x,(int)y,WHITE);
for(i=y;i<dy;i++)
{
x=x+(1/m);
y=y+1;
putpixel((int)x,(int)y,WHITE);
}
}
getch();
closegraph();
}
I |
No comments:
Post a Comment