Compute contour lines (isolines) for any 2D data in Go.
go get -u github.com/fogleman/contourmap
https://godoc.org/github.com/fogleman/contourmap
A new ContourMap
can be generated in many different ways, depending on what type of data you have.
Use FromFloat64s
if you have an array of numbers. The length of the array must equal width * height
. The two-dimensional data is stored in a flat array in row-major order.
m := contourmap.FromFloat64s(width, height, data)
Use FromImage
if you have an image.Image
, such as a grayscale heightmap.
m := contourmap.FromImage(im)
Use FromFunction
to specify an arbitrary function that will provide a Z for any given X, Y coordinate.
The function will be called for all points x = [0, w)
and y = [0, h)
to determine the Z value at each point in the grid.
var f func(x, y int) float64
...
m := contourmap.FromFunction(width, height, f)
Once your ContourMap
is created, you can use the Contours
function to find isolines at any given Z height. This function returns a list of contours where each contour is a list of X, Y points.
A Contour
may be open or closed. Closed contours have c[0] == c[len(c)-1]
.
contours := m.Contours(z)
for _, contour := range contours {
for _, point := range contour {
// do something with points...
fmt.Println(point.X, point.Y)
}
}
Contours may end at the edge of the grid data, forming open contours. If you want to force all contours to be closed by following the perimeter of the grid, you can use ContourMap.Closed
which will generate a new ContourMap that can be used for this purpose:
m = m.Closed() // now all contours will be closed paths
Some examples are included to help you get started.
$ cd go/src/github.com/fogleman/contourmap/examples
$ go run iceland.go iceland.jpg
$ go run examples/function.go