Let us understand what is Bresenham line algorithm in computer graphics and then let us see how to implement Bresenham line drawing algorithm in C programming using different methods.
What is Bresenham Line Algorithm?
The Bresenham’s line drawing algorithm constructs a straight line using close approximation between the points on an n-dimensional bitmap image. It was actually developed to sketch lines on digital plotters but due to its extensibility and versatility, it was found to be useful for computer graphics as well.
It was actually developed to sketch lines on digital plotters but due to its extensibility and versatility, it was found to be useful for computer graphics as well. It makes use of pixels concept to draw a curve or a straight line.
It is a highly efficient incremental method to scan and convert the lines as compared to the DDA line drawing algorithm. However, it requires the line coordinates to be of integer type.
The Bresenham algorithm is extremely simple to implement. Along with this, it also provides speed and efficiency. This makes it applicable in many domains such as:
- Graphic cards
- Firmware
- Graphical libraries
- graphics hardware
Assumptions:
- The slope of the line is between 0 and 1
- x1 < x2 and y1 < y2
- The line is sketched from lower left to top right
Algorithm for Bresenham’s Line
1 2 3 4 5 6 7 8 9 | Step 1: Input: Line coordinates. Assume a positive slope. Step 2: Plot the first line cooordinate(x0, y0). Step 3: Calculate constants 2dx-dy, 2dx, 2dy, dx, dy. It provides the starting value for decision variable: pixel = 2dx-dy. Step 4: At every iteration Y(count), verify: If(pixel(count) < 0) Then (X(count), Y(count + 1)) & (Pixel(count + 1) = Pixel(count) + 2dx) Else (X(count) + 1, Y(count + 1)) & (Piexl(count + 1) = Pixel(count) + 2dx – 2dy) Step 5: Repeat the previous step 4*dy times |
If you face any issues while compiling this line drawing graphics program, you may have to install graphics.h in your operating system.
Note: This C program for Bresenham line generation algorithm is compiled with Turbo C compiler on Microsoft Windows 10 operating system.
Method 1: C Program To Implement Bresenham Line Drawing Algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #include<stdio.h> #include<graphics.h> int main() { int graphics_mode, graphics_driver = DETECT; int x0, x1, y0, y1, x, y; int dx, dy, pixel, temp; initgraph(&graphics_driver, &graphics_mode, "c:\tc\bgi"); printf("Enter X-axis coordinate of the initial point:\t"); scanf("%d", &x0); printf("Enter Y-axis coordinate of the initial point:\t"); scanf("%d", &y1); printf("Enter X-axis coordinate of the final point:\t"); scanf("%d", &x1); printf("Enter Y-axis coordinate of the final point:\t"); scanf("%d", &y1); dx = abs(x0 - x1); dy = abs(y0 - y1); pixel = 2 * dy - dx; if(x0 > x1) { x = x1; y = y1; temp = x0; } else { x = x0; y = y0; temp = x1; } putpixel(x, y, 10); while(x < temp) { x++; if(pixel < 0) { pixel = pixel + 2 * dy; } else { y++; pixel = pixel + 2 * (dy - dx); } putpixel(x, y, 5); } closegraph(); return 0; } |
Method 2: Bresenham line drawing algorithm in C Programming using Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include<stdio.h> #include<graphics.h> #include<conio.h> void bresenham_algorithm(int x0, int x1, int y0, int y1); int main() { int graphics_driver = DETECT, graphics_mode; int x0, y0, x1, y1; initgraph(&graphics_driver, &graphics_mode, "c:\\turboc3\\bgi"); printf("Enter X-axis coordinate of the initial point:\t"); scanf("%d", &x0); printf("Enter Y-axis coordinate of the initial point:\t"); scanf("%d", &y0); printf("Enter X-axis coordinate of the final point:\t"); scanf("%d", &x1); printf("Enter Y-axis coordinate of the final point:\t"); scanf("%d", &y1); bresenham_algorithm(x0, x1, y0, y1); getch(); return 0; } void bresenham_algorithm(int x0, int x1, int y0, int y1) { int dx, dy; int temp, x, y; dx = x1 - x0; dy = y1 - y0; x = x0; y = y0; temp = 2 * dy - dx; while(x < x1) { if(temp < 0) { temp = temp + 2 * dy; } else { y = y + 1; temp = temp + 2 * dy - 2 * dx; } putpixel(x, y, 6); x++; } closegraph(); } |
Output
Enter X-axis coordinate of the initial point: 20
Enter Y-axis coordinate of the initial point: 30
Enter X-axis coordinate of the final point: 70
Enter Y-axis coordinate of the final point: 90

Let us discuss more on this Bresenham line drawing algorithm in C programming in the comment section below.
Do let us know if you have any doubts or have any information to share. For information, check Wikipedia.