/** * Return an array of all points within this QuadTree and its child nodes that fall * within the specified bounding box * * @param QuadTreeBoundingBox $boundary The bounding box that we want to search * @return QuadTreeXYPoint[] **/ public function search(QuadTreeBoundingBox $boundary) { $results = array(); if ($this->boundingBox->encompasses($boundary) || $this->boundingBox->intersects($boundary)) { // Test each point that falls within the current QuadTree node foreach ($this->points as $point) { // Test each point stored in this QuadTree node in turn, adding to the results array // if it falls within the bounding box if ($boundary->containsPoint($point)) { $results[] = $point; } } // If we have child QuadTree nodes.... if (isset($this->northWest)) { // ... search each child node in turn, merging with any existing results $results = array_merge($results, $this->northWest->search($boundary), $this->northEast->search($boundary), $this->southWest->search($boundary), $this->southEast->search($boundary)); } } return $results; }
/** * Identifies whether a specified bounding box completely encompasses this bounding box? * * @param QuadTreeBoundingBox $boundary The specified bounding box to test * @return boolean Does the specified bounding box completely encompasses * this bounding box? */ public function encompasses(QuadTreeBoundingBox $boundary) { return $boundary->startX() <= $this->startX() && $boundary->endX() >= $this->endX() && ($boundary->startY() <= $this->startY() && $boundary->endY() >= $this->endY()); }