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.
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 answerHide 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$:
| Case | Next $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$ | Decision | New $(x, y)$ | Next $p$ |
|---|---|---|---|---|
| 0 | 1 | $\ge 0$: x+1, y-1 | (7, 11) | $1-6=-5$ |
| 1 | -5 | $<0$: x same, y-1 | (7, 10) | $-5+8=3$ |
| 2 | 3 | $\ge 0$: x+1, y-1 | (8, 9) | $3-6=-3$ |
| 3 | -3 | $<0$: x same, y-1 | (8, 8) | $-3+8=5$ |
| 4 | 5 | $\ge 0$: x+1, y-1 | (9, 7) | $5-6=-1$ |
| 5 | -1 | $<0$: x same, y-1 | (9, 6) | $-1+8=7$ |
| 6 | 7 | $\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.
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 answerHide 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)$:
- Compute $dx = x_2 - x_1$, $dy = y_2 - y_1$.
- Determine steps: $$\text{steps} = \max(|dx|, |dy|)$$
- Compute increments: $$x_{inc} = \frac{dx}{\text{steps}}, \quad y_{inc} = \frac{dy}{\text{steps}}$$
- Start at $(x_1, y_1)$. Repeatedly add increments: $$x = x + x_{inc}, \quad y = y + y_{inc}$$
- 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$:
- $dx = x_2 - x_1$, $dy = y_2 - y_1$
- Initial decision parameter: $p_0 = 2dy - dx$
- Plot $(x_1, y_1)$
- 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$
- 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}$ |
|---|---|---|---|---|---|
| 0 | 3 | $\ge 0$ | 2 | 4 | $3 - 2 = 1$ |
| 1 | 1 | $\ge 0$ | 3 | 5 | $1 - 2 = -1$ |
| 2 | -1 | $< 0$ | 4 | 5 | $-1 + 8 = 7$ |
| 3 | 7 | $\ge 0$ | 5 | 6 | $7 - 2 = 5$ |
| 4 | 5 | $\ge 0$ | 6 | 7 | $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.
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 answerHide 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
| Condition | Next Pixel | Update |
|---|---|---|
| $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)
| Condition | Next point | Update |
|---|---|---|
| $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$ | Decision | Next $(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)$. ā
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]
2075 Ā· 5 marks
Trace the Bresenhamās Line drawing algorithm for the end points(1, 1) and (8, 5). [5]
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]
2080 Ā· 5 marks
Write down algorithm steps of mid-point ellipse drawing algorithm. [5]
2078 Ā· 5 marks
Plot the 1st octant of a circle centered at origin, having the radius 10 units. [5]
2075 Ā· 5 marks
Plot the ellipse centered at (0, 0) with radius rx = 8 and ry = 6, using mid point ellipse drawing algorithm. [5]
Area Filling
2080 Ā· 5 marks
Explain the importance of filling algorithms in graphics applications. Differentiate between boundary and flood fill algorithm with algorithm. [5]
2078 Ā· 5 marks
Describe the requirement for line clipping. Explain the scan line polygon filling algorithm. [5]
Make Unit 2 stick
Practice CSC214 with flashcards & quizzes