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: 6

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