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.5 DDA Line Drawing Algorithm

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

Goal

Given two integer endpoints (x1,y1) and (x2,y2), light up the pixels that best approximate the straight line segment between them.

Idea

DDA = Digital Differential Analyzer. Use the line equation y = m*x + c with slope m = dy/dx. Step in the major axis by 1 and increment the minor axis by m (or 1/m).

Algorithm Steps

  1. Compute dx = x2 - x1, dy = y2 - y1.
  2. steps = max(|dx|, |dy|).
  3. xInc = dx / steps, yInc = dy / steps.
  4. Initialize x = x1, y = y1; plot (round x, round y).
  5. Repeat steps times: x += xInc, y += yInc, plot (round x, round y).

Worked Example

Draw line from (2, 3) to (8, 6).

  • dx = 6, dy = 3, |dx| > |dy| -> steps = 6.
  • xInc = 1, yInc = 0.5.
  • Pixels:
kxyplot
02.03.0(2,3)
13.03.5(3,4)
24.04.0(4,4)
35.04.5(5,5)
46.05.0(6,5)
57.05.5(7,6)
68.06.0(8,6)

Pros and Cons

  • Pros: Simple, easy to understand, easy to implement.
  • Cons: Uses floating-point arithmetic and rounding in the inner loop -> slow on hardware without an FPU; accumulated rounding errors on long lines.

When to Use

  • Teaching, software prototyping where speed is not critical.
  • Real hardware uses Bresenham (next lesson) which is integer-only.