Unit 2: Drawing Algorithms - Computer Graphics Notes - CSU358

Unit 2: Drawing Algorithms

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:

  1. Input: Start $(x_1, y_1)$ and End $(x_2, y_2)$.
  2. Calculate Slope: $dx = x_2 - x_1$, $dy = y_2 - y_1$.
  3. Find Steps: If $|dx| > |dy|$, use $Steps = |dx|$; else $Steps = |dy|$.
  4. Calculate Increment: $X_{inc} = dx / Steps$, $Y_{inc} = dy / Steps$.
  5. 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:

  1. Constants: Calculate $2dy$ and $2dy - 2dx$.
  2. Initial Decision ($P_0$): $P_0 = 2dy - dx$.
  3. 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$.
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:

  1. Start: $(0, r)$.
  2. Initial Decision: $P_0 = 1 - r$ (assuming integer radius).
  3. 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$.
  4. 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.

Method 2: The Non-Zero Winding Rule

(Better for complex/overlapping shapes)

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.

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