Bresenham Line Drawing Algorithm in C and C++

Bresenham Line Drawing Algorithm in C and C++

Here, you will know about Bresenham Line Drawing Algorithm and get the example code of Bresenham Line Drawing Algorithm in C and C++. In the following examples, we will be using the turbo c++ compiler to run graphics program.

bresenham line drawing algorithm in C

 

Bresenham line drawing algorithm is an efficient way to plot a line. It’s an incremental algorithm that calculates the next pixel to draw depending on the current one.

The Bresenham Line Drawing Algorithm is very fast, accurate and efficient in computer graphics.

 

Bresenham Line Drawing Algorithm

Step 1: get input (x1, y1) and (x2, y2)

Step 2: search dx = x2 – x1 and dy = y2 – y1

Step 3: x=x1 and y=y1

Step 4: p=2*dy-dx

Step 5:

while(x<x2)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}

Program for Bresenham Line Drawing Algorithm in C

 

Program for Bresenham Line Drawing Algorithm in C++

Output:
bresenham line drawing

Other Line Drawing Algorithms

1.  DDA Line Drawing Algorithm

2.  Mid Point Line Drawing Algorithm

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top