Bressenham DDA Line drawing and Circle Drawing Algorithms
Write a program for Bressenham and DDA Line Drawing algorithms using C++ language. Simulate these algorithms using C++ graphics classes and functions.
User has to provide input initially and then by selecting proper option user will get the output.
DDA Line Drawing algorithm Code
#include
#include
#include
#include
#include
class algo
{
float x1,x2,y1,y2;
public:
int sign(float a)
{
if(a>0)
return 1;
if(a<0)
return -1;
return 0;
}
void dda(float,float,float,float);
void br_line(float,float,float,float);
void br_c(float,float,float);
};
void main()
{
algo a;
int ch,gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
do
{
cleardevice();
cout<< "\n\t\t\t#### MAIN MENU ####";
cout<< "\n\t\t1.DDA LINE DRAWING ALGORITHM";
cout<< "\n\t\t2.BRESENHAM LINE DRAWIG ALGORITHM";
cout<< "\n\t\t3.BRESENHAM CIRCLE DRAWNG ALGORITHM";
cout<< "\n\t\t4.EXIT";
cout<< "\n\t\tENTER UR CHOICE:";
cin>> ch;
clrscr();
switch(ch)
{
case 1: float x2,x1,y2,y1;
cout<< "\n\t\tDDA LINE DRAWING ALGO";
cout<< "\n\tEnter (x1,y1) co-ordinates of first point:";
cin>>x1>>y1;
cout<< "\n\tEnter (x2,y2) co-ordinates. of last point:";
cin>>x2>>y2;
a.dda(x1,y1,x2,y2);
getch();
break;
case 2: cout<< "\n\t\tBRESENHAM LINE DRAWIG ALGO";
cout<< "\n\tEnter (x1,y1) co-ordinates of first point";
cin>>x1>>y1;
cout<< "\n\tEnter (x2,y2) co-ordinates of end point";
cin>>x2>>y2;
a.br_line(x1,y1,x2,y2);
getch();
break;
case 3: cout<< "\n\t\tBRESENHAM CIRCLE DRAWNG ALGO";
cout<< "\n\tEnter center co-ordinates.";
cin>>x1>>y1;
float r;
cout<< "\n\tEnter radius";
cin>>r;
a.br_c(x1,y1,r);
getch();
break;
case 4: break;
default:cout<< "\t\tInvalid Choice\n";
getch();
break;
}
}while(ch!=4);
closegraph();
}
void algo:: dda(float x1,float y1,float x2,float y2)
{
float dx,dy,len,i,xn,yn,x,y,midx,midy;
midx=getmaxx()/2;
midy=getmaxy()/2;
line(0,midy,2*midx,midy);
line(midx,0,midx,2*midy);
x1+=midx;
y1=-y1;
y1+=midy;
x2+=midx;
y2=-y2;
y2+=midy;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
len=dx;
else
len=dy;
dx=(x2-x1)/len;
dy=(y2-y1)/len;
x=x1+0.5*sign(dx);
y=y1+0.5*sign(dy);
putpixel(x,y,WHITE);
i=1;
while(i<=len)
{
putpixel(x,y,RED);
x=x+dx;
y=y+dy;
i++;
}
getch();
}
void algo:: br_line(float x1,float y1,float x2,float y2)
{
float dx,dy,s1,s2,e,i,change,x,y,midx,midy,x3,y3,exch;
midx=getmaxx()/2;
midy=getmaxy()/2;
line(0,midy,2*midx,midy);
line(midx,0,midx,2*midy);
x1+=midx;
y1=-y1;
y1+=midy;
x2+=midx;
y2=-y2;
y2+=midy;
x=abs(x2-x1);
y=abs(y2-y1);
x3=x1;
y3=y1;
s1=sign(x2-x1);
s2=sign(y2-y1);
e=2*y-x;
if(y>x)
{
int temp=x;
x=y;
y=temp;
exch=1;
}
else
exch=0;
i=1;
while(i<=x)
{
putpixel(x3,y3,WHITE);
while(e>=0)
{
if(exch==1)
x3+=s1;
else
y3+=s2;
e=e-2*x;
}
if(exch==1)
y3+=s2;
else
x3+=s1;
e=e+2*y;
i++;
}
}
void algo:: br_c(float a,float b,float r)
{
float d,x,y,midx,midy;
midx=getmaxx()/2;
midy=getmaxy()/2;
line(0,midy,2*midx,midy);
line(midx,0,midx,2*midy);
x+=midx;
y=-y;
y+=midy;
x1=0,y1=r;
d=3-2*r;
while(x1<=y1)
{
if(d<0)
{
d=d+4*x1+6;
}
else
{
d=d+4*(x1-y1)+10;
y1=y1-1;
}
x1=x1+1;
putpixel(x1+a+(getmaxx()/2),y1+b+(getmaxy()/2),BLUE);
putpixel(y1+a+(getmaxx()/2),x1+b+(getmaxy()/2),BLUE);
putpixel(-x1+a+(getmaxx()/2),-y1+b+(getmaxy()/2),BLUE);
putpixel(-x1+a+(getmaxx()/2),y1+b+(getmaxy()/2),BLUE);
putpixel(y1+a+(getmaxx()/2),-x1+b+(getmaxy()/2),BLUE);
putpixel(-y1+a+(getmaxx()/2),x1+b+(getmaxy()/2),BLUE);
putpixel(x1+a+(getmaxx()/2),-y1+b+(getmaxy()/2),BLUE);
putpixel(-y1+a+(getmaxx()/2),-x1+b+(getmaxy()/2),BLUE);
}
}


Leave a Reply