Rasterizing a 2D polygon
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Rasterizing a 2D polygon is a crucial process in computer graphics and digital imaging, where vectors, defined mathematically, are converted into raster images or pixels for rendering on digital displays or storage in bitmap formats. This article explores the intricacies of the rasterization process, with a detailed technical explanation and relevant examples.
Understanding Rasterization
Rasterization is the process of determining which pixels will be covered by the polygon and assigning colors or shades to these pixels, respecting the geometric and interpolative details specified in the vector form. The process involves several steps and algorithms that help in efficiently converting the vector shape into a raster image.
Basic Steps of Rasterization
- Vertex Input and Transformation:
- Begin with the 2D vertex data that defines the polygon.
- Transform the vertices from world coordinates to screen space using transformation matrices.
- Edge Setup:
- Define the edges of the polygon, which help in determining which pixels lie inside or outside.
- Store edge information such as start and end points, slope, etc.
- Scan-Line Conversion:
- For each horizontal line (scan-line) on the raster grid, determine which sections are inside the polygon.
- Utilize edge tables and active edge lists to efficiently track where each span starts and ends along the current scan-line.
- Pixel Coloring:
- Assign colors to the pixels within the spans, potentially using methods such as area averaging or anti-aliasing to improve visual quality.
- Handle textures if textured rendering is required.
Algorithms in Rasterization
- Scan-Line Algorithm:
- A traditional approach that iterates over scan-lines and examines edge crossings to fill pixels.
- Efficient and straightforward for simple polygons but can be complex with intersecting edges or concave shapes.
- Edge Walking:
- Starts from an initial edge and “walks” through the structure following connections to fill the polygon.
- Requires sorting edges and meticulous management of intersecting points.
- Z-Buffering:
- Maintains depth information to ensure the correct rendering order when polygons overlap, critical in 3D extensions of polygon rasterization.
Example: Rasterizing a Simple Triangle
Consider a triangle with vertices at (2, 3), (5, 11), and (7, 6). Here's a simplified explanation of rasterization:
- Vertex Processing:
- The vertices are converted to screen space and defined in an edge table.
- Edge Detection:
- Identify edges:
(2, 3)to(5, 11),(5, 11)to(7, 6), and(7, 6)to(2, 3). - Calculate slopes and intercepts to track pixel transitions.
- Scan-Line Algorithm:
- For each scan-line from y=3 to y=11, compute intersections with the polygon's edges.
- Fill pixels between the intersections along the scan-line.
- Color Assignment:
- Apply colors, ensuring coherent shading or texturing across the triangle.
Enhancements through Anti-Aliasing
One of the challenges in rasterization is aliasing, where pixelized edges appear jagged. Anti-aliasing techniques, such as supersampling or multisampling, blend colors at boundaries, smoothing the transition and improving visual fidelity.
Table Summary of Key Concepts in Rasterization
| Key Concept | Description |
| Vertex Transformation | Converts world coordinates to screen space. |
| Edge Setup | Determines intersections on scan-lines to delineate polygon boundaries. |
| Scan-Line Algorithm | Uses edge tables and active edge lists to fill spans within scan-lines. |
| Anti-Aliasing | Reduces jagged edges using blending techniques for smoother image quality. |
| Z-Buffering | Depth management to handle overlapped polygons in a 3D space. |
Conclusion
Rasterizing a 2D polygon is a fundamental aspect of rendering in digital graphics, bridging the gap between vector-based models and pixel-based displays. By utilizing a range of algorithms and techniques, from scan-line conversion to anti-aliasing, rasterization ensures accurate and visually appealing representations of vector geometries on digital screens. These processes are executed millions of times per second in modern graphic systems, underpinning realistic and immersive digital experiences.
Related reading
- Ray-box Intersection Theory
- Ray-triangle intersection
- Ray - Octree intersection algorithms
- Real world applications of Binary heaps and Fibonacci Heaps
- Real world typo statistics?
- Rearrange a list of points to reach the shortest distance between them
- Real world examples to decide which sorting algorithm works best
- Real world implementations of classical algorithms

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.