quadtree

package module
v0.0.0-...-d12870f Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2019 License: MIT Imports: 0 Imported by: 0

README ΒΆ

πŸ€ Quadtree (Go)

godoc reference

A quadtree implementation in Go, featuring insertion and retrieval of bounding boxes and points.

What is it?

"A quadtree is a tree data structure in which each internal node has exactly four children. Quadtrees are most often used to partition a two-dimensional space by recursively subdividing it into four quadrants or regions. The regions may be square or rectangular, or may have arbitrary shapes. This data structure was named a quadtree by Raphael Finkel and J.L. Bentley in 1974." - Wikipedia (2016)



Setup

Usage

    qt := &quadtree.Quadtree{
		Bounds: quadtree.Bounds{
			X:      0,
			Y:      0,
			Width:  100,
			Height: 100,
		},
		MaxObjects: 10,
		MaxLevels:  4,
		Level:      0,
		Objects:    make([]quadtree.Bounds, 0),
		Nodes:      make([]quadtree.Quadtree, 0),
	}

    // Insert some boxes
    qt.Insert(Bounds{
		X:      1,
		Y:      1,
		Width:  10,
		Height: 10,
	})
	qt.Insert(Bounds{
		X:      5,
		Y:      5,
		Width:  10,
		Height: 10,
	})
	qt.Insert(Bounds{
		X:      10,
		Y:      10,
		Width:  10,
		Height: 10,
	})
	qt.Insert(Bounds{
		X:      15,
		Y:      15,
		Width:  10,
		Height: 10,
	})

	//Get all the intersections
	intersections := qt.RetrieveIntersections(Bounds{
		X:      5,
		Y:      5,
		Width:  2.5,
		Height: 2.5,
	})

	// Clear the Quadtree
	qt.Clear()


Acknowledgements

This package is based on Timo Hausmann JavaScript Quadtree implementation. This is in turn based on this post.

License

MIT

Original JavaScript code: Copyright Β© 2012 Timo Hausmann

Go code: Copyright Β© 2016 James Milner

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type Bounds ΒΆ

type Bounds struct {
	X      float64
	Y      float64
	Width  float64
	Height float64
}

Bounds - A bounding box with a x,y origin and width and height

func (*Bounds) Intersects ΒΆ

func (b *Bounds) Intersects(a Bounds) bool

Intersects - Checks if a Bounds object intersects with another Bounds

func (*Bounds) IsPoint ΒΆ

func (b *Bounds) IsPoint() bool

IsPoint - Checks if a bounds object is a point or not (has no width or height)

type Quadtree ΒΆ

type Quadtree struct {
	Bounds     Bounds
	MaxObjects int // Maximum objects a node can hold before splitting into 4 subnodes
	MaxLevels  int // Total max levels inside root Quadtree
	Level      int // Depth level, required for subnodes
	Objects    []Bounds
	Nodes      []Quadtree
	Total      int
}

Quadtree - The quadtree data structure

func (*Quadtree) Clear ΒΆ

func (qt *Quadtree) Clear()

Clear - Clear the Quadtree

func (*Quadtree) Insert ΒΆ

func (qt *Quadtree) Insert(pRect Bounds)

Insert - Insert the object into the node. If the node exceeds the capacity, it will split and add all objects to their corresponding subnodes.

func (*Quadtree) Retrieve ΒΆ

func (qt *Quadtree) Retrieve(pRect Bounds) []Bounds

Retrieve - Return all objects that could collide with the given object

func (*Quadtree) RetrieveIntersections ΒΆ

func (qt *Quadtree) RetrieveIntersections(find Bounds) []Bounds

RetrieveIntersections - Bring back all the bounds in a Quadtree that intersect with a provided bounds

func (*Quadtree) RetrievePoints ΒΆ

func (qt *Quadtree) RetrievePoints(find Bounds) []Bounds

RetrievePoints - Return all points that collide

func (*Quadtree) TotalNodes ΒΆ

func (qt *Quadtree) TotalNodes() int

TotalNodes - Retrieve the total number of sub-Quadtrees in a Quadtree

Jump to

Keyboard shortcuts

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