Skip to content

Commit c190dd6

Browse files
committed
completed point support
1 parent 30a71bb commit c190dd6

File tree

6 files changed

+72
-19
lines changed

6 files changed

+72
-19
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
## Installation
3+
4+
Using [Composer](https://getcomposer.org), just add it to your `composer.json` by running:
5+
6+
```
7+
composer require josue/spatial-hash-table
8+
```
9+
10+
## Usage
11+
12+
```php
13+
<?php
14+
15+
16+
```

composer.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
"description": "2D hash table for fast edges-touching-circle retrieval",
44
"type": "library",
55
"license": "MIT",
6-
"keywords": ["hash table","2d-hash table","spatial query", "spatial"],
7-
"homepage": "https://github.com/Seldaek/monolog",
6+
"keywords": [
7+
"hash table",
8+
"2d-hash table",
9+
"spatial query",
10+
"spatial",
11+
"distance",
12+
"bounds"],
13+
"homepage": "https://github.com/j05u3/spatial-hash-table",
814
"authors": [
915
{
1016
"name": "josue.0",
@@ -14,5 +20,10 @@
1420
"minimum-stability": "alpha",
1521
"require": {
1622
"php": ">=7.0.15"
23+
},
24+
"autoload": {
25+
"psr-0": {
26+
"Location": "src/"
27+
}
1728
}
1829
}

BiHashTable.php renamed to src/BiHashTable.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ public function addElement($e)
141141

142142
$this->addIdToTile($etx, $ety, $e->id);
143143

144+
} else {
145+
$tx = 0;
146+
$ty = 0;
147+
$this->getTileIndex($e, $tx, $ty);
148+
$this->addIdToTile($tx, $ty, $e->id);
144149
}
145150

146151
} else {
@@ -186,23 +191,28 @@ private function getSideSegment($tx, $ty, $side)
186191
}
187192

188193
/**
189-
*
194+
* Returns all elements that intersect the circle centered in $a with radius $len
190195
* @param Point $a
191196
* @return array in which the keys are the ids and the values are the elements
192197
*/
193-
public function getAllElementIdsInCircle(Point $a) {
198+
public function getAllElementsInCircle(Point $a) {
194199
$tx = 0; $ty = 0;
195200
$this->getTileIndex($a, $tx, $ty);
196201
$mSet = [];
197202
for ($i = -1 + $tx; $i < 2 + $tx; $i++) {
198203
for ($j = -1 + $ty; $j < 2 + $ty; $j++) {
199204
if (isset(($this->t)[$i]) && isset(($this->t)[$i][$j])) {
200205
foreach (($this->t)[$i][$j] as $id => $dummy) {
201-
$mSet[$id] = ($this->els)[$id];
202-
// TODO: check distance to agree with len
203-
/*if () {
204-
205-
}*/
206+
if (($this->els)[$id] instanceof Edge) {
207+
if (Point::distToSegment((($this->els)[$id])->p1, (($this->els)[$id])->p2, $a) <= $this->len) {
208+
$mSet[$id] = ($this->els)[$id];
209+
//echo "dist: " . Point::distToSegment((($this->els)[$id])->p1, (($this->els)[$id])->p2, $a) . " id : ".$id."\n";
210+
}
211+
} else {
212+
if (Point::dist(($this->els)[$id], $a) <= $this->len) {
213+
$mSet[$id] = ($this->els)[$id];
214+
}
215+
}
206216
}
207217
}
208218
}
File renamed without changes.

SupportedGeometries/Point.php renamed to src/SupportedGeometries/Point.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,21 @@ public static function dist(Point &$A, Point &$B) {
100100
}
101101

102102
public static function distToLine(Point &$A, Point &$B, Point &$P) {
103-
return abs(cross($A, $B, $P))/self::dist($A, $B);
103+
return abs(self::cross($A, $B, $P))/self::dist($A, $B);
104104
}
105105

106106
public static function distToSegment(Point &$A, Point &$B, Point &$P) {
107-
$mini = min(self::dist($A, $P), self::dist($B, $P));
108-
$distToLine = self::distToLine($A, $B, $P);
109-
if (self::dist($P)) {
110-
// TODO: complete this
111-
return 3;
107+
$da = self::dist($A, $P);
108+
$db = self::dist($B, $P);
109+
$mini = min($da, $db);
110+
$h = self::distToLine($A, $B, $P);
111+
112+
$b = self::dist($A, $B);
113+
$maxHypot2 = $b*$b + $h*$h;
114+
if ($da*$da > $maxHypot2 || $db*$db > $maxHypot2) {
115+
return $mini;
112116
}
117+
118+
return min($mini, $h);
113119
}
114120
}

test.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* Date: 3/30/17
66
* Time: 9:43 PM
77
*/
8-
require_once "BiHashTable.php";
9-
require_once "SupportedGeometries/Edge.php";
10-
require_once "SupportedGeometries/Point.php";
8+
require_once "src/BiHashTable.php";
9+
require_once "src/SupportedGeometries/Edge.php";
10+
require_once "src/SupportedGeometries/Point.php";
1111

1212
use SupportedGeometries\Edge;
1313
use SupportedGeometries\Point;
@@ -50,10 +50,20 @@
5050

5151
$b->addElement(new Edge(new Point(1.5, 1), new Point( 0.5, 0.5), 100));
5252
$b->addElement(new Edge(new Point(1.25, 1.5), new Point( 0.5, 0.75), 200));
53+
$b->addElement(new Edge(new Point(1.75, 1.75), new Point( 1.75, 1.25), 300));
54+
$b->addElement(new Point(0.5, 0.5, 400));
5355

5456
// inverted tricky one
5557
$b->outputHashTable();
5658

5759
echo "\n";
5860

59-
var_dump($b->getAllElementIdsInCircle(new Point(0,0)));
61+
var_dump($b->getAllElementsInCircle(new Point(0,0)));
62+
63+
var_dump($b->getAllElementsInCircle(new Point(2,0)));
64+
65+
var_dump($b->getAllElementsInCircle(new Point(2,1)));
66+
67+
var_dump($b->getAllElementsInCircle(new Point(2,2)));
68+
69+

0 commit comments

Comments
 (0)