Polygon infill
computational geometry
algorithm design
3D printing
computer graphics

Polygon infill algorithm

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Polygon infill algorithms are crucial in computational geometry, computer graphics, and various applications like computer-aided design (CAD) and 3D printing. They are used to determine the set of points that lie within a polygon and subsequently fill this space. There are multiple approaches to polygon infill, including scanline algorithms, flood fill algorithms, and even more sophisticated techniques like Voronoi-based infill.

Scanline Fill Algorithm

The scanline algorithm is one of the most popular approaches for polygon infill. The basic idea is to iterate over each horizontal line (scanline) that intersects with the polygon, identifying intersections with the polygon's edges, and subsequently filling the interior segments.

Process of Scanline Algorithm

  1. Edge Table Construction:
    • An edge table is created to store all the edges of the polygon sorted by the y-coordinate of the lower vertex.
  2. Active Edge Table (AET):
    • As the scanline progresses from the bottom to the top of the polygon, an active edge table is maintained, which stores currently intersecting edges.
  3. Intersection Calculation:
    • For each scanline, calculate intersections of this line with the edges in the AET.
    • Sort these intersection points to determine the pairs of points between which filling is required.
  4. Fill Between Intersections:
    • Draw horizontal lines between each pair of intersections.
  5. Update Tables:
    • Remove edges from the AET that are no longer intersected by the scanline.
    • Add new edges that the current scanline begins to intersect.

Example

Consider a simple polygon defined by vertices: (1,1), (5,1), (5,5), (1,5). The scanline algorithm would create horizontal lines from (1,1) to (5,1), from (1,5) to (5,5), and similarly across y=2, y=3, and y=4.

Flood Fill Algorithm

Flood fill is another technique often used in paint applications and graphic editors to fill polygons. It is a recursive or queue-based algorithm that spreads from a seed point and extends to neighboring points of similar color or predefined boundary conditions.

Steps:

  1. Select Seed Point:
    • Choose an initial point within the polygon.
  2. Check Neighboring Points:
    • Evaluate adjacent points to determine if they can be filled.
  3. Simultaneous Fill:
    • Continue filling until boundaries are reached, using either a stack (depth-first) or queue (breadth-first) mechanism for traversal.

Limitation:

Flood fill can be memory-intensive due to its recursive depth or stack usage, and may not be suitable for complex, large polygons.

Complex Infill Patterns

Modern applications like 3D printing need more than just solid fillings. Complex infill patterns such as honeycomb, grid, or Voronoi structures minimize material usage while maintaining strength.

Voronoi Infill

A sophisticated method, Voronoi-based infill, relies on dividing spaces into regions around a set of points (sites) such that each region contains all points closer to its site than any other site.

Honeycomb and Grid Infill

These patterns optimize material strength by using geometric tessellations to maintain rigidity with minimal material.

Key Points

Below is a table summarizing the key aspects of polygon infill algorithms:

AlgorithmTechniqueComplexityApplicationsLimitations
ScanlineEdge IntersectionModerateGraphics rendering, CADPoor performance on concave shapes
Flood FillColor/Region ExpansionHigh (recursive)Image editing, basic graphic tasksHigh memory usage
Voronoi InfillRegion PartitioningComplex3D Printing, Structural AnalysisComputationally intensive
Honeycomb/GridGeometric TessellationVariable3D Printing, material-efficient structuresLimited flexibility

Conclusion

Polygon infill algorithms are versatile and essential tools across multiple domains. By understanding the various types of infill algorithms, their applicable scenarios, advantages, and limitations, one can make informed choices for specific applications. As technology evolves, these algorithms may become more sophisticated and efficient, opening doors to new possibilities.


Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.