the attributes like :
->solid line
->dashed line
->dotted line
in four quadrant of 2 - D plane.
/*------------------------------Class BRESEN-------------------------------*/
class BRESEN
{
int gd,gm;
int x1,y1,x2,y2;
int dx,dy,m,ch,count,count1;
public:
BRESEN()
{
gd=DETECT;
dx=dy=m=0;
count=0,count1=3;
}
void getpoint();
void bres_line(int);
void gentle(int);
void sharp(int);
};
/*-----------------------------getpoint()----------------------------------*/
void BRESEN::getpoint()
{
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
cout<<"Enter X1 : ";
cin>>x1;
cout<<"Enter Y1 : ";
cin>>y1;
cout<<"Enter X2 : ";
cin>>x2;
cout<<"Enter Y2 : ";
cin>>y2;
cleardevice();
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
}
/*-----------------------------bres_line()---------------------------------*/
void BRESEN::bres_line(int ch)
{
dx=abs(x2-x1);
dy=abs(y2-y1);
m=(y2-y1)/(x2-x1);
if(dx > dy)
gentle(ch);
else
sharp(ch);
}
/*-------------------------------gentle()----------------------------------*/
void BRESEN::gentle(int ch)
{
int x,y,xend,p=0;
if (x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
p=(2*dy)-dx;
//Initialize the counter variable
if(ch==2)
count=0,count1=5;
else
count=0,count1=3;
while(x
{
x++;
if(p<0)
p+=(2*dy);
else if(p>=0)
{
if(m>0)
y++;
else
y--;
p+=(2*dy)-(2*dx);
}
// For Solid line
if(ch==1)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
// For Dot-Dashed line
if(ch==2)
{
count=0,count1=5;
if(count<5 && count1==5)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==5)
count1=0;
}
else
{
count1++;
if(count1==5)
count=0;
if(count1==3)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
}
}
//For Dashed line
if(ch==3)
{
count=0,count1=3;
if(count<3 && count1==3)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==3)
count1=0;
}
else
{
count1++;
if(count1==3)
count=0;
}
}
if(ch==4)
{
if(count<1)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==1)
count1=0;
}
else
{
count1++;
if(count1==2)
count=0;
}
}
delay(200);
}
}
/*-------------------------------sharp()-----------------------------------*/
void BRESEN::sharp(int ch)
{
int x,y,yend,p=0;
if (y1>y2)
{
x=x2;
y=y2;
yend=y1;
}
else
{
x=x1;
y=y1;
yend=y2;
}
// To Maintain the Dotted Line
if(ch!=4)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
p=(2*dx)-dy;
//Initialize counter variable
if(ch==2)
count=0,count1=5;
else
count=0,count1=3;
while(y
{
y++;
if(p<0)
p+=(2*dx);
else if(m>0)
x++;
else
{
x--;
p+=(2*dx)-(2*dy);
}
// For Solid line
if(ch==1)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
// For Dot-Dashed Line
if(ch==2)
{
if(count<5 && count1==5)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==5)
count1=0;
}
else
{
count1++;
if(count1==5)
count=0;
if(count1==3)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
}
}
// For Dashed line
if(ch==3)
{
if(count<3 && count1==3)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==3)
count1=0;
}
else
{
count1++;
if(count1==3)
count=0;
}
}
if(ch==4)
{
if(count<1)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==1)
count1=0;
}
else
{
count1++;
if(count1==2)
count=0;
}
}
delay(100);
}
}
/*-------------------------------main()-------------------------------------*/
int main()
{
BRESEN b;
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int ch;
do
{
cleardevice();
cout<<"\n----------------------------------------------------";
cout<<"\n BRESENHAM ALGORITHM ";
cout<<"\n----------------------------------------------------";
cout<<"\n 1.SOLID LINE";
cout<<"\n 2.DOT-DASHED LINE";
cout<<"\n 3.DASHED LINE";
cout<<"\n 4.DOTTED LINE";
cout<<"\n 5.EXIT";
cout<<"\n------------------------------------------------------";
cout<<"\nENTER YR CHOICE : ";
cin>>ch;
switch(ch)
{
case 1:
b.getpoint();
b.bres_line(ch);
getch();
break;
case 2:
b.getpoint();
b.bres_line(ch);
getch();
break;
case 3:
b.getpoint();
b.bres_line(ch);
getch();
break;
case 4:
b.getpoint();
b.bres_line(ch);
break;
case 5:
break;
}
}while(ch!=5);
return 0;
}