1. Gyanastra
  2. Computer Graphics
  3. Graphics Systems and 2D Primitives
  4. Line Drawing Algorithms
Free lesson, no account needed 10 min read 13 practice questions after it
Asked in past papers5 times
  • UGC NET20182017201520142013

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 (x1,y1)(x_1,y_1) to (x2,y2)(x_2,y_2), slope is m=ΔyΔxm=\frac{\Delta y}{\Delta x}, where Δx=x2−x1\Delta x=x_2-x_1 and Δy=y2−y1\Delta y=y_2-y_1.
  • Line forms: the explicit form is y=mx+cy=mx+c. The implicit form used in midpoint/Bresenham is F(x,y)=Δy(x−x1)−Δx(y−y1)F(x,y)=\Delta y(x-x_1)-\Delta x(y-y_1). The parametric form of a line segment is P(t)=P1+t(P2−P1)P(t)=P_1+t(P_2-P_1), 0≤t≤10\le t\le 1.
  • Shallow and steep lines: for ∣m∣≤1|m|\le 1, x changes faster than y, so the algorithm steps in x. For ∣m∣>1|m|>1, y changes faster, so the outer loop must be over y.
  • Horizontal and vertical lines: a horizontal line has m=0m=0. A vertical line has undefined slope because Δx=0\Delta x=0.
  • 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

  1. Read the two endpoints (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2).
  2. Compute Δx=x2−x1\Delta x=x_2-x_1 and Δy=y2−y1\Delta y=y_2-y_1.
  3. Compute the number of steps as
    steps=max⁡(∣Δx∣,∣Δy∣)\text{steps}=\max(|\Delta x|,|\Delta y|) This ensures that the largest coordinate changes by 1 unit per step, so no gaps appear on the line.
  4. Compute the increments: xinc=Δxsteps,yinc=Δystepsx_{\text{inc}}=\frac{\Delta x}{\text{steps}}, \qquad y_{\text{inc}}=\frac{\Delta y}{\text{steps}}
  5. Initialize x=x1x=x_1, y=y1y=y_1, and plot (round(x),round(y))( \text{round}(x), \text{round}(y) ).
  6. Repeat steps times:
    • x=x+xincx=x+x_{\text{inc}}
    • y=y+yincy=y+y_{\text{inc}}
    • plot (round(x),round(y))( \text{round}(x), \text{round}(y) )

For ∣m∣≤1|m|\le 1, steps=∣Δx∣\text{steps}=|\Delta x|, so xinc=±1x_{\text{inc}}=\pm 1 and yinc=my_{\text{inc}}=m. For ∣m∣>1|m|>1, steps=∣Δy∣\text{steps}=|\Delta y|, so yinc=±1y_{\text{inc}}=\pm 1 and xinc=1/mx_{\text{inc}}=1/m.

Bresenham/Midpoint algorithm for 0<m<10<m<1

This is the standard case: x is the driving axis and the line is shallow. Assume integer endpoints and x2>x1x_2>x_1, Δy>0\Delta y>0.

  1. Compute Δx=x2−x1\Delta x=x_2-x_1, Δy=y2−y1\Delta y=y_2-y_1.
  2. Compute the initial decision parameter: p0=2Δy−Δxp_0=2\Delta y-\Delta x
  3. Start at (x1,y1)(x_1,y_1).
  4. At each step, after plotting the current pixel, the next x-coordinate is x+1x+1. The two pixel candidates are:
    • East pixel E: (x+1,y)(x+1,y)
    • North-East pixel NE: (x+1,y+1)(x+1,y+1)
  5. Use the decision parameter pp to choose:
    • If p<0p<0, choose E: keep yy unchanged and update p=p+2Δyp=p+2\Delta y.
    • If p≥0p\ge0, choose NE: increment yy by 1 and update p=p+2(Δy−Δx)p=p+2(\Delta y-\Delta x).
  6. Repeat until x=x2x=x_2.

The midpoint derivation is as follows. The implicit line function is F(x,y)=Δy(x−x1)−Δx(y−y1)F(x,y)=\Delta y(x-x_1)-\Delta x(y-y_1) At the midpoint between E and NE, M=(x+1,y+12)M=(x+1,y+\tfrac12), the decision value is d=F(M)=Δy−Δx2d=F(M)=\Delta y-\frac{\Delta x}{2} To avoid fractions, multiply by 2: p=2d=2Δy−Δxp=2d=2\Delta y-\Delta x If p<0p<0, the midpoint is above the ideal line, so E is closer. If p≥0p\ge0, the midpoint is on or below the line, so NE is closer.

The following flowchart shows the standard Bresenham decision loop for 0<m<10<m<1:

For slope m>1m>1, 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.

Figure. At each x, compare p with 0: choose E when p<0, otherwise choose NE and increment y; update p and x, then repeat until x=x1.

Formulas

  • Line slope: m=ΔyΔx=y2−y1x2−x1m=\frac{\Delta y}{\Delta x}=\frac{y_2-y_1}{x_2-x_1}

  • Explicit line equation: y=mx+cy=mx+c

  • Implicit line equation used in midpoint/Bresenham: F(x,y)=Δy(x−x1)−Δx(y−y1)=0F(x,y)=\Delta y(x-x_1)-\Delta x(y-y_1)=0

  • Parametric line segment: P(t)=P1+t(P2−P1),0≤t≤1P(t)=P_1+t(P_2-P_1), \quad 0\le t\le1

  • or component-wise: x(t)=x1+tΔx,y(t)=y1+tΔyx(t)=x_1+t\Delta x,\quad y(t)=y_1+t\Delta y

  • DDA steps: steps=max⁡(∣Δx∣,∣Δy∣)\text{steps}=\max(|\Delta x|,|\Delta y|)

  • DDA increments: xinc=Δxsteps,yinc=Δystepsx_{\text{inc}}=\frac{\Delta x}{\text{steps}},\quad y_{\text{inc}}=\frac{\Delta y}{\text{steps}}

  • DDA point update: xk+1=xk+xinc,yk+1=yk+yincx_{k+1}=x_k+x_{\text{inc}},\quad y_{k+1}=y_k+y_{\text{inc}}

  • Bresenham initial decision parameter for 0<m<10<m<1: p0=2Δy−Δxp_0=2\Delta y-\Delta x

  • Bresenham updates for 0<m<10<m<1: pk+1=pk+2Δyif pk<0p_{k+1}=p_k+2\Delta y \quad \text{if } p_k<0 pk+1=pk+2(Δy−Δx)if pk≥0p_{k+1}=p_k+2(\Delta y-\Delta x) \quad \text{if } p_k\ge0

Worked examples

Example 1: DDA first four plots

Draw the line from (0,0)(0,0) to (8,4)(8,4) using DDA and give the first four plotted pixels.

Δx=8,Δy=4,m=48=0.5\Delta x=8,\quad \Delta y=4,\quad m=\frac{4}{8}=0.5 Since ∣m∣<1|m|<1, x increments by 1 and y increments by 0.5.

xexact yplotted pixel
00.0(0,0)
10.5(1,1)
21.0(2,1)
31.5(3,2)

First four plots: (0,0),(1,1),(2,1),(3,2)(0,0),(1,1),(2,1),(3,2).

Example 2: DDA steps and increments

For a line from (2,3)(2,3) to (8,6)(8,6), find the number of DDA steps and the incremental values.

Δx=6,Δy=3,steps=max⁡(6,3)=6\Delta x=6,\quad \Delta y=3,\quad \text{steps}=\max(6,3)=6 xinc=66=1,yinc=36=0.5x_{\text{inc}}=\frac{6}{6}=1,\quad y_{\text{inc}}=\frac{3}{6}=0.5 First five plotted points are (2,3),(3,4),(4,4),(5,5),(6,5)(2,3),(3,4),(4,4),(5,5),(6,5), using rounding.

Example 3: Bresenham initial decision parameter

A line has Δx=9\Delta x=9 and Δy=4\Delta y=4. Compute p0p_0 and tell which pixel is selected first.

p0=2Δy−Δx=2(4)−9=8−9=−1p_0=2\Delta y-\Delta x=2(4)-9=8-9=-1 Since p0<0p_0<0, the East pixel is selected and the next decision value is: p1=p0+2Δy=−1+8=7p_1=p_0+2\Delta y=-1+8=7

Example 4: Full Bresenham trace

Trace Bresenham's algorithm for the line from (1,1)(1,1) to (8,5)(8,5).

Δx=7,Δy=4,p0=2(4)−7=1\Delta x=7,\quad \Delta y=4,\quad p_0=2(4)-7=1

xyp before decisionselected pixelp after update
111(2,2) NE-5
22-5(3,2) E3
323(4,3) NE-3
43-3(5,3) E5
535(6,4) NE-1
64-1(7,4) E7
747(8,5) NE—

The raster pixels are: (1,1),(2,2),(3,2),(4,3),(5,3),(6,4),(7,4),(8,5)(1,1),(2,2),(3,2),(4,3),(5,3),(6,4),(7,4),(8,5)

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 0<m<10<m<1, 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

FeatureDDABresenham/Midpoint
ArithmeticFloating-point/realInteger only
RoundingRequired after each incrementNot needed after initial setup
SpeedSlower due to floating operationsFaster
AccuracyGood, but floating errors can accumulateExact for integer endpoints
Decision mechanismDirect coordinate calculationInteger decision parameter
Main loopRepeated addition of incrementsInteger addition/subtraction and comparison
Slope handlingUses 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 max⁡(∣Δx∣,∣Δy∣)\max(|\Delta x|,|\Delta y|), not the sum of the differences.
  • For ∣m∣≤1|m|\le1, DDA steps in x; for ∣m∣>1|m|>1, DDA steps in y.
  • DDA plots are obtained by rounding, not truncation.
  • For 0<m<10<m<1, Bresenham's initial decision parameter is p0=2Δy−Δxp_0=2\Delta y-\Delta x.
  • In Bresenham: if p<0p<0, choose E; if p≥0p\ge0, choose NE.
  • Tie in Bresenham p=0p=0 is not a failure; it selects NE.
  • Slope m>1m>1 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 d=F(M)d=F(M), the updates are d=d+Δyd=d+\Delta y for E and d=d+Δy−Δxd=d+\Delta y-\Delta x 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 0.50.5 to 00; 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 p0=2Δy−Δxp_0=2\Delta y-\Delta x, then trace carefully.
  • Decision parameter interpretation: A negative pp means E pixel; a positive pp 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 m>1m>1, 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 0<m<10<m<1 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.

Pages 1–2 of 2