diff --git a/README.md b/README.md new file mode 100644 index 0000000..c38b307 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ + +## Installation + +Using [Composer](https://getcomposer.org), just add it to your `composer.json` by running: + +``` +composer require josue/spatial-hash-table +``` + +## Usage + +```php +=7.0.15" + }, + "autoload": { + "psr-0": { + "Location": "src/" + } } } diff --git a/BiHashTable.php b/src/BiHashTable.php similarity index 86% rename from BiHashTable.php rename to src/BiHashTable.php index 8f1411d..eaa2847 100644 --- a/BiHashTable.php +++ b/src/BiHashTable.php @@ -141,6 +141,11 @@ public function addElement($e) $this->addIdToTile($etx, $ety, $e->id); + } else { + $tx = 0; + $ty = 0; + $this->getTileIndex($e, $tx, $ty); + $this->addIdToTile($tx, $ty, $e->id); } } else { @@ -186,11 +191,11 @@ private function getSideSegment($tx, $ty, $side) } /** - * + * Returns all elements that intersect the circle centered in $a with radius $len * @param Point $a * @return array in which the keys are the ids and the values are the elements */ - public function getAllElementIdsInCircle(Point $a) { + public function getAllElementsInCircle(Point $a) { $tx = 0; $ty = 0; $this->getTileIndex($a, $tx, $ty); $mSet = []; @@ -198,11 +203,16 @@ public function getAllElementIdsInCircle(Point $a) { for ($j = -1 + $ty; $j < 2 + $ty; $j++) { if (isset(($this->t)[$i]) && isset(($this->t)[$i][$j])) { foreach (($this->t)[$i][$j] as $id => $dummy) { - $mSet[$id] = ($this->els)[$id]; - // TODO: check distance to agree with len - /*if () { - - }*/ + if (($this->els)[$id] instanceof Edge) { + if (Point::distToSegment((($this->els)[$id])->p1, (($this->els)[$id])->p2, $a) <= $this->len) { + $mSet[$id] = ($this->els)[$id]; + //echo "dist: " . Point::distToSegment((($this->els)[$id])->p1, (($this->els)[$id])->p2, $a) . " id : ".$id."\n"; + } + } else { + if (Point::dist(($this->els)[$id], $a) <= $this->len) { + $mSet[$id] = ($this->els)[$id]; + } + } } } } diff --git a/SupportedGeometries/Edge.php b/src/SupportedGeometries/Edge.php similarity index 100% rename from SupportedGeometries/Edge.php rename to src/SupportedGeometries/Edge.php diff --git a/SupportedGeometries/Point.php b/src/SupportedGeometries/Point.php similarity index 86% rename from SupportedGeometries/Point.php rename to src/SupportedGeometries/Point.php index 6c7439f..a0adea3 100644 --- a/SupportedGeometries/Point.php +++ b/src/SupportedGeometries/Point.php @@ -100,15 +100,21 @@ public static function dist(Point &$A, Point &$B) { } public static function distToLine(Point &$A, Point &$B, Point &$P) { - return abs(cross($A, $B, $P))/self::dist($A, $B); + return abs(self::cross($A, $B, $P))/self::dist($A, $B); } public static function distToSegment(Point &$A, Point &$B, Point &$P) { - $mini = min(self::dist($A, $P), self::dist($B, $P)); - $distToLine = self::distToLine($A, $B, $P); - if (self::dist($P)) { - // TODO: complete this - return 3; + $da = self::dist($A, $P); + $db = self::dist($B, $P); + $mini = min($da, $db); + $h = self::distToLine($A, $B, $P); + + $b = self::dist($A, $B); + $maxHypot2 = $b*$b + $h*$h; + if ($da*$da > $maxHypot2 || $db*$db > $maxHypot2) { + return $mini; } + + return min($mini, $h); } } \ No newline at end of file diff --git a/test.php b/test.php index 85ece2b..6b00a05 100644 --- a/test.php +++ b/test.php @@ -5,9 +5,9 @@ * Date: 3/30/17 * Time: 9:43 PM */ -require_once "BiHashTable.php"; -require_once "SupportedGeometries/Edge.php"; -require_once "SupportedGeometries/Point.php"; +require_once "src/BiHashTable.php"; +require_once "src/SupportedGeometries/Edge.php"; +require_once "src/SupportedGeometries/Point.php"; use SupportedGeometries\Edge; use SupportedGeometries\Point; @@ -50,10 +50,20 @@ $b->addElement(new Edge(new Point(1.5, 1), new Point( 0.5, 0.5), 100)); $b->addElement(new Edge(new Point(1.25, 1.5), new Point( 0.5, 0.75), 200)); +$b->addElement(new Edge(new Point(1.75, 1.75), new Point( 1.75, 1.25), 300)); +$b->addElement(new Point(0.5, 0.5, 400)); // inverted tricky one $b->outputHashTable(); echo "\n"; -var_dump($b->getAllElementIdsInCircle(new Point(0,0))); +var_dump($b->getAllElementsInCircle(new Point(0,0))); + +var_dump($b->getAllElementsInCircle(new Point(2,0))); + +var_dump($b->getAllElementsInCircle(new Point(2,1))); + +var_dump($b->getAllElementsInCircle(new Point(2,2))); + +