Documentation
¶
Overview ¶
Package hilbert is for mapping values to and from space-filling curves, such as Hilbert and Peano curves.
Example ¶
package main
import (
"fmt"
"github.com/bramp/hilbert"
)
func main() {
// Create a Hilbert curve for mapping to and from a 16 by 16 space.
s, _ := hilbert.NewHilbert(16)
// Create a Peano curve for mapping to and from a 27 by 27 space.
//s, _ := hilbert.NewPeano(27)
// Create a Morton curve for mapping to and from a 16 by 16 space.
//s, _ := hilbert.NewMorton(16)
// Now map one dimension numbers in the range [0, N*N-1], to an x,y
// coordinate on the curve where both x and y are in the range [0, N-1].
x, y, _ := s.Map(96)
// Also map back from (x,y) to t.
t, _ := s.MapInverse(x, y)
fmt.Printf("x = %d, y = %d, t = %d\n", x, y, t)
}
Output: x = 4, y = 12, t = 96
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotPositive = errors.New("n must be greater than zero") ErrNotPowerOfTwo = errors.New("n must be a power of two") ErrNotPowerOfThree = errors.New("n must be a power of three") ErrOutOfRange = errors.New("value is out of range") )
Errors returned when validating input.
Functions ¶
This section is empty.
Types ¶
type Gosper ¶
Gosper represents a 2D Gosper curve of order N. The Gosper curve is a space-filling curve on a hexagonal grid. Implements SpaceFilling2D interface.
func NewGosper ¶
NewGosper returns a Gosper space which maps integers to and from the curve. order is the recursive depth. Total hexagons will be 7^order.
func (*Gosper) GetDimensions ¶
GetDimensions returns the width and height of the bounding box for the hexagonal grid.
func (*Gosper) GetGridType ¶
GetGridType returns the geometry of the grid.
type Hilbert ¶
type Hilbert struct {
N int
}
Hilbert represents a 2D Hilbert space of order N for mapping to and from. Implements SpaceFilling interface.
func NewHilbert ¶
NewHilbert returns a Hilbert space which maps integers to and from the curve. n must be a power of two.
func (*Hilbert) GetDimensions ¶
GetDimensions returns the width and height of the 2D space.
func (*Hilbert) GetGridType ¶
GetGridType returns the geometry of the grid.
type Moore ¶
type Moore struct {
Hilbert
}
Moore represents a 2D Moore curve of order N for mapping to and from. The Moore curve is a closed-loop variation of the Hilbert curve. Implements SpaceFilling interface.
func NewMoore ¶
NewMoore returns a Moore space which maps integers to and from the curve. n must be a power of two.
func (*Moore) GetGridType ¶
GetGridType returns the geometry of the grid.
type Morton ¶
type Morton struct {
N int
}
Morton represents a 2D Morton (Z-order) curve of order N for mapping to and from. Implements SpaceFilling interface.
func NewMorton ¶
NewMorton returns a Morton space which maps integers to and from the curve. n must be a power of two.
func (*Morton) GetDimensions ¶
GetDimensions returns the width and height of the 2D space.
func (*Morton) GetGridType ¶
GetGridType returns the geometry of the grid.
type Peano ¶
type Peano struct {
N int // Always a power of three, and is the width/height of the space.
}
Peano represents a 2D Peano curve of order N for mapping to and from. Implements SpaceFilling interface.
func NewPeano ¶
NewPeano returns a new Peano space filling curve which maps integers to and from the curve. n must be a power of three.
func (*Peano) GetDimensions ¶
GetDimensions returns the width and height of the 2D space.
func (*Peano) GetGridType ¶
GetGridType returns the geometry of the grid.
type Sierpinski ¶
type Sierpinski struct {
N int
}
Sierpinski represents a 2D Sierpinski space of order N for mapping to and from. Implements SpaceFilling interface.
func NewSierpinski ¶
func NewSierpinski(n int) (*Sierpinski, error)
NewSierpinski returns a Sierpinski space which maps integers to and from the curve. n must be a power of two.
func (*Sierpinski) GetCount ¶
func (s *Sierpinski) GetCount() int
GetCount returns the total number of points on the curve.
func (*Sierpinski) GetDimensions ¶
func (s *Sierpinski) GetDimensions() (int, int)
GetDimensions returns the width and height of the 2D space.
func (*Sierpinski) GetGridType ¶
func (s *Sierpinski) GetGridType() GridType
GetGridType returns the geometry of the grid.
func (*Sierpinski) Map ¶
func (s *Sierpinski) Map(t int) (x, y int, err error)
Map transforms a one dimension value, t, in the range [0, n^2-1] to coordinates on the Sierpinski curve in a triangular grid, where y is the row index [0, n-1] and x is the triangle index in that row [0, 2y].
func (*Sierpinski) MapInverse ¶
func (s *Sierpinski) MapInverse(x, y int) (t int, err error)
MapInverse transform coordinates on Sierpinski curve from (x,y) to t.
type Snake ¶
type Snake struct {
N int
}
Snake represents a 2D Snake (Boustrophedon) scan of order N. Implements SpaceFilling2D interface.
func (*Snake) GetDimensions ¶
GetDimensions returns the width and height of the 2D space.
func (*Snake) GetGridType ¶
GetGridType returns the geometry of the grid.
type SpaceFilling ¶
type SpaceFilling = SpaceFilling2D
SpaceFilling is an alias for SpaceFilling2D for backward compatibility. Deprecated: Use SpaceFilling2D instead.
type SpaceFilling2D ¶
type SpaceFilling2D interface {
// Map transforms a one dimension value, t, in the range [0, n^2-1] to coordinates on the
// curve in the two-dimension space.
Map(t int) (x, y int, err error)
// MapInverse transform coordinates on the curve from (x,y) to t.
MapInverse(x, y int) (t int, err error)
// GetDimensions returns the width and height of the 2D space.
GetDimensions() (x, y int)
// GetCount returns the total number of points on the curve.
GetCount() int
// GetGridType returns the geometry of the grid.
GetGridType() GridType
}
SpaceFilling2D represents a 2D space-filling curve that can map points from one dimensions to two.













