2 Scan Conversion Algorithm

Computer Graphics Ā· Unit 2 Ā· 6 hrs

Scan Conversion Algorithm

Exam-focused notes for Scan Conversion Algorithm (Computer Graphics, CSC214): what the TU syllabus asks and how it has actually been tested, with 11 solved past questions from this unit.

Scan Conversion Algorithm in short

Scan conversion is the process of deciding exactly which pixels on a raster display should be lit to approximate a mathematical primitive: a point, a straight line, a circle, an ellipse, or the interior of a shape. This chapter works through the standard incremental algorithms for lines and curves and the standard algorithms for filling closed areas.

Scan Conversion Algorithm: notes

Scan Converting a Point and a straight Line: DDA Line Algorithm, Bresenham's Line Algorithm

A point is scan converted trivially, by lighting the single pixel nearest its coordinates. A straight line is harder: a line between two integer endpoints $(x_0, y_0)$ and $(x_1, y_1)$ almost never passes exactly through the centres of the intervening pixels, so an algorithm must choose, at every step, which pixel best approximates the true line.

Digital Differential Analyzer (DDA) algorithm. The DDA algorithm treats the line as a sequence of small, equal steps in whichever of $x$ or $y$ changes faster. Let $\mathrm{d}x = x_1 - x_0$ and $\mathrm{d}y = y_1 - y_0$. The number of steps is $\text{steps} = \max(|\mathrm{d}x|, |\mathrm{d}y|)$, and the increment per step in each direction is $$ x_{\text{inc}} = \frac{\mathrm{d}x}{\text{steps}}, \qquad y_{\text{inc}} = \frac{\mathrm{d}y}{\text{steps}}. $$

Starting at $(x_0, y_0)$, each step adds $x_{\text{inc}}$ and $y_{\text{inc}}$ to the running (floating point) coordinates and plots the rounded pixel. Since exactly one of $x_{\text{inc}}, y_{\text{inc}}$ has magnitude $1$, the algorithm always advances by a full pixel in the dominant direction, and by a fractional, rounded amount in the other.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void dda_line(int x0, int y0, int x1, int y1) {
    int dx = x1 - x0;
    int dy = y1 - y0;
    int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
    float xinc = (float) dx / steps;
    float yinc = (float) dy / steps;
    float x = x0, y = y0;
    printf("(%d, %d)\n", (int) round(x), (int) round(y));
    for (int k = 0; k < steps; k++) {
        x += xinc;
        y += yinc;
        printf("(%d, %d)\n", (int) round(x), (int) round(y));
    }
}

Worked example. Plot the line from $(5,4)$ to $(12,7)$. Here $\mathrm{d}x = 7$, $\mathrm{d}y = 3$, so $\text{steps} = 7$, $x_{\text{inc}} = 1$, $y_{\text{inc}} = 3/7 \approx 0.4286$:

$k$$x$$y$Plotted $(x,y)$
05.004.00(5, 4)
16.004.43(6, 4)
27.004.86(7, 5)
38.005.29(8, 5)
49.005.71(9, 6)
510.006.14(10, 6)
611.006.57(11, 7)
712.007.00(12, 7)

The DDA algorithm is simple but uses floating point addition and rounding at every step, which is slower than pure integer arithmetic.

Bresenham's line algorithm. Bresenham's algorithm produces the identical set of pixels using only integer addition, subtraction and comparison, by tracking a decision parameter that records, in scaled integer form, which of the two candidate pixels is closer to the true line. For a line with $0 < \mathrm{d}y < \mathrm{d}x$ starting at $(x_0, y_0)$, the initial decision parameter is $$ p_0 = 2,\mathrm{d}y - \mathrm{d}x, $$

What this unit covers

  • Scan Converting a Point and a straight Line: DDA Line Algorithm, Bresenham's Line Algorithm
  • Scan Converting Circle and Ellipse: Mid Point Circle and Ellipse Algorithm
  • Area Filling: Scan Line Polygon fill Algorithm, Inside-outside Test, Scan line fill of Curved Boundary area, Boundary-fill and Flood-fill algorithm

Scan Converting a Point and a straight Line

2081 Ā· 10 marks

Derive the expression for Bresenham's Line Drawing algorithm. Trace the points in the line path with starting point (6, 12) and end point (10, 5) using Bresenham’s line drawing algorithm.[10]

Show solved answer

Bresenham's Line Drawing Algorithm

Part 1: Derivation

Concept

Bresenham's algorithm draws a line using only integer arithmetic (addition, subtraction, and multiplication by 2). It selects, at each step, the pixel nearest to the true line path.

Derivation (for slope $0 < m < 1$)

Assume we have plotted pixel $(x_k, y_k)$. The next pixel is either:

  • E: $(x_k+1, y_k)$
  • NE: $(x_k+1, y_k+1)$

True line at $x = x_k+1$: $$y = m(x_k+1) + b, \qquad m = \frac{\Delta y}{\Delta x}$$

Distances from true line to the two candidates: $$d_1 = y - y_k = m(x_k+1) + b - y_k$$ $$d_2 = (y_k+1) - y = y_k + 1 - m(x_k+1) - b$$

$$d_1 - d_2 = 2m(x_k+1) + 2b - 2y_k - 1$$

Define decision parameter $p_k = \Delta x (d_1 - d_2)$ (multiply by $\Delta x > 0$ to remove the fraction):

$$p_k = 2\Delta y \cdot x_k - 2\Delta x \cdot y_k + c, \quad c = 2\Delta y + \Delta x(2b-1)$$

Rule:

  • If $p_k < 0$: choose E, plot $(x_k+1, y_k)$
  • If $p_k \ge 0$: choose NE, plot $(x_k+1, y_k+1)$

Recurrence

$$p_{k+1} - p_k = 2\Delta y (x_{k+1}-x_k) - 2\Delta x (y_{k+1}-y_k)$$

Since $x_{k+1} = x_k + 1$:

CaseNext $y$Update
$p_k < 0$$y_k$$p_{k+1} = p_k + 2\Delta y$
$p_k \ge 0$$y_k+1$$p_{k+1} = p_k + 2\Delta y - 2\Delta x$

Initial value: $$\boxed{p_0 = 2\Delta y - \Delta x}$$


Part 2: Tracing (6, 12) to (10, 5)

Parameters

$$x_1=6,\ y_1=12,\ x_2=10,\ y_2=5$$ $$\Delta x = |10-6| = 4, \qquad \Delta y = |5-12| = 7$$

Slope magnitude $= \frac{7}{4} = 1.75 > 1$ → steep line, so we step along the y-axis.

Since $x$ increases ($6 \to 10$) and $y$ decreases ($12 \to 5$), we step -1 in y each step and possibly +1 in x.

Adjusted rules (driving variable = y, $\Delta y = 7$ steps)

$$p_0 = 2\Delta x - \Delta y = 2(4) - 7 = 1$$

  • $2\Delta x = 8$
  • $2\Delta x - 2\Delta y = 8 - 14 = -6$

Rules (step $y \to y-1$ each iteration):

  • If $p_k < 0$: $x$ stays, $p_{k+1} = p_k + 8$
  • If $p_k \ge 0$: $x \to x+1$, $p_{k+1} = p_k - 6$

Trace Table

Start plotted: $(6, 12)$, $p_0 = 1$

Step $k$$p_k$DecisionNew $(x, y)$Next $p$
01$\ge 0$: x+1, y-1(7, 11)$1-6=-5$
1-5$<0$: x same, y-1(7, 10)$-5+8=3$
23$\ge 0$: x+1, y-1(8, 9)$3-6=-3$
3-3$<0$: x same, y-1(8, 8)$-3+8=5$
45$\ge 0$: x+1, y-1(9, 7)$5-6=-1$
5-1$<0$: x same, y-1(9, 6)$-1+8=7$
67$\ge 0$: x+1, y-1(10, 5)end

Points Plotted

$$(6,12),\ (7,11),\ (7,10),\ (8,9),\ (8,8),\ (9,7),\ (9,6),\ (10,5)$$

The endpoint $(10, 5)$ is reached correctly after 7 steps.

Open this answer on its own page →

2080 Ā· 10 marks

What is a Digital Differential Analyzer (DDA)? How can you draw the line using this algorithm? Trace the points on the line path between A (1,3) and B (6,7) using Bresenham's line drawing algorithm.[10]

Show solved answer

Digital Differential Analyzer (DDA) and Bresenham's Line Drawing

Part 1: What is DDA?

A Digital Differential Analyzer (DDA) is an incremental scan-conversion line drawing algorithm. It computes points along a line by sampling the line at unit intervals in one coordinate and calculating the corresponding value of the other coordinate using the line's slope. It uses floating-point arithmetic and rounding.

Drawing a line using DDA

For endpoints $(x_1, y_1)$ and $(x_2, y_2)$:

  1. Compute $dx = x_2 - x_1$, $dy = y_2 - y_1$.
  2. Determine steps: $$\text{steps} = \max(|dx|, |dy|)$$
  3. Compute increments: $$x_{inc} = \frac{dx}{\text{steps}}, \quad y_{inc} = \frac{dy}{\text{steps}}$$
  4. Start at $(x_1, y_1)$. Repeatedly add increments: $$x = x + x_{inc}, \quad y = y + y_{inc}$$
  5. Plot $(\text{round}(x), \text{round}(y))$ at each step until all steps done.

DDA is simple but slower and prone to rounding error accumulation because of floating-point operations.


Part 2: Bresenham's Line Drawing Algorithm

Bresenham's algorithm uses only integer addition, subtraction and comparison, avoiding floating point. It chooses the next pixel using a decision parameter.

For a line with $0 \le m \le 1$:

  1. $dx = x_2 - x_1$, $dy = y_2 - y_1$
  2. Initial decision parameter: $p_0 = 2dy - dx$
  3. Plot $(x_1, y_1)$
  4. At each step:
    • If $p_k < 0$: $x_{k+1} = x_k + 1$, $y_{k+1} = y_k$, and $p_{k+1} = p_k + 2dy$
    • If $p_k \ge 0$: $x_{k+1} = x_k + 1$, $y_{k+1} = y_k + 1$, and $p_{k+1} = p_k + 2dy - 2dx$
  5. Repeat $dx$ times.

Part 3: Tracing A(1,3) to B(6,7) using Bresenham

Given

  • $(x_1, y_1) = (1, 3)$
  • $(x_2, y_2) = (6, 7)$

Step 1: Compute dx, dy

$$dx = 6 - 1 = 5, \quad dy = 7 - 3 = 4$$

Slope $m = 4/5 = 0.8$, so $0 \le m \le 1$; step along x. Total steps $= dx = 5$.

Step 2: Initial decision parameter

$$p_0 = 2dy - dx = 2(4) - 5 = 8 - 5 = 3$$

Step 3: Constants

$$2dy = 8, \quad 2dy - 2dx = 8 - 10 = -2$$

Step 4: Iteration table

Plot starting point $(1,3)$.

k$p_k$Condition$x_{k+1}$$y_{k+1}$$p_{k+1}$
03$\ge 0$24$3 - 2 = 1$
11$\ge 0$35$1 - 2 = -1$
2-1$< 0$45$-1 + 8 = 7$
37$\ge 0$56$7 - 2 = 5$
45$\ge 0$67$5 - 2 = 3$

Step 5: Final plotted points

$$\boxed{(1,3),\ (2,4),\ (3,5),\ (4,5),\ (5,6),\ (6,7)}$$

Six pixels are plotted, ending exactly at B(6,7). āœ“

Grid visualization

7 |                     * B(6,7)
6 |                *
5 |         *   *
4 |     *
3 | * A(1,3)
  +----------------------
    1   2   3   4   5   6

The verification confirms the endpoint (6,7) is reached correctly.

Open this answer on its own page →

2079 Ā· 10 marks

Explain the working details of Mid-point circle algorithm? Trace the points along the line path having two end points (6,9) and (2,3) using Bresenham's line drawing algorithm.[10]

Show solved answer

Mid-Point Circle Algorithm and Bresenham's Line Trace


PART 1: Mid-Point Circle Algorithm

Concept

The Mid-Point Circle Algorithm draws a circle using only integer arithmetic (additions/subtractions). At each step it uses a decision parameter to choose between two candidate pixels.

Eight-Way Symmetry

A circle centered at origin has 8-fold symmetry. If $(x, y)$ lies on the circle, so do:

$$(\pm x, \pm y), \quad (\pm y, \pm x)$$

So only one octant (0 to 45°) needs to be computed; the rest are reflected.

Circle Function

For a circle of radius $r$ centered at origin:

$$f(x, y) = x^2 + y^2 - r^2$$

  • $f < 0$: point inside
  • $f = 0$: point on circle
  • $f > 0$: point outside

Decision Parameter

Starting at $(0, r)$, moving in $+x$, the next pixel is either E $(x+1, y)$ or SE $(x+1, y-1)$. The midpoint is $\left(x+1, y-\tfrac{1}{2}\right)$:

$$P_k = f\left(x_k+1,\ y_k-\tfrac{1}{2}\right) = (x_k+1)^2 + \left(y_k-\tfrac{1}{2}\right)^2 - r^2$$

Initial value:

$$P_0 = \tfrac{5}{4} - r \approx 1 - r$$

Decision Rule

ConditionNext PixelUpdate
$P_k < 0$E $(x+1, y)$$P_{k+1} = P_k + 2x_{k+1} + 1$
$P_k \ge 0$SE $(x+1, y-1)$$P_{k+1} = P_k + 2x_{k+1} - 2y_{k+1} + 1$

Algorithm

1. (x, y) = (0, r);  P = 1 - r
2. Plot (x, y) and 8 symmetric points
3. While x < y:
       x = x + 1
       if P < 0:  P = P + 2x + 1
       else:      y = y - 1;  P = P + 2x - 2y + 1
       Plot (x, y) and 8 symmetric points

Advantages

  • Integer arithmetic only, no floating point
  • Fast and efficient
  • Accurate circle approximation

PART 2: Bresenham's Line Trace from (6, 9) to (2, 3)

Given data

  • Start: $(x_1, y_1) = (6, 9)$
  • End: $(x_2, y_2) = (2, 3)$

Step 1: Differences

$$\Delta x = |x_2 - x_1| = |2 - 6| = 4$$ $$\Delta y = |y_2 - y_1| = |3 - 9| = 6$$

Since $\Delta y > \Delta x$, slope $> 1$, so we step along the y-axis and decide x.

Direction of travel (from start to end):

  • x decreases: $x_{inc} = -1$
  • y decreases: $y_{inc} = -1$

Step 2: Initial Decision Parameter

For the steep case (driving along y):

$$P_0 = 2\Delta x - \Delta y = 2(4) - 6 = 2$$

Step 3: Update Rules (driving along y)

ConditionNext pointUpdate
$P_k < 0$y steps, x fixed$P_{k+1} = P_k + 2\Delta x$
$P_k \ge 0$x steps and y steps$P_{k+1} = P_k + 2\Delta x - 2\Delta y$

where $2\Delta x = 8$ and $2\Delta x - 2\Delta y = 8 - 12 = -4$.

Step 4: Trace Table

Plot start point $(6, 9)$. Then iterate for $\Delta y = 6$ steps (y from 9 down to 3).

Step$P_k$DecisionNext $(x, y)$
start--$(6, 9)$
1$P_0 = 2 \ge 0$x--, y--: $P = 2 - 4 = -2$$(5, 8)$
2$P_1 = -2 < 0$y--: $P = -2 + 8 = 6$$(5, 7)$
3$P_2 = 6 \ge 0$x--, y--: $P = 6 - 4 = 2$$(4, 6)$
4$P_3 = 2 \ge 0$x--, y--: $P = 2 - 4 = -2$$(3, 5)$
5$P_4 = -2 < 0$y--: $P = -2 + 8 = 6$$(3, 4)$
6$P_5 = 6 \ge 0$x--, y--: $P = 6 - 4 = 2$$(2, 3)$

Final Plotted Points

$$(6, 9),\ (5, 8),\ (5, 7),\ (4, 6),\ (3, 5),\ (3, 4),\ (2, 3)$$

Verification

Line has slope $\dfrac{3-9}{2-6} = \dfrac{-6}{-4} = 1.5$. The traced points correctly approximate this steep line, ending exactly at $(2, 3)$. āœ“

Open this answer on its own page →

2077 Ā· 10 marks

List the major differences between DDA and Bradenham’s Line drawing algorithm. Illustrate the DDA algorithm to the line width end points (2, 2) and (9, 2).[10]

