From e2751726e199f5b50d702a61672c037be96a2673 Mon Sep 17 00:00:00 2001 From: hanemile Date: Sat, 3 Nov 2018 17:49:55 +0100 Subject: the insideOf function is now called InsideOf and can be now called from everywhere! --- structs.go | 27 +++++++++++++++++++++------ structs_test.go | 3 ++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/structs.go b/structs.go index 2d7f6a9..0fcdc14 100644 --- a/structs.go +++ b/structs.go @@ -1,5 +1,7 @@ package quadtree +import "fmt" + // coordinate type storing a position type Coord struct { x float64 @@ -17,7 +19,20 @@ func NewCoord(x float64, y float64) Coord { return Coord{x, y} } -func (point Coord) insideOf(quadtreeCell *quadtree) bool { +func ExampleNewCoord() { + fmt.Println(NewCoord(1, 1)) + // Output: {1, 1} +} + +// NewBoundingBox returns a new bounding with the given struct elements +func NewBoundingBox(inCenter Coord, inHalfDimension float64) BoundingBox { + return BoundingBox{ + center: inCenter, + halfDimension: inHalfDimension, + } +} + +func (point Coord) InsideOf(quadtreeCell *quadtree) bool { // define the bounds var lowerXBound = quadtreeCell.boundary.center.x - quadtreeCell.boundary.halfDimension var upperXBound = quadtreeCell.boundary.center.x + quadtreeCell.boundary.halfDimension @@ -125,16 +140,16 @@ func (quadtree *quadtree) Insert(point Coord) { if quadtree.depth < 4 { // ... test if the given point is inside the one of the cells // and add Insert it to that specific cell - if point.insideOf(quadtree.northWest) { + if point.InsideOf(quadtree.northWest) { quadtree.northWest.Insert(point) } - if point.insideOf(quadtree.northEast) { + if point.InsideOf(quadtree.northEast) { quadtree.northEast.Insert(point) } - if point.insideOf(quadtree.southWest) { + if point.InsideOf(quadtree.southWest) { quadtree.southWest.Insert(point) } - if point.insideOf(quadtree.southEast) { + if point.InsideOf(quadtree.southEast) { quadtree.southEast.Insert(point) } } @@ -157,5 +172,5 @@ func (quadtree *quadtree) Print() { if quadtree.southWest != nil { quadtree.southWest.Print() } - print(quadtree.pointsSlice) + fmt.Println(quadtree) } diff --git a/structs_test.go b/structs_test.go index 685557b..108cbc3 100644 --- a/structs_test.go +++ b/structs_test.go @@ -48,6 +48,7 @@ func TestNewCoord(t *testing.T) { t.Log(newCoordinate) } +// TestBoundingBox creates a bounding box func TestBoundingBox(t *testing.T) { var newBoundingBox = BoundingBox{ center: Coord{ @@ -70,7 +71,7 @@ func TestInsideOf(t *testing.T) { halfDimension: 1, }, 0) - var isInsideOf = testCoordinate.insideOf(testQuadTree) + var isInsideOf = testCoordinate.InsideOf(testQuadTree) t.Log(isInsideOf) } -- cgit 1.4.1