Here is the program Bresenham line drawing algorithm in C. This is Computer Graphics algorithm, Used to draw a line.

To draw a line using the Bresenham algorithm, C compiler uses Graphics.h header file. Here we will use Turbo C compiler to compile the program.

Before you copy and start execution of the program, I would suggest completing the instructions mentioned in the article.

Also Read: Bresenham Circle Drawing Algorithm program in c

Also Read: Bresenham Circle Drawing Algorithm program in c

# 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,"c:\\tc\\bgi");

putpixel(x,y,3);

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,3);

}

getch();

closegraph();

}

# 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,"c:\\tc\\bgi");

putpixel(x,y,3);

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,3);

}

getch();

closegraph();

}

Also Read:

## 3 Comments

algorithme 3D si possible

ReplyDeletewhat does the variable "p" stand for?

ReplyDeleteTry to understand this..

Deletehttp://www.tutorialspoint.com/computer_graphics/line_generation_algorithm.htm