1. m c
  2. polyclip-go

Overview

About

Library polyclip-go is a pure Go, MIT-licensed implementation of an algorithm for Boolean operations on 2D polygons (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.

Example

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

Note: the polyclip-go library is developed for a fairly modern (at the time of writing) version of Go compiler, known as Go 1 RC1. If you have an older "release" version of Go, you may have problems compiling, and you are advised to switch to a newer "weekly" version. On the other hand, if you do have a newer version and encounter problems, please try using the go fix tool to update the polyclip-go library. I'll also be grateful if you could contact me about that.

For full package documentation, run locally godoc github.com/akavel/polyclip-go, or visit online documentation for polyclip-go.

See also