Resources » Articles/Knowledge Sharing » Computer & Technology

# c program for bresenham line drawing algorithm

 Updated: 05-Dec-2008 Category: Computer & Technology Author: ramya Member Level: Bronze Points: 2

This is a C program for implementing bresenham line drawing algorithm.The explanation of the coding is as follows:
Get the two end points for a line and store it into the frame buffer.Then calculate the decision parameter,p.then if p>0,then the p=p+2dy,else p=p+2(dy-dx).
Open Turbo C windows and type the coding.then compile and run the program by specifying the end points.the program is as follows.

BRESENHAM’S ALGORITHM FOR LINE DRAWING

#include
#include
#include
void linebres(int,int,int,int);
int a,b,c,d;
int main()
{
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode," ");
printf("enter x co ordinates");
scanf("%d %d",&a,&b);
printf("enter y co ordinates");
scanf("%d %d",&c,&d);
linebres(a,b,c,d);
getch();
closegraph();
return 0;
}
void linebres(int xa,int ya,int xb,int yb)
{
int dx=abs(xb-xa),dy=abs(yb-ya);
int p=2*dy-dx;
int twody=2*dy,twodydx=2*(dy-dx);
int x,y,xend;
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
while(x{
x++;
if(p<0)
p+=twody;
else
{
y++;
p+=twodydx;
}
putpixel(x,y,1);
}
}

Did you like this resource? Share it with your friends and show your love!

Responses to "c program for bresenham line drawing algorithm"

No responses found. Be the first to respond...

Feedbacks

## Post Comment:

• Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
• No HTML formatting and links to other web sites are allowed.
• This is a strictly moderated site. Absolutely no spam allowed.
•  Name: Sign In to fill automatically. Email: (Will not be published, but required to validate comment) Type the numbers and letters shown on the left.

### Subscribe to Email

• Get Jobs by Email
• Forum posts by Email
• Articles by Email