Tuesday, March 25, 2014

PROGRAM TO DRAW LINE & CIRCLE USING DDA & BRESENHAM’S ALGORITHM USING C++

Bresenham’s algorithm: It is commonly used to draw lines on a computer screen, as it uses only integer addition, subtraction ,bit shifting all of which are very cheap operations in standard computer architectures.
DDA: Digital Differential Analyzer is a scan conversion line algorithm based on calculating either dy or dx. We sample the line at unit intervals in one coordinate & determine corresponding integer values nearest to the line path for the other coordinate. DDAs are used for rasterization of lines, triangles and polygons.
  
Code :
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<dos.h>
#include<stdio.h>
#include<process.h>
class draw
{
float x,y,x1,y1,x2,y2,dx,dy,xinc,yinc,g,temp,m;
int steps;
public:
void ddaline();
void bline();
void ddacircle();
void bcircle();
};
void draw::ddaline()
{
int i;
cout<<”\n Please enter x1: “;
cin>>x1;
cout<<”\n Please enter y1: “;
cin>>y1;
cout<<”\n Please enter x2: “;
cin>>x2;
cout<<”\n Please enter y2: “;
cin>>y2;
dx=abs(x2-x1);
dy=abs(y2-y1);
clrscr();
if(dx>dy)
steps=dx;
else
steps=dy;
x=x1;
y=y1;
xinc=dx/steps;
yinc=dy/steps;
for(i=0;i<steps;i++)
{
putpixel(x+5.0,y+5.0,RED);
delay(10);
x=x+xinc;
y=y+yinc;
}
}
void draw::bline()
{
int i;
cout<<”\n Please enter x1: “;
cin>>x1;
cout<<”\n Please enter y1: “;
cin>>y1;
cout<<”\n Please enter x2: “;
cin>>x2;
cout<<”\n Please enter y2: “;
cin>>y2;
dx=x2-x1;
dy=y2-y1;
g=(2*dy)-dx;
x=x1;
y=y1;
m=dy/dx;
x=x1;
y=y1;
if(m>1)
{
temp=x;
x=y;
y=temp;
}
for(i=0;i<dx;i++)
{
putpixel(x,y,19);
delay(10);
x++;
if(g>0)
{
y++;
g=g+(2*dy)-(2*dx);
}
else
g=g+(2*dy);
}
}
void draw::ddacircle()
{
float rad,start_x=rad,start_y=0,e;
int p,n=0;
cout<<”\n\nEnter the radius of circle: “;
cin>>rad;
x1=0;
x2=rad;
x1=start_x;
y1=start_y;
do
{
p=pow(2,n);
n++;
}while(p<rad);
e=1/(pow(2,n-1));
do
{
x2=x1+e*y1;
y2=y1-e*x2;
delay(10);
putpixel(200+x2,200+y2,3);
x1=x2;
y1=y2;
}while( (y1-start_y)<e || (start_x-x1)>e );
}
void draw::bcircle()
{
float rad;
float d=3-(2*rad);
cout<<”\n\nEnter the radius of circle: “;
cin>>rad;
x2=0;
y2=rad;
do
{
delay(15);
putpixel(200+x2,200+y2,1);
putpixel(200+y2,200+x2,2);
putpixel(200-y2,200+x2,3);
putpixel(200+x2,200-y2,4);
putpixel(200-x2,200-y2,5);
putpixel(200-y2,200-x2,6);
putpixel(200+y2,200-x2,7);
putpixel(200-x2,200+y2,8);
if(d<0)
{
d=d+(4*x2)+6;
}
else
{
d=d+(4*(x2-y2))+10;
y2–;
}
x2++;
}while(x2<y2);
}
void main()
{
draw obj;
int gd=DETECT,gm;
int choice;
char ans;
float rad;
clrscr();
initgraph(&gd,&gm,”C:\\TC\\BGI”);
do
{
cout<<”\n\n MENU:”;
cout<<”\n\n\t1.DDA Line\n\n\t2.Bresenham’s Line\n\n\t3.DDA Circle\n\n\t4.Bresenham’s Circle”;
cout<<”\n\nEnter your choice: “;
cin>>choice;
switch(choice)
{
case 1:
obj.ddaline();
break;
case 2:
obj.bline();
break;
case 3:
obj.ddacircle();
break;
case 4:
obj.bcircle();
break;
default:
cout<<”\n\nPlease Enter Correct Choice: “;
break;
}
cout<<”\n\nDo you want to continue?(y/n): “;
fflush(stdin);
cin>>ans;
}while(ans==’y’ || ans==’Y');
getch();
closegraph();
}

No comments:

Post a Comment