Computer Graphics Algorithms for Lines, Circles, and Curves
Posted by Anonymous and classified in Mathematics
Written on in
English with a size of 70.67 KB
In computer graphics, output primitives are the basic geometric structures used to describe and construct images. The simplest primitives are Points and Lines. Before these primitives can be displayed on a screen, they must undergo scan conversion—the process of digitizing a continuous geometric shape into a discrete grid of screen pixels.
Points and Lines
Points
A point is a zero-dimensional geometric object. In a digital system, a point is represented by an ordered pair of coordinates (X, Y). To display a point, the graphics control system locates the corresponding row and column address in the frame buffer and sets its intensity or color value.
Lines
A line is a one-dimensional geometric object defined by two endpoints: (X1, Y1) and (X2, Y2). The ideal mathematical equation of a straight line is:
y = mx + c
Where:
- m is the slope of the line: m = (Y2 - Y1) / (X2 - X1) = ΔY / ΔX
- c is the Y-intercept: c = Y1 - m · X1
Because screen pixels are arranged in an integer grid, a line-drawing algorithm must figure out which discrete pixels best approximate the true path of the ideal mathematical line.
DDA Line Drawing Algorithm
The DDA (Digital Differential Analyzer) Algorithm is an incremental conversion method. It calculates pixel positions by utilizing the step variations ΔX or ΔY from the previous pixel coordinate.
The Logic
Depending on the slope m of the line, we increment one coordinate by 1 unit and calculate the corresponding fractional increment for the other coordinate:
- Case 1: If |m| < 1 (Line is closer to horizontal)
We step through X by 1 unit increments (ΔX = 1), and calculate the next Y value as: Yk+1 = Yk + m - Case 2: If |m| > 1 (Line is closer to vertical)
We step through Y by 1 unit increments (ΔY = 1), and calculate the next X value as: Xk+1 = Xk + 1/m
DDA Algorithm Steps
- Input the two endpoints (X1, Y1) and (X2, Y2).
- Calculate ΔX = X2 - X1 and ΔY = Y2 - Y1.
- Determine the number of steps required: Steps = max(|ΔX|, |ΔY|).
- Calculate the incremental step sizes for both coordinates: Xinc = ΔX / Steps and Yinc = ΔY / Steps.
- Put the initial pixel on the screen by rounding to the nearest integer: draw_pixel(round(X), round(Y)).
- Loop for the total number of steps:
- X = X + Xinc
- Y = Y + Yinc
- draw_pixel(round(X), round(Y))
Limitation of DDA: It requires floating-point additions and a time-consuming round() operation at every single step, making it computationally expensive for real-time graphics hardware.
Bresenham’s Line Algorithm
Bresenham’s Line Algorithm is a highly efficient scan-conversion method. It avoids floating-point math completely, relying solely on integer addition, subtraction, and bit-shifting, which can be executed directly at the hardware layer.
The Logic
For a line with a slope 0 < m < 1, the algorithm increments X by 1 at each step. To determine the next Y coordinate, it tests whether the ideal line is closer to the current pixel level (Yk) or the upper level (Yk + 1). This choice is governed by a Decision Parameter (Pk).
Bresenham’s Algorithm Steps (for 0 < m < 1)
- Input the two endpoints (X1, Y1) and (X2, Y2), and ensure X1 < X2.
- Plot the first point (X1, Y1).
- Calculate constants: ΔX = X2 - X1, ΔY = Y2 - Y1, 2ΔY, and 2ΔY - 2ΔX.
- Calculate the initial decision parameter P0 = 2ΔY - ΔX.
- At each Xk along the line, starting at k = 0, perform the following test:
- If Pk < 0: The next pixel to plot is (Xk + 1, Yk) and Pk+1 = Pk + 2ΔY.
- If Pk ≥ 0: The next pixel to plot is (Xk + 1, Yk + 1) and Pk+1 = Pk + 2ΔY - 2ΔX.
Circle Drawing Algorithms
A circle is defined by the geometric equation: (X - Xc)2 + (Y - Yc)2 = R2, where (Xc, Yc) is the center position and R is the radius. To make drawing efficient, algorithms calculate pixel positions for a circle centered at the origin (0,0) and then shift the coordinates by adding Xc and Yc.
The Polynomial Method
The Polynomial Method uses the standard algebraic equation of a circle to solve for Y at each integer step of X.
The Logic
For a circle centered at (0,0), we solve for Y: Y = ±√(R2 - X2). We step X from 0 to R in unit increments and calculate the corresponding integer value for Y.
Limitations
- Heavy Calculations: Requires a square root and squaring operations at every single step, making it extremely slow.
- Uneven Pixel Spacing: As X approaches R, the slope of the circle approaches infinity. This leaves wide, un-plotted vertical gaps between pixels along the sides of the circle, creating an uneven look.
Bresenham’s Circle Algorithm (Midpoint Method)
Bresenham’s circle algorithm avoids floating-point operations and square roots entirely by working incrementally. It leverages eight-way symmetry. If we calculate the coordinates for just one octant (a 45° slice from X = 0 to X = Y), we can instantly plot the pixels for the remaining seven octants by swapping signs and positions.
Mathematical Derivation
Assuming we just plotted pixel (Xk, Yk) in the first octant, our next pixel choice is between:
- East pixel: E = (Xk + 1, Yk)
- South-East pixel: SE = (Xk + 1, Yk - 1)
We evaluate the midpoint between these two choices: M = (Xk + 1, Yk - 1/2). We plug this midpoint into the circle function f(X,Y) = X2 + Y2 - R2 to get our Decision Parameter (Pk):
- If Pk < 0: The midpoint lies inside the true circle boundary, meaning the East pixel (E) is closer. Select Yk+1 = Yk and Pk+1 = Pk + 2Xk+1 + 1.
- If Pk ≥ 0: The midpoint lies outside or on the boundary, meaning the South-East pixel (SE) is closer. Select Yk+1 = Yk - 1 and Pk+1 = Pk + 2Xk+1 + 1 - 2Yk+1.
Initial Decision Parameter
Starting at the top of the circle (0, R), the initial decision parameter evaluates directly to: P0 = 5/4 - R (or 1 - R for integer simplification).
Curves in Computer Graphics
When drawing smooth, complex, non-linear shapes that cannot be described by simple circles or lines, we turn to spline curves.
Parametric Representation of Cubic Curves
Using an explicit equation like Y = f(X) falls short for complex curves because it cannot easily handle vertical lines or self-overlapping loops. Instead, computer graphics relies on parametric equations, where both X and Y are computed independently as functions of a parameter u that ranges from 0 to 1.
A Cubic Polynomial Curve uses a third-degree polynomial expression because it provides enough flexibility to create smooth, winding shapes while keeping calculation costs reasonable. In matrix notation, a 3D point vector along the curve is expressed as: P(u) = U · C, where U = [u3 u2 u 1] and C is the algebraic coefficient matrix.
Bézier Curves
Developed by Pierre Bézier for automotive design, Bézier Curves are foundational tools in modern digital vector graphics. Instead of forcing a curve to pass through points directly, it is shaped by Control Points (P0, P1, ..., Pn). The curve acts like a flexible string pulled toward these points, forming a convex hull.
Mathematical Formulation
The path of a Bézier curve of degree n is defined as the weighted sum of its control points multiplied by Bernstein Polynomials, which act as blending functions.
Cubic Bézier Curves (n = 3)
The most widely used variation handles four control points:
- P0: The exact starting vertex point of the curve (u=0).
- P1, P2: Intermediate steering points that guide the directional tangent vectors.
- P3: The exact termination vertex point of the curve (u=1).
Key Properties of Bézier Curves
- Convex Hull Property: The entire curve is guaranteed to lie completely inside the polygon formed by its control points, making collision detection efficient.
- Affine Invariance: If you scale, rotate, or translate the control points, the resulting curve transforms uniformly without needing recalculation.
- Variation Diminishing Property: The curve is smooth and well-behaved; it will never oscillate more times than the straight lines connecting its control polygon points.