Skip to content

Commit 7af7ae0

Browse files
committed
contains()
1 parent 140e77b commit 7af7ae0

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Quadtree/Geometry/Bounds.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,16 @@ public function __toString()
169169
return '(' . $values . ')';
170170
}
171171

172+
/**
173+
* Tests whether this rectangle entirely contains another rectangle
174+
* @param \Quadtree\Geometry\Bounds $other
175+
* @return boolean
176+
*/
177+
public function contains(Bounds $other)
178+
{
179+
return $this->left <= $other->getLeft()
180+
&& $this->top <= $other->getTop()
181+
&& ($this->left + $this->width) >= ($other->getLeft() + $other->getWidth())
182+
&& ($this->top + $this->height) >= ($other->getTop() + $other->getHeight());
183+
}
172184
}

tests/Geometry/Bounds.contains.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
require __DIR__ . '/../bootstrap.php';
4+
5+
use Tester\Assert;
6+
use Quadtree\Geometry\Bounds;
7+
8+
$b0 = new Bounds(100, 100);
9+
Assert::true($b0->contains(new Bounds(10, 10)));
10+
Assert::true($b0->contains(new Bounds(10, 10, 5, 5)));
11+
Assert::true($b0->contains($b0));
12+
Assert::false($b0->contains(new Bounds(200, 200)));
13+
14+
$b = new Bounds(10, 20, 10, 10);
15+
Assert::true($b->contains(new Bounds(5, 5, 15, 15)));
16+
Assert::true($b->contains(new Bounds(5, 5, 15, 25)));
17+
Assert::true($b->contains($b));
18+
Assert::false($b->contains(new Bounds(10, 10)));
19+

0 commit comments

Comments
 (0)