Computer Graphics · Graphics Systems and 2D Primitives
Line Drawing Algorithms
Overview
Line drawing algorithms are the basic rasterization techniques that convert a mathematically defined straight line segment into a set of pixels on a raster display.
Since a raster screen has only integer pixel positions, the goal is to choose the pixels that are closest to the ideal continuous line.
This topic is foundational in Computer Graphics and belongs to the "Graphics Systems and 2D Primitives" area. The two algorithms relevant for UGC NET are DDA and Bresenham/Midpoint.
Before studying them, a student should know the Cartesian coordinate system, the equation of a straight line, slope, rounding of fractional values, and the meaning of incremental computation.
Key concepts
- Rasterization/scan conversion: the process of converting a continuous geometric primitive, such as a line, into discrete pixel coordinates on a raster grid.
- DDA (Digital Differential Analyzer): an incremental line-drawing method that uses floating-point arithmetic. It computes intermediate points by repeatedly adding small increments to the current coordinates.
- Bresenham's line algorithm: a rasterization method that uses only integer arithmetic in the main loop. This makes it faster than DDA and ideal for hardware implementation.
It is based on a decision parameter that selects the closest pixel at each integer x-step. - Midpoint line algorithm: mathematically equivalent to Bresenham's algorithm. It uses the implicit form of the line equation and evaluates it at the midpoint between two candidate pixels.
- Slope: for a line from to , slope is , where and .
- Line forms: the explicit form is . The implicit form used in midpoint/Bresenham is . The parametric form of a line segment is , .
- Shallow and steep lines: for , x changes faster than y, so the algorithm steps in x. For , y changes faster, so the outer loop must be over y.
- Horizontal and vertical lines: a horizontal line has . A vertical line has undefined slope because .
- Aliasing: drawing a continuous line on a discrete pixel grid produces a stair-step appearance, especially for diagonal lines. This is a side effect of the sampling process.
How it works
DDA algorithm
- Read the two endpoints and .
- Compute and .
- Compute the number of steps as
This ensures that the largest coordinate changes by 1 unit per step, so no gaps appear on the line. - Compute the increments:
- Initialize , , and plot .
- Repeat
stepstimes:- plot
For , , so and . For , , so and .
Bresenham/Midpoint algorithm for
This is the standard case: x is the driving axis and the line is shallow. Assume integer endpoints and , .
- Compute , .
- Compute the initial decision parameter:
- Start at .
- At each step, after plotting the current pixel, the next x-coordinate is . The two pixel candidates are:
- East pixel E:
- North-East pixel NE:
- Use the decision parameter to choose:
- If , choose E: keep unchanged and update .
- If , choose NE: increment by 1 and update .
- Repeat until .
The midpoint derivation is as follows. The implicit line function is At the midpoint between E and NE, , the decision value is To avoid fractions, multiply by 2: If , the midpoint is above the ideal line, so E is closer. If , the midpoint is on or below the line, so NE is closer.
The following flowchart shows the standard Bresenham decision loop for :
For slope , the roles of x and y are exchanged. The algorithm steps in y, and the candidates are North and North-East. For negative slopes, the same logic applies with appropriate sign changes, generally handled by symmetry across octants.
Drawing the diagram…
Formulas
-
Line slope:
-
Explicit line equation:
-
Implicit line equation used in midpoint/Bresenham:
-
Parametric line segment:
-
or component-wise:
-
DDA steps:
-
DDA increments:
-
DDA point update:
-
Bresenham initial decision parameter for :
-
Bresenham updates for :
Worked examples
Example 1: DDA first four plots
Draw the line from to using DDA and give the first four plotted pixels.
Since , x increments by 1 and y increments by 0.5.
| x | exact y | plotted pixel |
|---|---|---|
| 0 | 0.0 | (0,0) |
| 1 | 0.5 | (1,1) |
| 2 | 1.0 | (2,1) |
| 3 | 1.5 | (3,2) |
First four plots: .
Example 2: DDA steps and increments
For a line from to , find the number of DDA steps and the incremental values.
First five plotted points are , using rounding.
Example 3: Bresenham initial decision parameter
A line has and . Compute and tell which pixel is selected first.
Since , the East pixel is selected and the next decision value is:
Example 4: Full Bresenham trace
Trace Bresenham's algorithm for the line from to .
| x | y | p before decision | selected pixel | p after update |
|---|---|---|---|---|
| 1 | 1 | 1 | (2,2) NE | -5 |
| 2 | 2 | -5 | (3,2) E | 3 |
| 3 | 2 | 3 | (4,3) NE | -3 |
| 4 | 3 | -3 | (5,3) E | 5 |
| 5 | 3 | 5 | (6,4) NE | -1 |
| 6 | 4 | -1 | (7,4) E | 7 |
| 7 | 4 | 7 | (8,5) NE | — |
The raster pixels are:
Example 5: Statement-type question
Evaluate the following statements:
I. DDA uses incremental coordinate calculations to generate pixels along a line.
II. In Bresenham's algorithm for , the outer loop advances in x.
III. A positive decision parameter in the midpoint algorithm means the algorithm has failed.
Statement I is true: DDA updates x and y by adding increments. Statement II is true: for shallow lines x is the driving axis. Statement III is false: a positive value simply indicates that the North-East pixel is closer; it is a normal decision state, not a failure.
Comparison
| Feature | DDA | Bresenham/Midpoint |
|---|---|---|
| Arithmetic | Floating-point/real | Integer only |
| Rounding | Required after each increment | Not needed after initial setup |
| Speed | Slower due to floating operations | Faster |
| Accuracy | Good, but floating errors can accumulate | Exact for integer endpoints |
| Decision mechanism | Direct coordinate calculation | Integer decision parameter |
| Main loop | Repeated addition of increments | Integer addition/subtraction and comparison |
| Slope handling | Uses steps = max( | Δx |
Revision summary
- DDA stands for Digital Differential Analyzer and uses floating-point arithmetic.
- Bresenham's algorithm is attractive because it uses integer arithmetic only.
- DDA number of steps is , not the sum of the differences.
- For , DDA steps in x; for , DDA steps in y.
- DDA plots are obtained by rounding, not truncation.
- For , Bresenham's initial decision parameter is .
- In Bresenham: if , choose E; if , choose NE.
- Tie in Bresenham is not a failure; it selects NE.
- Slope means the outer loop must be over y; otherwise line gaps appear.
- Horizontal line has slope 0; vertical line has undefined slope.
- The midpoint algorithm updates the implicit line function incrementally; it does not recompute it from scratch.
- If using the unscaled midpoint function , the updates are for E and for NE.
Previous-year exam insights
- Direct concept: Bresenham's main advantage is often asked.
The expected answer is that it uses integer arithmetic only. - DDA numeric questions: UGC NET usually gives endpoints and asks for the first few plotted pixels.
Compute slope, step size, and use rounding.
A common wrong answer comes from truncating to ; standard DDA uses rounding. - Bresenham trace questions: They may provide a table of x and y values and ask which one is chosen by Bresenham.
Use , then trace carefully. - Decision parameter interpretation: A negative means E pixel; a positive means NE pixel.
A positive value is not an error. - Slope greater than one: Questions ask whether the outer loop should change to y.
The answer is true for , because y changes faster. - Statement/Assertion style: Typical statements test whether DDA uses incremental evaluation, whether Bresenham uses integer arithmetic, whether the midpoint algorithm fails for positive d, and whether slope affects the driving axis.
- Match-the-list style: Lists may include DDA, Bresenham, horizontal line, vertical line with properties like floating increments, integer decision parameter, zero slope, undefined slope.
- Trap: Do not confuse DDA with Bresenham.
DDA is easier but uses floating point; Bresenham is integer-based and faster.
Practice at PARIKSHA
You are ready for the PARIKSHA tests of this sub-topic when you can:
- State the full forms and one key property of DDA and Bresenham algorithms.
- Compute the number of DDA steps for any given endpoints.
- Trace the first few DDA plotted pixels using rounding correctly.
- Write the Bresenham initial decision parameter for any shallow line.
- Trace Bresenham's algorithm for and explain each selected pixel.
- Explain what happens when slope is greater than 1 and why the outer loop must change.
- Evaluate true/false statements about integer arithmetic, incremental updates, and decision-parameter sign.
- Distinguish the implicit, explicit, and parametric forms of a line segment.
- Solve numeric questions on initial decision parameter and the next update value.
Attempt the sub-topic test first, then the subject test.