contourmap

package module
v0.0.0-...-6b67914 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2021 License: MIT Imports: 4 Imported by: 0

README

contourmap

Compute contour lines (isolines) for any 2D data in Go.

Installation

go get -u github.com/fogleman/contourmap

Documentation

https://godoc.org/github.com/fogleman/contourmap

Example Usage

Creating a 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)
Finding Contour Lines

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)
    }
}
Closing Contours at the Grid Perimeter

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

Examples

Some examples are included to help you get started.

$ cd go/src/github.com/fogleman/contourmap/examples
$ go run iceland.go iceland.jpg

Iceland Example

$ go run examples/function.go

Function Example

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Contour

type Contour []Point

Contour is a list of Points which define an isoline. Contours may be open or closed. Closed contours have c[0] == c[len(c)-1].

type ContourMap

type ContourMap struct {
	W   int     // width of the contour map in pixels
	H   int     // height of the contour map in pixels
	Min float64 // minimum value contained in this contour map
	Max float64 // maximum value contained in this contour map
	// contains filtered or unexported fields
}

func FromFloat64s

func FromFloat64s(w, h int, grid []float64) *ContourMap

FromFloat64s returns a new ContourMap for the provided 2D grid of values. len(grid) must equal w * h.

func FromFunction

func FromFunction(w, h int, f Function) *ContourMap

FromFloat64s returns a new ContourMap for the provided function. The function will be called for all points x = [0, w) and y = [0, h) to determine the Z value at each point.

func FromImage

func FromImage(im image.Image) *ContourMap

FromImage returns a new ContourMap for the provided image. The image is converted to 16-bit grayscale and will have Z values mapped from [0, 65535] to [0, 1].

func (*ContourMap) Closed

func (m *ContourMap) Closed() *ContourMap

Closed returns a new ContourMap that will ensure all Contours are closed paths by following the border when they would normally stop at the edge of the grid.

func (*ContourMap) Contours

func (m *ContourMap) Contours(z float64) []Contour

Contours returns a list of contours that represent isolines at the specified Z value.

func (*ContourMap) HistogramZs

func (m *ContourMap) HistogramZs(numLevels int) []float64

type Function

type Function func(x, y int) float64

Function returns a height Z for the specified X, Y point in a 2D grid.

type Point

type Point struct {
	X, Y float64
}

Point represents a 2D Cartesian point.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL