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();

}

36 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

Anonymous 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...

Anonymous said...

SEARCH THE NET FOR HOW TO LINK THE GRAPHICS LIBRARY...

Anonymous said...

how to draw line(600,200,800,100)

Anonymous said...

WELL GUD JOB THNX FOR SHARING UR IDEAS

Anonymous said...

Well said macha joyson

Anonymous said...

YA MACHAN KAUSHICK PARAMASIVAM.E SAY HATS OF TO THE THINK

Anonymous said...

HIII KAUSHICK,JOYSON WATSUP.....GUYS...IT'S SO INTRESTING PROGRAM...

Anonymous said...

AMA MACHAN NALLA IRUKU RMBA NALLA IRUKU @U.KANNAN PANDII

Anonymous said...

Hi Hello Vanakkam @Kannan what about ur Digital System ?

Anonymous said...

HI @KAUSHICK...ITS CHO CHO EASY....ITS CENTUM I THINK SO...

Vishnu said...

Hi @bala

Anonymous said...

HAI KANNAN IM RATHAN

WHTS ABOUT UR AREARS

BALA said...

HII VISHNU..

MARII said...

HAI GUYS HE R U IM FINE I WISH THE SAME WITH U AL

KANNAN HW S U PHD ABOUT PHYTHON,JAVA,.NET

Anonymous said...

gandu tera program bhi gandu jaisa hai..... bhosdiwale yeh ubuntu lavde par chalta hi nahi.... maa ki chut error hi deta hai... lund ke bal agar program likhna nhi aata toh kyu blog bana ke apni maa chudate ho naam jayant hai mera sabka mooh mein leta hoon

Unknown said...
This comment has been removed by the author.
Unknown said...

FOR RUN THIS PROGRAM
LINK GRAPHICS LIBRARY

Phoenix Techie said...

Contact 7025294384 WhatsApp

Phoenix Techie said...

Contact 7025294384 WhatsApp

Unknown said...

View of OS as an Extended Machine and Resource Manager
Features of 80486 and Pentium Processor
AMP Module
One-pass Macro Processors
Dependency Graph
Storage Organization
Interrupt Cycle
Classification and Prediction
Web Application Design

Tutorialspoint said...


SAP Tutorial online training
SAP HANA BI Development Tutorial
SAP HANA Administration Tutorial
SAP FICO Tutorial
SAP CRM Tutorial
SAP CCA Tutorial

Unknown said...

this is working or not I am not check out but I know it is working