Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

1.6 Bresenham Line Drawing Algorithm

Lesson 7 of 32 in the free Computer Graphics notes on Siksha Sarovar, written by Rohit Jangra.

Goal

Draw a line using only integer arithmetic by maintaining a decision variable that tells us, after each step in the major axis, whether to also step in the minor axis.

Derivation Sketch (slope 0 < m < 1)

At step k we are at (x_k, y_k). Next pixel candidates are E = (x_k+1, y_k) and NE = (x_k+1, y_k+1). The actual line at x = x_k+1 has y = m(x_k+1) + c. Define decision variable p_k = 2 dy x_k - 2 dx y_k + 2 dy + (2c - 1) * dx, which simplifies recurrences to:

  • if p_k < 0 -> choose E, p_(k+1) = p_k + 2*dy.
  • if p_k >= 0 -> choose NE, p_(k+1) = p_k + 2dy - 2dx.

Initial p_0 = 2*dy - dx.

Worked Example

Line from (2, 3) to (10, 6). dx = 8, dy = 3.

  • p_0 = 2*3 - 8 = -2.
  • 2dy = 6, 2dy - 2dx = 6 - 16 = -10.
kxyp beforenextp after
023-2E-2+6=4
1334NE4-10=-6
244-6E-6+6=0
3540NE0-10=-10
465-10E-4
575-4E2
6852NE-8
796-8E-2
8106end--

Pixels: (2,3) (3,3) (4,4) (5,4) (6,5) (7,5) (8,5) (9,6) (10,6).

DDA vs Bresenham

FeatureDDABresenham
ArithmeticFloat + roundInteger only
SpeedSlowerFaster
Error accumulationYesNo
Hardware friendlyNoYes

Generalization

Handle 8 octants by swapping/negating x,y and stepping in the major axis. Modern GPUs use rasterizers based on edge-equation evaluation, but Bresenham remains the textbook reference for line scan-conversion.