Chapter 5: Fundamental Drawing Algorithms
To render shapes on a raster display, we need algorithms that determine which pixels should be "on" to represent a geometric primitive. This process is called scan conversion or rasterization.
Line Drawing Algorithms
A line connects two points: and . The equation of a line is , where is the slope ().
DDA (Digital Differential Analyzer)
The DDA algorithm is a simple incremental method for scan conversion of lines.
- Calculate and .
- The number of steps is .
- Calculate the increment for each step: and .
- In each step, increment and and plot
round(x), round(y).
- Pros: Simple to implement.
- Cons: Requires floating-point operations and rounding, which are slower than integer arithmetic.
Bresenham's Line Algorithm
Bresenham's algorithm is a more efficient line-drawing method that uses only integer arithmetic. It determines the pixel coordinates by considering the error between the true line and the discrete pixel positions.
For a slope :
- Initialize , , , .
- The decision parameter .
- For each from to :
- If : The next pixel is and .
- Else: The next pixel is and .
- Pros: Fast, integer-only, highly efficient for hardware implementation.
Circle Drawing Algorithms
Drawing a circle is more complex because it's non-linear. The equation is .
Midpoint Circle Algorithm
This algorithm uses the symmetry of a circle. We only need to calculate the points for one octant (45°) and then reflect them across the other seven.
- Initialize , .
- Initial decision parameter (or for integer-only).
- While :
- Plot in all 8 octants.
- If : , , .
- Else: , , .
Ellipse Drawing Algorithm
The Midpoint Ellipse Algorithm is similar to the circle algorithm but more complex because an ellipse has two radii ( and ) and no 8-way symmetry (only 4-way).
- The algorithm is divided into two regions based on the slope of the curve.
- Region 1: The slope is less than 1. We increment and decide whether to decrement .
- Region 2: The slope is greater than 1. We decrement and decide whether to increment .
Summary
Algorithms like DDA and Bresenham's are the "foundation stones" of 2D graphics. While modern GPUs handle these tasks in hardware, understanding the underlying math is crucial for anyone working in low-level graphics or specialized rendering systems. Bresenham's focus on integer-only operations remains a hallmark of efficient computer graphics design.