Go library for Boolean operations on 2D polygons.
The library is KNOWN TO HAVE BUGS!!! Unfortunately, currently I don't have resources to investigate them thoroughly enough and in timely fashion. In case somebody is interested in taking ownership of the library, I'm open to ceding it. That said, the issues totally haunt me and occasionally I stubbornly try to come back to them and pick the fight up again. In particular:
Library polyclip-go is a pure Go, MIT-licensed implementation of an [algorithm for Boolean operations on 2D polygons] fmartin (invented by F. Martínez, A.J. Rueda, F.R. Feito) -- that is, for calculation of polygon intersection, union, difference and xor.
The original paper describes the algorithm as performing in time O((n+k) log n), where n is number of all edges of all polygons in operation, and k is number of intersections of all polygon edges.
Simplest Go program using polyclip-go for calculating intersection of a square and triangle:
// example.go
package main
import (
"fmt"
"github.com/akavel/polyclip-go" // or: bitbucket.org/...
)
func main() {
subject := polyclip.Polygon{{{1, 1}, {1, 2}, {2, 2}, {2, 1}}} // small square
clipping := polyclip.Polygon{{{0, 0}, {0, 3}, {3, 0}}} // overlapping triangle
result := subject.Construct(polyclip.INTERSECTION, clipping)
// will print triangle: [[{1 1} {1 2} {2 1}]]
fmt.Println(result)
}
To compile and run the program above, execute the usual sequence of commands:
go get github.com/akavel/polyclip-go # or: bitbucket.org/...
go build example.go
./example # Windows: example.exe
For full package documentation, run locally godoc github.com/akavel/polyclip-go
, or visit online documentation for polyclip-go.