Unit 2 Computer Graphics
Unit 2 Computer Graphics
Transformations
This document explores fundamental raster graphics algorithms and transformations essential in computer graphics and
image processing. We'll cover line drawing algorithms like DDA and Bresenham's, circle and ellipse drawing techniques,
various filling algorithms, and both 2D and 3D transformations. These concepts form the foundation of digital image
creation and manipulation in modern computer graphics systems.
Line Drawing Algorithms: Digital Differential
Analyzer (DDA)
The Digital Differential Analyzer (DDA) algorithm is a simple yet effective method for rasterizing lines on digital displays.
It works by calculating intermediate points along the line path using floating-point arithmetic.
The DDA algorithm follows these steps:
1. Calculate the line slope and determine the longer axis (x or y).
2. Increment along the longer axis by 1 unit each step.
3. Calculate the corresponding position on the shorter axis using the slope.
4. Round the calculated values to the nearest integer for pixel placement.
While easy to implement, the DDA algorithm's reliance on floating-point calculations can lead to accumulated rounding
errors and slower performance compared to integer-based algorithms.
Line Drawing Algorithms: Bresenham's
Algorithm
Bresenham's line drawing algorithm, developed by Jack Bresenham in 1962, is a more efficient alternative to the DDA
algorithm. It uses only integer arithmetic, making it faster and more precise for rasterizing lines.
The algorithm works by making decisions about which pixels to plot based on the accumulated error term. For lines with
a slope between 0 and 1, it follows these steps:
Bresenham's algorithm can be adapted for lines with other slopes and even for drawing circles and ellipses.
Circle Drawing Algorithms
Circle drawing algorithms are crucial for creating circular shapes in raster graphics. The most common approach is the
midpoint circle algorithm, also known as Bresenham's circle algorithm. This algorithm leverages the circle's symmetry to
efficiently plot points.
This method only calculates points for one octant of the circle, significantly reducing computation time while maintaining
accuracy.
Ellipse Drawing Algorithms
Ellipse drawing algorithms extend the concepts used in circle drawing to handle the non-uniform radii of ellipses. The
midpoint ellipse algorithm is a common approach, similar to the midpoint circle algorithm but adapted for elliptical
shapes.
Key steps in the midpoint ellipse algorithm include:
1. Calculating initial decision parameters for both regions of the ellipse (more vertical and more horizontal sections).
2. Plotting points starting from (0, b) and (a, 0), where a and b are the semi-major and semi-minor axes.
3. Using decision parameters to choose the next pixel to plot.
4. Reflecting points in all four quadrants of the ellipse.
Optimizations can be made to reduce floating-point calculations and improve performance, especially for real-time
graphics applications.
Polygon Filling: Scan-Line Algorithm
1 Edge Table Creation
Create a table of all polygon edges, sorted by minimum y-coordinate.
4 Edge Updates
Add new edges and remove completed edges as scanning progresses.
The scan-line polygon filling algorithm efficiently fills complex polygons by processing the shape one horizontal line at a
time. This method is particularly effective for handling concave polygons and those with holes.
Inside-Outside Tests and Boundary Fill
Inside-outside tests are crucial for determining whether a point lies within a polygon. Common methods include:
Ray casting: Count intersections of a ray from the point with polygon edges.
Winding number: Calculate the total angle formed by vectors from the point to polygon vertices.
Barycentric coordinates: Used for triangles, based on area ratios.
Boundary fill algorithms use these tests to fill shapes from a starting interior point. The algorithm recursively fills
neighboring pixels until it reaches the boundary color. This method is effective for simple shapes but can be inefficient
and prone to stack overflow for large areas.
Optimizations like span filling and non-recursive implementations can improve performance and reliability for boundary
fill operations.
Flood Fill and Area Fill Algorithms
Flood fill algorithms are used to fill connected regions of similar color in raster graphics. The basic flood fill algorithm
starts from a seed point and recursively fills adjacent pixels that meet a specified criterion (usually having the same color
as the starting point).
Two main approaches to flood fill are:
1. 4-connected: Fills only horizontally and vertically adjacent pixels.
2. 8-connected: Fills all adjacent pixels, including diagonals.
Area fill algorithms extend this concept to fill regions based on more complex criteria, such as texture patterns or color
ranges. These algorithms often employ techniques like pattern matching or color similarity thresholds to determine
which pixels to fill.
2D Transformations
Translation Rotation Scaling Reflection
Moves objects by adding Rotates objects around a Changes object size by Reflection creates the
constant values to x and point using trigonometric multiplying coordinates illusion of a mirrored
y coordinates. functions. by scaling factors. surface using techniques
like ray tracing. It's useful
for realistic materials like
glass, metal, or water.
2D transformations are fundamental operations in computer graphics that modify the position, orientation, and size of
objects in a two-dimensional space. Other important 2D transformations include reflection (flipping objects across an
axis) and shearing (slanting objects along one or both axes).
These transformations can be represented as matrix operations, allowing for efficient computation and combination of
multiple transformations. Homogeneous coordinates are often used to represent 2D points as 3D vectors, enabling all
transformations (including translation) to be expressed as matrix multiplications.
3D Transformations and Homogeneous
Coordinates
3D transformations extend the concepts of 2D transformations to three-dimensional space. These include: