Go-tesselator is a pure Go implementation of a polygon tesselation library for triangulating complex polygons. It can handle polygons with holes and self-intersecting polygons, making it suitable for graphics processing, GIS, CAD and other applications.
- Triangulation of complex polygons including those with holes
- Support for both convex and concave polygons
- Efficient sweep line algorithm implementation
- SVG visualization generation for debugging and demonstration
- No external dependencies, only uses Go standard library
- Comprehensive test coverage
go get github.com/flywave/go-tesselator
package main
import (
"fmt"
"github.com/flywave/go-tesselator"
)
func main() {
// Define a simple square polygon
vertices := []tesselator.Vertex{
{X: 0, Y: 0, Z: 0},
{X: 100, Y: 0, Z: 0},
{X: 100, Y: 100, Z: 0},
{X: 0, Y: 100, Z: 0},
}
contour := tesselator.Contour(vertices)
contours := []tesselator.Contour{contour}
// Perform triangulation
indices, vertices, err := tesselator.Tesselate(contours, tesselator.WindingRuleOdd)
if err != nil {
panic(err)
}
fmt.Printf("Vertices: %v\n", vertices)
fmt.Printf("Indices: %v\n", indices)
}
// Define outer contour (square)
outerVertices := []tesselator.Vertex{
{X: 0, Y: 0, Z: 0},
{X: 100, Y: 0, Z: 0},
{X: 100, Y: 100, Z: 0},
{X: 0, Y: 100, Z: 0},
}
outerContour := tesselator.Contour(outerVertices)
// Define inner hole (smaller square)
innerVertices := []tesselator.Vertex{
{X: 25, Y: 25, Z: 0},
{X: 75, Y: 25, Z: 0},
{X: 75, Y: 75, Z: 0},
{X: 25, Y: 75, Z: 0},
}
innerContour := tesselator.Contour(innerVertices)
contours := []tesselator.Contour{outerContour, innerContour}
// Perform triangulation
indices, vertices, err := tesselator.Tesselate(contours, tesselator.WindingRuleOdd)
// Generate SVG visualization of triangulation
err := tesselator.TessellateAndGenerateSVG("example", contours)
if err != nil {
panic(err)
}
Here are some examples of triangulation results generated by the library:
Tesselate(contours []Contour, windingRule WindingRule) ([]int, []Vertex, error)
- Main triangulation functionTessellateAndGenerateSVG(filename string, contours []Contour) error
- Triangulate and generate SVG visualizationGenerateSVG(filename string, contours []Contour, vertices []Vertex, elements []int) error
- Generate SVG from triangulation data
Vertex
- Represents a 3D point with X, Y, Z coordinatesContour
- A slice of vertices defining a polygon contourWindingRule
- Enumeration for different winding rules:WindingRuleOdd
WindingRuleNonzero
WindingRulePositive
WindingRuleNegative
WindingRuleAbsGeqTwo
GenerateRegularPolygon(sides int, cx, cy, radius float32) Contour
- Generate regular polygonGenerateStar(points int, cx, cy, outerRadius, innerRadius float32) Contour
- Generate star shapeGenerateRandomPolygon(points int, cx, cy, radius float32) Contour
- Generate random polygon
The library includes comprehensive tests to verify correctness:
go test -v ./...
See the test files for various usage examples:
simple_test.go
- Basic polygon triangulation examplessvg_test.go
- SVG generation examplestess_test.go
- Comprehensive triangulation tests
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.