Skip to content

Commit 9c76c6c

Browse files
committed
Infinit recursion fix
1 parent 7af7ae0 commit 9c76c6c

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

Quadtree/QuadtreeAbstract.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ abstract class QuadtreeAbstract
3131

3232
/** @var Quadtree */
3333
private $se;
34+
35+
/** @var boolean */
36+
private $overlappingInserted = FALSE;
3437

3538
/**
3639
* @param \Quadtree\ICollisionDetector $detector
@@ -89,6 +92,12 @@ public function collideWithItems(Insertable $item)
8992
*/
9093
private function insertItem(Insertable $item)
9194
{
95+
if ($this->overlappingInserted || $item->getBounds()->contains($this->bounds)) {
96+
$this->items[] = $item;
97+
$this->overlappingInserted = true;
98+
return TRUE;
99+
}
100+
92101
if ($this->nw === NULL && count($this->items) < $this->capacity) {
93102
$this->items[] = $item;
94103
return TRUE;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
class BlackHoleQuadtree extends \Quadtree\QuadtreeAbstract
4+
{
5+
public function __construct(\Quadtree\Geometry\Bounds $bounds, $leafCapacity)
6+
{
7+
$detector = new \DummyCollisionDetector();
8+
parent::__construct($detector, $bounds, $leafCapacity);
9+
}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
class DummyCollisionDetector implements \Quadtree\ICollisionDetector
4+
{
5+
public function collide(array $insertedItems, \Quadtree\Insertable $item)
6+
{
7+
return FALSE;
8+
}
9+
10+
public function intersects(\Quadtree\Geometry\Bounds $bounds, \Quadtree\Insertable $item)
11+
{
12+
return $bounds->intersects($item->getBounds());
13+
}
14+
}
15+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
require __DIR__ . '/../bootstrap.php';
4+
require './DummyCollistionDetector.php';
5+
require './BlackHoleQuadtree.php';
6+
7+
use Tester\Assert;
8+
use Quadtree\Quadtree;
9+
use Quadtree\Geometry\Bounds;
10+
11+
$qt = new BlackHoleQuadtree(new Bounds(100, 100), 2);
12+
Assert::true($qt->insert(new Bounds(100, 100)));
13+
Assert::true($qt->insert(new Bounds(100, 100)));
14+
Assert::true($qt->insert(new Bounds(100, 100)));
15+
16+
Assert::true($qt->insert(new Bounds(10, 10)));
17+
Assert::true($qt->insert(new Bounds(10, 10)));
18+
Assert::true($qt->insert(new Bounds(10, 10)));

0 commit comments

Comments
 (0)