quadtree

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2019 License: MIT Imports: 1 Imported by: 2

README

QuadTree

Golang implementation of the quadtree algorithm. Includes removal, update and knearest search.

Godoc https://godoc.org/github.com/asim/quadtree

Usage Example

Create a quadtree fitting the world geographic bounds (from [-90,-180] to [90,180])

centerPoint := quadtree.NewPoint(0.0, 0.0, nil)
halfPoint := quadtree.NewPoint(90.0, 180.0, nil)
boundingBox := quadtree.NewAABB(centerPoint, halfPoint)

qtree := quadtree.New(boundingBox, 0, nil)

Insert a point into the tree

point := quadtree.NewPoint(52.5200, 13.4050, "Berlin")
if !qtree.Insert(point) {
  log.Fatal("Failed to insert the point")
}

Find the k-nearest points

center := quadtree.NewPoint(lat, lng, nil)
distance := 10000 /* Distance to the center point in meters */
bounds := quadtree.NewAABB(center, center.HalfPoint(distance))

maxPoints := 10
for _, point := range qtree.KNearest(bounds, maxPoints, nil) {
  log.Printf("Found point: %s\n", point.Data().(string))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Capacity = 8
	MaxDepth = 6
)

Functions

This section is empty.

Types

type AABB

type AABB struct {
	// contains filtered or unexported fields
}

func NewAABB

func NewAABB(center, half *Point) *AABB

NewAABB creates an axis aligned bounding box. It takes the center and half point.

func (*AABB) ContainsPoint

func (a *AABB) ContainsPoint(p *Point) bool

ContainsPoint checks whether the point provided resides within the axis aligned bounding box.

func (*AABB) Intersect

func (a *AABB) Intersect(b *AABB) bool

Intersect checks whether two axis aligned bounding boxes overlap.

type Point

type Point struct {
	// contains filtered or unexported fields
}

func NewPoint

func NewPoint(x, y float64, data interface{}) *Point

NewPoint generates a new *Point struct.

func (*Point) Coordinates

func (p *Point) Coordinates() (float64, float64)

Coordinates return the x and y coordinates of a point.

func (*Point) Data

func (p *Point) Data() interface{}

Data returns the data stored within a point.

func (*Point) HalfPoint

func (p *Point) HalfPoint(m float64) *Point

HalfPoint is a convenience function for generating the half point required to created an axis aligned bounding box. It takes an argument of metres as float64.

type QuadTree

type QuadTree struct {
	// contains filtered or unexported fields
}

func New

func New(boundary *AABB, depth int, parent *QuadTree) *QuadTree

New creates a new *QuadTree. It requires a boundary defining the center and half points, depth at which the QuadTree resides and parent node. Depth of 0 and parent as nil implies the root node.

func (*QuadTree) Insert

func (qt *QuadTree) Insert(p *Point) bool

Insert will attempt to insert the point into the QuadTree. It will recursively search until it finds the leaf node. If the leaf node is at capacity then it will try split the node. If the tree is at max depth then point will be stored in the leaf.

func (*QuadTree) KNearest

func (qt *QuadTree) KNearest(a *AABB, i int, fn filter) []*Point

func (*QuadTree) RInsert

func (qt *QuadTree) RInsert(p *Point) bool

RInsert is used in conjuction with Update to try reveser insert a point.

func (*QuadTree) Remove

func (qt *QuadTree) Remove(p *Point) bool

Remove attemps to remove a point from the QuadTree. It will recurse until the leaf node is found and then try to remove the point.

func (*QuadTree) Search

func (qt *QuadTree) Search(a *AABB) []*Point

Search will return all the points within the given axis aligned bounding box. It recursively searches downward through the tree.

func (*QuadTree) Update

func (qt *QuadTree) Update(p *Point, np *Point) bool

Update will update the location of a point within the tree. It is optimised to attempt reinsertion within the same node and recurse back up the tree until it finds a suitable node.

Jump to

Keyboard shortcuts

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