Code Competitions

Coding competition

Tuesday, 14 May 2013

Implement line algorithm for all slopes using DDA algorithm


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<iostream.h>
void main()
{
int x1,y1,x2,y2,temp;
cout<<"\n ENTER X1,Y1=";
cin>>x1>>y1;
cout<<"\n ENTER X2,Y2=";
cin>>x2>>y2;
float dx,dy,Xinc,Yinc,x,y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
if(x1>x2 || y1>y2)
{
temp=x1;
x1=x2;
x2=temp;
int temp1;
temp1=y1;
y1=y2;
y2=temp1;
}
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>dy)
 {
  temp=dx;
 }
else
 {
  temp=dy;
 }
Xinc=dx/temp;
Yinc=dy/temp;
x=x1;
y=y1;
putpixel(x,y,3);
for(int i=1;i<=temp;i++)
{
x+=Xinc;
y+=Yinc;
putpixel((int)x,(int)y,3);
}
putpixel(x1,y1,15);
putpixel(x2,y2,15);
getch();
}





No comments:

Post a Comment