From 9c76c6cba67a17890b86f253d6c0335433c70ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Sa=CC=88nger?= Date: Wed, 29 Oct 2014 00:31:28 +0100 Subject: [PATCH] Infinit recursion fix --- Quadtree/QuadtreeAbstract.php | 9 +++++++++ tests/QuadtreeOverlaping/BlackHoleQuadtree.php | 10 ++++++++++ .../DummyCollistionDetector.php | 15 +++++++++++++++ tests/QuadtreeOverlaping/Overlapping.phpt | 18 ++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 tests/QuadtreeOverlaping/BlackHoleQuadtree.php create mode 100644 tests/QuadtreeOverlaping/DummyCollistionDetector.php create mode 100644 tests/QuadtreeOverlaping/Overlapping.phpt diff --git a/Quadtree/QuadtreeAbstract.php b/Quadtree/QuadtreeAbstract.php index 69b3b80..4eac984 100644 --- a/Quadtree/QuadtreeAbstract.php +++ b/Quadtree/QuadtreeAbstract.php @@ -31,6 +31,9 @@ abstract class QuadtreeAbstract /** @var Quadtree */ private $se; + + /** @var boolean */ + private $overlappingInserted = FALSE; /** * @param \Quadtree\ICollisionDetector $detector @@ -89,6 +92,12 @@ public function collideWithItems(Insertable $item) */ private function insertItem(Insertable $item) { + if ($this->overlappingInserted || $item->getBounds()->contains($this->bounds)) { + $this->items[] = $item; + $this->overlappingInserted = true; + return TRUE; + } + if ($this->nw === NULL && count($this->items) < $this->capacity) { $this->items[] = $item; return TRUE; diff --git a/tests/QuadtreeOverlaping/BlackHoleQuadtree.php b/tests/QuadtreeOverlaping/BlackHoleQuadtree.php new file mode 100644 index 0000000..b644054 --- /dev/null +++ b/tests/QuadtreeOverlaping/BlackHoleQuadtree.php @@ -0,0 +1,10 @@ +intersects($item->getBounds()); + } +} + \ No newline at end of file diff --git a/tests/QuadtreeOverlaping/Overlapping.phpt b/tests/QuadtreeOverlaping/Overlapping.phpt new file mode 100644 index 0000000..fbc0cf6 --- /dev/null +++ b/tests/QuadtreeOverlaping/Overlapping.phpt @@ -0,0 +1,18 @@ +insert(new Bounds(100, 100))); +Assert::true($qt->insert(new Bounds(100, 100))); +Assert::true($qt->insert(new Bounds(100, 100))); + +Assert::true($qt->insert(new Bounds(10, 10))); +Assert::true($qt->insert(new Bounds(10, 10))); +Assert::true($qt->insert(new Bounds(10, 10)));