Sunday, June 11, 2017
DDA Line Drawing Algorithm
DDA Line Drawing Algorithm
PROGRAM:
using namespace std;
#include<iostream>
#include<graphics.h>
#include<math.h>
class DDALINE
{
int xmid,ymid;
float x1,y1,x2,y2;
public :
void quad();
void simple(float,float,float,float);
void dot(float,float,float,float);
void dash(float,float,float,float);
void dashdot(float,float,float,float);
void thick(float,float,float,float,int);
};
void DDALINE :: quad()
{
int xmax=640,ymax=480;
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
xmid=xmax/2;
ymid=ymax/2;
line(0,ymid,xmax,ymid);
line(xmid,0,xmid,ymax);
}
void DDALINE :: simple(float x1,float y1,float x2,float y2)
{
int i,steps;
float dx,dy,xinc,yinc;
dx=x2-x1;
dy=y2-y1 ;
if(dx<dy)
{
steps=abs(dy);
}
else
{
steps=abs(dx);
}
xinc=(x2-x1)/steps;
yinc=(y2-y1)/steps;
putpixel(x1,y1,15);
for(i=1;i<=steps;i++)
{
x1=x1+xinc;
y1=y1+yinc;
putpixel(x1,y1,18);
}
}
void DDALINE :: dot(float x1,float y1,float x2,float y2)
{
int i,steps;
float dx,dy,xinc,yinc;
dx=x2-x1;
dy=y2-y1 ;
if(dx<dy)
{
steps=abs(dy);
}
else
{
steps=abs(dx);
}
xinc=(x2-x1)/steps;
yinc=(y2-y1)/steps;
putpixel(x1,y1,15);
for(i=1;i<=steps;i++)
{
x1=x1+xinc;
y1=y1+yinc;
if(i%2==0)
{putpixel(x1,y1,18);}
}
}
void DDALINE :: dash(float x1,float y1,float x2,float y2)
{
int i,steps;
float dx,dy,xinc,yinc;
dx=x2-x1;
dy=y2-y1 ;
if(dx<dy)
{
steps=abs(dy);
}
else
{
steps=abs(dx);
}
xinc=(x2-x1)/steps;
yinc=(y2-y1)/steps;
putpixel(x1,y1,15);
for(i=1;i<=steps;i++)
{
x1=x1+xinc;
y1=y1+yinc;
if(i%5<3)
{putpixel(x1,y1,18);}
}
}
void DDALINE :: dashdot(float x1,float y1,float x2,float y2)
{
int i,steps;
float dx,dy,xinc,yinc;
dx=x2-x1;
dy=y2-y1 ;
if(dx<dy)
{
steps=abs(dy);
}
else
{
steps=abs(dx);
}
xinc=(x2-x1)/steps;
yinc=(y2-y1)/steps;
putpixel(x1,y1,15);
for(i=1;i<=steps;i++)
{
x1=x1+xinc;
y1=y1+yinc;
if(i%6<3 || i%6==4)
{putpixel(x1,y1,18);}
}
}
void DDALINE :: thick(float x1,float y1,float x2,float y2,int t)
{
int i,j,steps;
float dx,dy,xinc,yinc;
dx=x2-x1;
dy=y2-y1 ;
if(dx<dy)
{
steps=abs(dy);
}
else
{
steps=abs(dx);
}
xinc=(x2-x1)/steps;
yinc=(y2-y1)/steps;
putpixel(x1,y1,15);
for(i=1;i<=steps;i++)
{
x1=x1+xinc;
y1=y1+yinc;
for(j=0;j<t;j++)
{putpixel(x1,y1+j,18);}
}
}
int main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2;
int ans, ch,t;
DDALINE p;
do
{
cout<<" Enter X1 : ";
cin>>x1;
cout<<" Enter Y1 : ";
cin>>y1;
cout<<" Enter X2 : ";
cin>>x2;
cout<<" Enter Y2 : ";
cin>>y2;
x1=x1+320;
y1=240-y1;
x2=x2+320;
y2=240-y2;
cout<<" Menu:";
cout<<" 1.Simple line 2.Dashed line 3.Dotted line 4.Center line 5.thick";
cout<<" Enter choice : ";
cin>>ch;
switch(ch)
{
case 1:
p.quad();
p.simple(x1,y1,x2,y2);
break;
case 2:
p.quad();
p.dash(x1,y1,x2,y2);
break;
case 3:
p.quad();
p.dot(x1,y1,x2,y2);
break;
case 4:
p.quad();
p.dashdot(x1,y1,x2,y2);
break;
case 5:
printf(" Enter the thick ness of line");
scanf("%d",&t);
p.quad();
p.thick(x1,y1,x2,y2,t);
break;
}
getch();
closegraph();
cout<<"Do You Want To Continue : ";
cin>>ans;
getch();
closegraph();
}while(ans==1);
getch();
return 0;
}
download more info
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment