Exam Strategy: If asked for an "Algorithm", write the steps in a numbered list exactly as shown below.
2.1 Line Drawing Algorithms
A. DDA Algorithm (Digital Differential Analyzer)
Concept: The "Calculator" method. It calculates the next point using floating-point addition (decimals).
The Recipe:
- Input: Start $(x_1, y_1)$ and End $(x_2, y_2)$.
- Calculate Slope: $dx = x_2 - x_1$, $dy = y_2 - y_1$.
- Find Steps: If $|dx| > |dy|$, use $Steps = |dx|$; else $Steps = |dy|$.
- Calculate Increment: $X_{inc} = dx / Steps$, $Y_{inc} = dy / Steps$.
- Loop: Start at $(x_1, y_1)$. Repeat Steps times:
- $x_{next} = x + X_{inc}$
- $y_{next} = y + Y_{inc}$
- Round values and plot pixel.
B. Bresenham’s Line Algorithm
Concept: The "Integer" method. Faster because it avoids decimals. It simply asks: "Should I go straight or diagonal?"
The Recipe:
- Constants: Calculate $2dy$ and $2dy - 2dx$.
- Initial Decision ($P_0$): $P_0 = 2dy - dx$.
- Loop: At each step $k$:
- If $P_k < 0$: Plot $(x+1, y)$.
Update $P_{k+1} = P_k + 2dy$. - If $P_k \ge 0$: Plot $(x+1, y+1)$.
Update $P_{k+1} = P_k + 2dy - 2dx$.
- If $P_k < 0$: Plot $(x+1, y)$.
mindmap
root((Line Strategy))
DDA (Calculator)
Decimals / Floats
Rounding Needed
Slow & Simple
Bresenham (Integer)
Integer Math Only
Decision Var (P)
Fast & Precise
2.2 Mid-Point Circle Algorithm
Analogy: Drawing a circle is hard. So we cheat. We only calculate 1/8th of the circle (pizza slice) and mirror it 8 times to get the full circle.
The Recipe:
- Start: $(0, r)$.
- Initial Decision: $P_0 = 1 - r$ (assuming integer radius).
- Loop (while $x < y$):
- Always increment $x$ ($x = x + 1$).
- If $P_k < 0$: $y$ stays same.
Update $P_{k+1} = P_k + 2x + 1$. - If $P_k \ge 0$: Decrement $y$ ($y = y - 1$).
Update $P_{k+1} = P_k + 2x - 2y + 1$.
- Plot: For every point $(x,y)$, plot its 8 reflections: $(y,x), (-x,y), (-y,x)$, etc.
mindmap
root((Circle Hacks))
The Challenge
Curves are costly
Trig is slow
The Solution
8-Way Symmetry
Calc 1 slice
Mirror 8 times
The Math
Mid-Point Algo
Integer Decisions
2.3 Polygon Filling (Coloring the Shapes)
How does the computer know which pixels are "inside" a shape to fill it with color?
Method 1: The Odd-Even Rule (Parity Rule)
Used in simpler shapes.
- How it works: Draw a horizontal line (scan line) across the screen. Count the edge intersections.
- Rule:
- Odd count (1, 3, 5): You are inside (Color ON).
- Even count (0, 2, 4): You are outside (Color OFF).
Method 2: The Non-Zero Winding Rule
(Better for complex/overlapping shapes)
- How it works: Imagine winding a string around the shape.
- Give edges a direction.
- Intersection going Up = +1.
- Intersection going Down = -1.
- Rule: If the final sum is $\neq 0$, the point is Inside. If 0, it is Outside.
Method 3: Scan-Line Fill Algorithm (Using Tables)
The efficient way computers actually do it. It doesn't check every pixel; it calculates "spans".
Keywords for Exam: Mention "Edge Tables" to score full marks.
- Global Edge Table (GET): A master list of all polygon edges, sorted by their bottom Y-coordinate.
- Active Edge Table (AET): A temporary list of edges that intersect the current scan line being drawn. We only process edges in the AET.
mindmap
root((Filling Shapes))
Inside Tests
Odd-Even Rule
Simple Shapes
Count Edges
Non-Zero Winding
Complex Shapes
Direction check
The Engine
Scan-Line Algo
Edge Tables
GET to AET