Write C++/Java program to draw line using DDA and Bresenham‘s algorithm. Inherit pixel class and Use function overloading.

Second Year Computer Engineering Microprocessor Programs:

Computer Graphics Lab:

Practical 1:
   Write C++/Java program to draw line using DDA and Bresenham‘s algorithm. Inherit pixel class and Use function overloading.

----------------------------------------------------------------------------------------------------------------------------------

#include<iostream>
#include<graphics.h>
#include<math.h>
#include<string>
#include<cstdlib>
using namespace std;

class pixel
{
public:
int x;
pixel()
{

}
virtual void getInput()
{
};
int sign(int x)
{
    if(x>0)
    {
    return 1;
    }
    else if(x<0)
    {
    return -1;
    }
    else if(x==0)
    {
    return 0;
    }
}
    

};
class dda:public pixel
{
public:
pixel a;
void getInput()
{
int x1,y1,x2,y2;
cout<<"Enter X1 and Y1"<<endl;
cin>>x1>>y1;
cout<<"Enter X2 and Y2"<<endl;
cin>>x2>>y2;

int gm=DETECT,gd;
initgraph(&gd,&gm,NULL);

DDA(x1,y1,x2,y2);
closegraph();

}
void DDA(int x1,int y1,int x2,int y2)
{
int dx,dy;
int length;
int x,y;

dx=abs(x2-x1);
dy=abs(y2-y1);

if(dx>=dy)
{
length=dx;
}
else
{
length=dy;
}
dx=(x2-x1)/length;
dy=(y2-y1)/length;

int s;
s=a.sign(dx);
x=x1+0.5*s;
s=a.sign(dy);
y=y1+0.5*s;
int i;
while(i<=length)
{
putpixel(x,y,WHITE);
x=x+dx;
y=y+dy;
i++;

delay(50);
}
 }


};


class BAL:public pixel
{
public:
pixel a;

void getInput()
{
int x1,y1,x2,y2;
cout<<"Enter X1 and Y1"<<endl;
cin>>x1>>y1;

cout<<"Enter X2 and Y2"<<endl;
cin>>x2>>y2;

int color;
cout<<"Enter Color code - 12(RED)"<<endl;
cin>>color;

int gm=DETECT,gd;
initgraph(&gd,&gm,NULL);

Breshenham(x1,y1,x2,y2,color);
closegraph();
}
void Breshenham(int x1,int y1,int x2,int y2,int color)
{

int dx,dy;
dx=abs(x2-x1);
dy=abs(y2-y1);

int x,y;
x=x1;
y=y1;
int s1;
s1=a.sign(x2-x1);
int s2;
s2=a.sign(y2-y1);

int exchange;
if(dy>dx)
{
exchange=1;

int temp;
temp=dy;
dy=dx;
dx=temp;
}
else
{
exchange =0;
}
int e;
e=(2*dy)-dx;
for(int i=0;i<dx;i++)
{
putpixel(x,y,color);
while(e>0)
{
if(exchange=1)
{
x=x+s1;
}
else
{
y=y+s2;
}

e=e-(2*dx);
}

while(e<=0)
{
if(exchange=1)
{
y=y+s2;
}
else
{
x=x+s1;
}

e=e+(2*dy);
}
delay(100);
}
}

};

int main()
{
dda a;
BAL b;
while(true)
{
cout<<"Menu"<<endl;
cout<<"1.DDA"<<endl;
cout<<"2.Bresanham Algorithm"<<endl;
cout<<"3.Exit"<<endl;
cout<<"Enter Your choice"<<endl;
int ans;
cin>>ans;
switch(ans)
{
case 1:

a.dda::getInput();
break;
case 2:

b.BAL::getInput();
break;
case 3:
exit(0);
}
}
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

Implement C++ program for expression conversion as infix to postfix and its evaluation using stack.

Write C++ program to maintain club member‘s information using singly linked list. Store student PRN and Name.

Write C++/Java program for line drawing using DDA or Bresenhams algorithm with patterns such as solid, dotted, dashed, dash dot and thick.