--- - Algorithm comparison: DDA vs Bresenham's (question spells it "Bradenham's" and "width" is a typo for "with") - Line end points: - Start: $(x1, y1) = (2, 2)$ - End: $(x2, y2) = (9, 2)$ --- Basis DDA Algorithm Bresenham's Algorithm --------- Arithmetic Uses floating-point (real) arithmetic Us...

Open this answer on its own page →

2075 Ā· 5 marks

Trace the Bresenham’s Line drawing algorithm for the end points(1, 1) and (8, 5). [5]

  • Start point: $(x1, y1) = (1, 1)$ - End point: $(x2, y2) = (8, 5)$ $$\Delta x = x2 - x1 = 8 - 1 = 7$$ $$\Delta y = y2 - y1 = 5 - 1 = 4$$ Since $\Delta x \Delta y$, we step along the x-axis (increment x by 1 each step). Initial decision parameter: $$p0 = 2\Delta y - \Delta x = 2(4) - 7 = 8 - 7 = ...
Open this answer on its own page →

Scan Converting Circle and Ellipse

2081 Ā· 5 marks

Plot the first octant of a circle centered at (-2, -2), having a radius of 5 units using the mid-point circle algorithm. [5]

  • Center: $(xc, yc) = (-2, -2)$ - Radius: $r = 5$ Compute points relative to origin starting at $(0, r)$, then translate by adding $(xc, yc)$. First octant runs while $x \le y$. Initial decision parameter: $$p0 = 1 - r = 1 - 5 = -4$$ Update rules: - If $p < 0$: $x = x+1$, $p = p + 2x + 1$ - If
Open this answer on its own page →

2080 Ā· 5 marks

Write down algorithm steps of mid-point ellipse drawing algorithm. [5]

The mid-point ellipse drawing algorithm is an efficient raster scan method that uses integer arithmetic to determine the closest pixel positions along an ellipse path. The algorithm divides the ellipse into two regions based on the slope of the curve. - Region 1: Where the slope magnitude dy/dx <...

Open this answer on its own page →

2078 Ā· 5 marks

Plot the 1st octant of a circle centered at origin, having the radius 10 units. [5]

  • Center: origin $(0, 0)$ - Radius: $r = 10$ units - Required: 1st octant only (arc from $(0, 10)$ to the $x = y$ line, i.e. 90° to 45°) Starting point: $(x0, y0) = (0, r) = (0, 10)$ Initial decision parameter: $$p0 = 1 - r = 1 - 10 = -9$$ Update rules (for 1st octant, x increasing, y decreasing)...
Open this answer on its own page →

2075 Ā· 5 marks

Plot the ellipse centered at (0, 0) with radius rx = 8 and ry = 6, using mid point ellipse drawing algorithm. [5]

--- - Center $(xc, yc) = (0, 0)$ - $rx = 8$ - $ry = 6$ Precomputed: - $rx^2 = 64$ - $ry^2 = 36$ - Starting point: $(x0, y0) = (0, ry) = (0, 6)$ --- Continue while $2 ry^2 x < 2 rx^2 y$. Initial decision parameter: $$p10 = ry^2 - rx^2 ry + \tfrac{1}{4} rx^2 = 36 - 64(6) + \tfrac{1}{4}(64) = 36 - 3...

Open this answer on its own page →

Area Filling

2080 Ā· 5 marks

Explain the importance of filling algorithms in graphics applications. Differentiate between boundary and flood fill algorithm with algorithm. [5]

Filling algorithms are fundamental in computer graphics for the following reasons: - Region coloring: They allow filling closed regions with a desired color, which is essential for rendering 2D shapes, polygons, and objects realistically. - Paint applications: Tools like the "bucket fill" in MS P...

Open this answer on its own page →

2078 Ā· 5 marks

Describe the requirement for line clipping. Explain the scan line polygon filling algorithm. [5]

--- Line clipping is the process of removing (clipping) portions of a line that lie outside a defined clipping window, retaining only the visible portion inside the window. 1. Clipping Window Definition: A rectangular clipping window must be defined by its boundaries: - Xmin, Xmax (left and right...

Open this answer on its own page →