Skip to content

Commit

Permalink
completed point support
Browse files Browse the repository at this point in the history
  • Loading branch information
j05u3 committed Mar 31, 2017
1 parent 30a71bb commit c190dd6
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 19 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
<?php


```
15 changes: 13 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
"description": "2D hash table for fast edges-touching-circle retrieval",
"type": "library",
"license": "MIT",
"keywords": ["hash table","2d-hash table","spatial query", "spatial"],
"homepage": "https://github.com/Seldaek/monolog",
"keywords": [
"hash table",
"2d-hash table",
"spatial query",
"spatial",
"distance",
"bounds"],
"homepage": "https://github.com/j05u3/spatial-hash-table",
"authors": [
{
"name": "josue.0",
Expand All @@ -14,5 +20,10 @@
"minimum-stability": "alpha",
"require": {
"php": ">=7.0.15"
},
"autoload": {
"psr-0": {
"Location": "src/"
}
}
}
24 changes: 17 additions & 7 deletions BiHashTable.php → src/BiHashTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -186,23 +191,28 @@ 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 = [];
for ($i = -1 + $tx; $i < 2 + $tx; $i++) {
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];
}
}
}
}
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
18 changes: 14 additions & 4 deletions test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)));


0 comments on commit c190dd6

Please sign in to comment.