BRESENHAMs Line Drawing Algorithm
BRESENHAMs Line Drawing Algorithm
using namespace std;
#include<iostream>
#include<graphics.h>
#include<math.h>
class BRES
{
int xmid,ymid;
float x1,y1,x2,y2;
public :
void quad();
void simple(float,float,float,float);
void dot(float,float,float,float);
void dash(float,float,float,float);
void dashdot(float,float,float,float);
void thick(float,float,float,float,int);
};
void BRES :: quad()
{
int xmax=640,ymax=480;
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
xmid=xmax/2;
ymid=ymax/2;
line(0,ymid,xmax,ymid);
line(xmid,0,xmid,ymax);
}
void BRES :: simple(float x1,float y1,float x2,float y2)
{
int i,j,p,x,y;
float dx,dy;
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
for(i=x;i<=x2;i++)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(320+x,240-y,18);
}
}
void BRES :: dot(float x1,float y1,float x2,float y2)
{
int i,j,p,x,y;
float dx,dy;
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
for(i=x;i<=x2;i++)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
if(i%2==0)
putpixel(320+x,240-y,18);
}
}
void BRES :: dash(float x1,float y1,float x2,float y2)
{
int i,j,p,x,y;
float dx,dy;
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
for(i=x;i<=x2;i++)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
if(i%5<3)
putpixel(320+x,240-y,18);
}
}
void BRES :: dashdot(float x1,float y1,float x2,float y2)
{
int i,j,p,x,y;
float dx,dy;
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
for(i=x;i<=x2;i++)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
if(i%6<3 || i%6==4)
putpixel(320+x,240-y,18);
}
}
void BRES :: thick(float x1,float y1,float x2,float y2,int t)
{
int i,j,p,x,y;
float dx,dy;
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
for(i=x;i<=x2;i++)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
for(j=0;j<t;j++)
putpixel(320+x,240-y+j,18);
}
}
int main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2;
int ans, ch,t;
BRES p;
do
{
cout<<" Enter X1 : ";
cin>>x1;
cout<<" Enter Y1 : ";
cin>>y1;
cout<<" Enter X2 : ";
cin>>x2;
cout<<" Enter Y2 : ";
cin>>y2;
cout<<" Menu:";
cout<<" 1.Simple line 2.Dashed line 3.Dotted line 4.Center line 5.thick";
cout<<" Enter choice : ";
cin>>ch;
switch(ch)
{
case 1:
p.quad();
if(x1<x2 || y1<y2 )
p.simple(x1,y1,x2,y2);
else if (x1==x2 && y1==y2)
putpixel(320+x1,240-y1,15);
else
p.simple(x2,y2,x1,y1);
break;
case 2:
p.quad();
if(x1<x2 || y1<y2 )
p.dash(x1,y1,x2,y2);
else if (x1==x2 && y1==y2)
putpixel(320+x1,240-y1,15);
else
p.dash(x2,y2,x1,y1);
break;
case 3:
p.quad();
if(x1<x2 || y1<y2 )
p.dot(x1,y1,x2,y2);
else if (x1==x2 && y1==y2)
putpixel(320+x1,240-y1,15);
else
p.dot(x2,y2,x1,y1);
break;
case 4:
p.quad();
if(x1<x2 || y1<y2 )
p.dashdot(x1,y1,x2,y2);
else if (x1==x2 && y1==y2)
putpixel(320+x1,240-y1,15);
else
p.dashdot(x2,y2,x1,y1);
break;
case 5:
printf(" Enter the thick ness of line");
scanf("%d",&t);
p.quad();
if(x1<x2 || y1<y2 )
p.thick(x1,y1,x2,y2,t);
else if (x1==x2 && y1==y2)
putpixel(320+x1,240-y1,15);
else
p.thick(x2,y2,x1,y1,t);
break;
}
getch();
closegraph();
cout<<"Do You Want To Continue : ";
cin>>ans;
}while(ans==1);
getch();
return 0;
}
#include<iostream>
#include<graphics.h>
#include<math.h>
class BRES
{
int xmid,ymid;
float x1,y1,x2,y2;
public :
void quad();
void simple(float,float,float,float);
void dot(float,float,float,float);
void dash(float,float,float,float);
void dashdot(float,float,float,float);
void thick(float,float,float,float,int);
};
void BRES :: quad()
{
int xmax=640,ymax=480;
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
xmid=xmax/2;
ymid=ymax/2;
line(0,ymid,xmax,ymid);
line(xmid,0,xmid,ymax);
}
void BRES :: simple(float x1,float y1,float x2,float y2)
{
int i,j,p,x,y;
float dx,dy;
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
for(i=x;i<=x2;i++)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(320+x,240-y,18);
}
}
void BRES :: dot(float x1,float y1,float x2,float y2)
{
int i,j,p,x,y;
float dx,dy;
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
for(i=x;i<=x2;i++)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
if(i%2==0)
putpixel(320+x,240-y,18);
}
}
void BRES :: dash(float x1,float y1,float x2,float y2)
{
int i,j,p,x,y;
float dx,dy;
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
for(i=x;i<=x2;i++)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
if(i%5<3)
putpixel(320+x,240-y,18);
}
}
void BRES :: dashdot(float x1,float y1,float x2,float y2)
{
int i,j,p,x,y;
float dx,dy;
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
for(i=x;i<=x2;i++)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
if(i%6<3 || i%6==4)
putpixel(320+x,240-y,18);
}
}
void BRES :: thick(float x1,float y1,float x2,float y2,int t)
{
int i,j,p,x,y;
float dx,dy;
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
for(i=x;i<=x2;i++)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
for(j=0;j<t;j++)
putpixel(320+x,240-y+j,18);
}
}
int main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2;
int ans, ch,t;
BRES p;
do
{
cout<<" Enter X1 : ";
cin>>x1;
cout<<" Enter Y1 : ";
cin>>y1;
cout<<" Enter X2 : ";
cin>>x2;
cout<<" Enter Y2 : ";
cin>>y2;
cout<<" Menu:";
cout<<" 1.Simple line 2.Dashed line 3.Dotted line 4.Center line 5.thick";
cout<<" Enter choice : ";
cin>>ch;
switch(ch)
{
case 1:
p.quad();
if(x1<x2 || y1<y2 )
p.simple(x1,y1,x2,y2);
else if (x1==x2 && y1==y2)
putpixel(320+x1,240-y1,15);
else
p.simple(x2,y2,x1,y1);
break;
case 2:
p.quad();
if(x1<x2 || y1<y2 )
p.dash(x1,y1,x2,y2);
else if (x1==x2 && y1==y2)
putpixel(320+x1,240-y1,15);
else
p.dash(x2,y2,x1,y1);
break;
case 3:
p.quad();
if(x1<x2 || y1<y2 )
p.dot(x1,y1,x2,y2);
else if (x1==x2 && y1==y2)
putpixel(320+x1,240-y1,15);
else
p.dot(x2,y2,x1,y1);
break;
case 4:
p.quad();
if(x1<x2 || y1<y2 )
p.dashdot(x1,y1,x2,y2);
else if (x1==x2 && y1==y2)
putpixel(320+x1,240-y1,15);
else
p.dashdot(x2,y2,x1,y1);
break;
case 5:
printf(" Enter the thick ness of line");
scanf("%d",&t);
p.quad();
if(x1<x2 || y1<y2 )
p.thick(x1,y1,x2,y2,t);
else if (x1==x2 && y1==y2)
putpixel(320+x1,240-y1,15);
else
p.thick(x2,y2,x1,y1,t);
break;
}
getch();
closegraph();
cout<<"Do You Want To Continue : ";
cin>>ans;
}while(ans==1);
getch();
return 0;
}
Comments
Post a Comment