blog about computer science ,here we provide detailed information about topics in computer science.Computer graphics programming,computer graphics concepts,microprocessor assembly language programming. computer networking,computer programming,artificial intelligence,computer hardware,etc here you will find detailed explanation of topics
Search This Blog
TITLE: Write C++/Java program to draw line using DDA and Bresenham‘s algorithm. Inherit pixel class and Use function overloading.
TITLE: Write C++/Java program to draw line using DDA and Bresenham‘s algorithm. Inherit pixel class and Use function overloading.
#include<iostream>
#include<math.h>
#include<graphics.h>
using namespace std;
class pixel_entry
{
public:
int x1,y1,x2,y2;
void accept()
{
cout<<"\n Enter the x1 coordinates: ";
cin>>x1;
cout<<"\n Enter the y
1 coordinates: ";
cin>>y1;
cout<<"\n Enter the x2 coordinates: ";
cin>>x2;
cout<<"\n Enter the y2 coordinates: ";
cin>>y2;
}
int plotpixel(int x, int y)
{
putpixel(int(x),int(y),WHITE);
}
};
class DDA:public pixel_entry
{
public:
int i,length;
float x,y,dx,ix,iy,dy;
int sign(int z)
{
if(z>1)
return 1;
else
return -1;
}
int line(int x1,int y1,int x2,int y2)
{
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>dy)
{
length=dx;
}
else
{
length=dy;
}
ix=(dx/length);
iy=(dy/length);
x=x1+0.5*sign(x2-x1);
y=y1+0.5*sign(y2-y1);
for(i=0;i<=length;i++)
{
plotpixel(x,y);
x=x+ix;
y=y+iy;
}
}
};
class Bresenhms:public pixel_entry
{
int dx,dy,i,length,x,y,s1,s2,e,temp,exc;
public:
int sign(int z)
{
if(z>1)
return 1;
else
return -1;
}
int line(int x1,int y1,int x2,int y2)
{
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
plotpixel(x,y);
s1=sign(x2-x1);
s2=sign(y2-y1);
if(dy>dx)
{
temp=dy;
dy=dx;
dx=temp;
exc=1;
}
else
{
exc=0;
}
e=((2*dy)-dx);
i=1;
while(i<dx)
{
while(e>=0)
{
if(exc=1)
{
x=x+s1;
}
else
{
y=y+s2;
}
e=e-2*dx;
}
if(exc=1)
{
y=y+s2;
}
else
{
x=x+s1;
}
e=e+2*dy;
plotpixel(x,y);
i++;
}
}
};
int main()
{
int ch,gm,gd=DETECT;
char ans;
DDA D;
Bresenhms B;
cout<<"\n********************MENU**************************";
cout<<"\n1.DDA Line Drawing Algorithm...";
cout<<"\n2.Bresenhams Line Drawing Algorithm...";
cout<<"\n3.Exit...";
do
{
cout<<"\nEnter the choice: ";
cin>>ch;
switch(ch)
{
case 1: D.accept();
initgraph(&gd,&gm,NULL);
D.line(D.x1,D.y1,D.x2,D.y2);
delay(100000);
closegraph();
break;
case 2: B.accept();
initgraph(&gd,&gm,NULL);
B.line(B.x1,B.y1,B.x2,B.y2);
delay(100000);
closegraph();
break;
}
}while(ch!=3);
delay(10000);
closegraph();
return 0;
}
OUTPUT:
Subscribe to:
Posts (Atom)