Saturday, August 25, 2007

PROGRAM FOR DRAWING A DOTTED LINE AND DASHED LINE USING BRESENHAM'S ALGORITHM

/*PROGRAM FOR DRAWING A DOTTED LINE USING BRESENHAM'S ALGORITHM*/

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
#include<dos.h>

void main()
{
int x1,x2,y1,y2,dx,dy,i,k,p,xend,yend,cnt=0;
float xinc,yinc,x,y,m;
int round(float);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

setbkcolor(5);
//cleardevice();
cout<<"ENTER THE VALUE OF X1: "<<endl;
cin>>x1;
cout<<"ENTER THE VALUE OF y1: "<<endl;
cin>>y1;

cout<<"ENTER THE VALUE OF X2: "<<endl;
cin>>x2;
cout<<"ENTER THE VALUE OF y2: "<<endl;
cin>>y2;
cleardevice();
// putpixel(x1,y1,1);
// putpixel(x2,y2,1);

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

if (abs(dx) > abs(dy))
xend=abs(dx);
else
xend=abs(dy);

m=dy/(float)dx;
if(x2 > x1)
{
x=x1;
y=y1;

}
else
{
x=x2;
y=y2;

}


putpixel(x,y,1);

while(x < xend)
{
x=x+1;
if((cnt/2)%3 ==0)
putpixel(x,y,2);
if (p<0)
{
p=p+2*dy;
}
else
{
if(m>=0 && m<=1)
y=y+1;
else
{
if(dx==0)
x--;
y--;
}
p=p+2*(dy-dx);
}
//putpixel(x,y,1);
delay(10);
cnt++;
}
getch();
closegraph();

}


/*PROGRAM FOR DRAWING A DASHED LINE USING BRESENHAM'S ALGORITHM*/

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
#include<dos.h>

void main()
{
int x1,x2,y1,y2,dx,dy,i,k,p,xend,yend,cnt=0;
float xinc,yinc,x,y,m;
int round(float);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

setbkcolor(5);
//cleardevice();
cout<<"ENTER THE VALUE OF X1: "<<endl;
cin>>x1;
cout<<"ENTER THE VALUE OF y1: "<<endl;
cin>>y1;

cout<<"ENTER THE VALUE OF X2: "<<endl;
cin>>x2;
cout<<"ENTER THE VALUE OF y2: "<<endl;
cin>>y2;
cleardevice();
// putpixel(x1,y1,1);
// putpixel(x2,y2,1);

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

if (abs(dx) > abs(dy))
xend=abs(dx);
else
xend=abs(dy);

m=dy/(float)dx;
if(x2 > x1)
{
x=x1;
y=y1;

}
else
{
x=x2;
y=y2;

}


putpixel(x,y,1);

while(x < xend)
{
x=x+1;
if((cnt/5)%2 ==0)
putpixel(x,y,2);
if (p<0)
{
p=p+2*dy;
}
else
{
if(m>=0 && m<=1)
y=y+1;
else
{
if(dx==0)
x--;
y--;
}
p=p+2*(dy-dx);
}
//putpixel(x,y,1);
delay(10);
cnt++;
}
getch();
closegraph();

}




4 comments:

Anonymous said...

I love your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you?

Plz respond as I'm looking to create my own blog and would like to know where u got this from. thank you

my page: best automobile gps ()

Anonymous said...

This is my first time visit at here and i am actually happy to
read all at one place.

Here is my blog - skycaddie golf gps reviews - www.hr-pedia.ru -

Anonymous said...

Quality articles is the key to be a focus for the people to
pay a quick visit the site, that's what this web site is providing.

Feel free to visit my blog :: garmin approach s1 review

Anonymous said...

What's up to every one, the contents present at this web site are really amazing for people knowledge, well, keep up the nice work fellows.

Here is my weblog bushnell gps review *www.credicol.com*