A Swift implementation of the Earcut polygon triangulation algorithm. This library efficiently triangulates 2D polygons with holes.
- Features
- Requirements
- Installation
- Basic Usage
- Advanced Usage
- Performance Considerations
- API Reference
- Contributing
- Algorithm Details
- License
- Pure Swift implementation
- Cross-platform support (macOS, iOS, tvOS, iPadOS, visionOS, Linux, Windows, Android)
- Efficient with conditional importing (FoundationEssentials when available)
- Fast and robust polygon triangulation
- Support for polygons with holes
- Comprehensive test suite using Swift Testing
- CI integration with GitHub Actions
- Swift 6.0 or later
- macOS 10.15+, iOS 13.0+, tvOS 13.0+, watchOS 6.0+, or Linux with Swift support
- Xcode 15.0+ (for development on Apple platforms)
Add the dependency to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/apache-edge/earcut.git", from: "0.0.1")
]
Then add the dependency to your target:
targets: [
.target(
name: "YourTarget",
dependencies: [
.product(name: "Earcut", package: "earcut")
]
)
]
- Clone the repository:
git clone https://github.com/apache-edge/earcut.git
- Drag the
Sources/Earcut
folder into your Xcode project.
import Earcut
// Create a flat array of coordinates [x0, y0, x1, y1, ...]
let squareData: [Double] = [0, 0, 1, 0, 1, 1, 0, 1]
// Triangulate the polygon
let indices = Earcut.triangulate(squareData)
// Process the resulting triangle indices...
// Each group of 3 indices forms a triangle
import Earcut
// Create a polygon with an outer ring
let square = Polygon(
outerRing: [
Point(x: 0, y: 0),
Point(x: 1, y: 0),
Point(x: 1, y: 1),
Point(x: 0, y: 1)
]
)
// Triangulate the polygon
let indices = square.triangulate()
// Get the triangles as an array of point arrays
let triangles = square.triangles()
// Process the resulting triangles...
import Earcut
// Create a polygon with a hole
let polygonWithHole = Polygon(
outerRing: [
Point(x: 0, y: 0),
Point(x: 10, y: 0),
Point(x: 10, y: 10),
Point(x: 0, y: 10)
],
holes: [
[
Point(x: 2, y: 2),
Point(x: 8, y: 2),
Point(x: 8, y: 8),
Point(x: 2, y: 8)
]
]
)
// Triangulate the polygon
let triangles = polygonWithHole.triangles()
// Calculate the area of the polygon
let area = polygonWithHole.area()
The Earcut algorithm has a time complexity of O(n log n) where n is the number of vertices.
The space complexity is O(n) for storing the vertices and triangulation results.
- For best performance, avoid unnecessary polygon vertices
- Pre-allocate arrays when processing multiple polygons
- Consider simplifying complex polygons for real-time applications
Point
: A 2D point with x and y coordinatesPolygon
: A polygon with an outer ring and optional holesEarcut
: The main triangulation algorithm implementation
Earcut.triangulate(_:holeIndices:dimensions:)
: Triangulates a flat array of coordinatesPolygon.triangulate()
: Triangulates a polygon objectPolygon.triangles()
: Returns the triangulation as an array of point arraysPolygon.area()
: Calculates the area of the polygon
Contributions are welcome! Please feel free to submit a Pull Request.
Earcut is an efficient algorithm for triangulating polygons with holes. It works by:
- Processing the polygon vertices in a specific order
- Creating a linked list of vertices
- Connecting holes to the outer polygon
- Triangulating the resulting shape using ear clipping
This Swift implementation is based on the JavaScript reference implementation by Mapbox.
The algorithm has a time complexity of O(n log n) and a space complexity of O(n).
This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.
The Apache License 2.0 is a permissive open source license that allows you to:
- Use the code for commercial purposes
- Modify the code and create derivative works
- Distribute the original or modified code
- Use the code privately
The license requires you to:
- Include the license and copyright notice with any distribution
- State changes made to the code if distributed
Based on the original Earcut JavaScript implementation by Mapbox.