Sunday, May 3, 2009

C Program for Bresenham line drawing algorithm

# include <stdio.h>
# include <conio.h>
# include <graphics.h>

void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;

clrscr();

printf("\n\n\tEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);

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

p = 2 * (dy) - (dx);

x = x1;
y = y1;

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
putpixel(x,y,WHITE);

while(x <= x2)
{
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(x,y,WHITE);
}
getch();
closegraph();

}

15 comments:

Anonymous said...

this is not working for vertical line
check
(100,100,100,250)

suraj arora said...

Because this is an basic bresenham's algorithm dear. It work only for slope less than 1. Read Generalized Bresenham's algorithm for your vertical line...

delta_blog said...

To plot a pixel with 30% intensity of the drawing color,
Rohit:
how putpixel() function can be used??

will the following code work??

putpixel(20,20,0.3*Green); // 30% intensity of Green

Please help me. mail me in the following e-mail id with answer:
rohitju50@yahoo.com

israel said...

this program is not working because you compile it and it gives you no error when you run it
it gives you 4 errors
please help me with answer on my e-mail is israel0045@yahoo.fr

Anonymous said...

thanks a lot!!

Binary Option said...

thanks for sharing this code...could you please share the algorithm ?

Aditya Sriram M said...

How can we make this run in VC++ or MingWC++ compiler? Need help..

Anonymous said...

PROGRAM GIVES FOUR ERRORS
LINKER ERROR: UNDIFINED SYMBOL _DETECTGRAPH IN MODULE LINE.C
PLZ SEND ANSWER TO DEEP.GARG389@YAHOO.COM

princess said...

thankss alots

chandni said...

thAnkS.....!!!!!!

Anonymous said...

PROGRAM GIVES FOUR ERRORS
LINKER ERROR: UNDIFINED SYMBOL _DETECTGRAPH IN MODULE LINE.C
PLZ SEND ANSWER TO prathameshbhilare@yahoo.com

Md Tanzib Hossain said...

Use Parameter Linker.
Add these as parameter:
-L.
-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32

Anonymous said...

program shows no errors while compiling but run time it shows BGI Error:Gaphics not initialized (use 'initgraph') so plz help me to avoid this type of errors give urs answers to this jogendra_m@yahoo.com

Anonymous said...

for graphics not initialized error go to file,den chng directory,den click on TC nd den select BGI instead of bin,nd ur program wil execute

Anonymous said...

wat dhelll... its nt wrkngg... holy...