Understanding Bresenham’s Line and Circle Drawing Algorithm
Posted by: Team Codeframer | @codeframerIntroduction
If you’ve ever tried to draw a straight line or a smooth circle on a digital canvas using nothing but square pixels, you’ve encountered the need for a smart, pixel-level drawing algorithm. That’s exactly what Bresenham’s Algorithm is about.
This blog will help you understand the algorithm first, then walk you through C code using graphics.h
, a classic graphics library widely used in computer graphics labs.
Part 1: The Need for Bresenham
When drawing on a computer screen, we work with a grid of pixels. Traditional line or circle equations like y = mx + c
or x² + y² = r²
involve floating-point numbers, which are inefficient and sometimes inaccurate on pixel displays.
Bresenham's Algorithms use only integer arithmetic to decide which pixel is the best fit at every step, making them fast, simple, and perfect for raster graphics.
Part 2: Bresenham’s Line Algorithm – Deep Dive
What it does:
It selects the nearest pixel to a theoretical line between two points (x1, y1)
and (x2, y2)
.
Core Idea:
Move in the X-direction one step at a time.
At each X, choose the Y that's closest to the ideal line.
Use a decision parameter
p
to avoid floats.
Line Drawing Logic for Slope (0 ≤ m ≤ 1):
Start from
(x1, y1)
Calculate:
dx = x2 - x1
dy = y2 - y1
p = 2*dy - dxAt each step:
If
p < 0
: move to (x+1, y),p = p + 2*dy
If
p ≥ 0
: move to (x+1, y+1),p = p + 2*dy - 2*dx
Part 3: Bresenham’s Circle Algorithm – Deep Dive
What it does:
It draws a circle centered at (xc, yc)
with radius r
, using symmetry to minimize computation.
Symmetry Hack:
Only calculate 1/8th of the circle and mirror it across the other 7 octants.
Circle Drawing Logic:
Start at
(0, r)
Initialize
p = 3 - 2*r
At each point
(x, y)
:If
p < 0
: move to(x+1, y)
,p = p + 4*x + 6
Else: move to
(x+1, y-1)
,p = p + 4*(x - y) + 10
Part 4: Bresenham’s Line Algorithm Using graphics.h
>_ C1#include <graphics.h> 2#include <stdio.h> 3#include <stdlib.h> 4 5void bresenhamLine(int x1, int y1, int x2, int y2) { 6 int dx = abs(x2 - x1), dy = abs(y2 - y1); 7 int sx = (x2 > x1) ? 1 : -1; 8 int sy = (y2 > y1) ? 1 : -1; 9 int err = dx - dy; 10 11 while (1) { 12 putpixel(x1, y1, WHITE); 13 if (x1 == x2 && y1 == y2) break; 14 15 int e2 = 2 * err; 16 if (e2 > -dy) { err -= dy; x1 += sx; } 17 if (e2 < dx) { err += dx; y1 += sy; } 18 } 19} 20 21int main() { 22 int gd = DETECT, gm; 23 initgraph(&gd, &gm, ""); 24 25 bresenhamLine(100, 100, 300, 150); 26 27 getch(); 28 closegraph(); 29 return 0; 30}
Part 5: Bresenham’s Circle Algorithm Using graphics.h
>_ C1#include <graphics.h> 2#include <stdio.h> 3 4void plotCirclePoints(int xc, int yc, int x, int y) { 5 putpixel(xc + x, yc + y, WHITE); 6 putpixel(xc - x, yc + y, WHITE); 7 putpixel(xc + x, yc - y, WHITE); 8 putpixel(xc - x, yc - y, WHITE); 9 putpixel(xc + y, yc + x, WHITE); 10 putpixel(xc - y, yc + x, WHITE); 11 putpixel(xc + y, yc - x, WHITE); 12 putpixel(xc - y, yc - x, WHITE); 13} 14 15void bresenhamCircle(int xc, int yc, int r) { 16 int x = 0, y = r; 17 int p = 3 - 2 * r; 18 19 while (x <= y) { 20 plotCirclePoints(xc, yc, x, y); 21 x++; 22 if (p < 0) { 23 p = p + 4 * x + 6; 24 } else { 25 y--; 26 p = p + 4 * (x - y) + 10; 27 } 28 } 29} 30 31int main() { 32 int gd = DETECT, gm; 33 initgraph(&gd, &gm, ""); 34 35 bresenhamCircle(250, 250, 100); 36 37 getch(); 38 closegraph(); 39 return 0; 40}
Conclusion
Bresenham’s Algorithm is a perfect blend of mathematics, logic, and programming optimization. It avoids floating points, leverages symmetry, and is ideal for pixel-based graphics rendering.
Whether you're drawing a line, circle, or ellipse, Bresenham’s technique is your foundational tool in the world of computer graphics.