Program 1: Draw a line using DDA Algorithm.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void ddaline(int,int,int,int);
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\turboc3");
int x1,x2,y1,y2;
cout<<"Enter Initial
co-ordinates:(x1,y1)";
cout<<"\nx-axis:";
cin>>x1;
cout<<"y-axis:";
cin>>y1;
cout<<"Enter End
co-ordinates:(x2,y2)";
cout<<"\nx-axis:";
cin>>x2;
cout<<"y-axis:";
cin>>y2;
ddaline(x1,x2,y1,y2);
getch();
}
void ddaline(int x1,int x2,int y1,int y2)
{
int dx,dy,steps,k;
float xincr,yincr,x=200,y=200;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
{
steps=dx;
}
else
{
steps=dy;
}
xincr=dx/steps;
yincr=dy/steps;
putpixel(x,y,12);
for(k=0;k<=steps;k++)
{
x+=xincr;
y+=yincr;
putpixel(x,y,12);
}
}
Output:
Enter Initial co-ordinates:(x1,y1)
x-axis:100
y-axis:150
Enter End co-ordinates:(x2,y2)
x-axis:500
y-axis:150
No comments:
Post a Comment