Skip to content

Commit

Permalink
Updated docs, and created helper method for Radial Distance
Browse files Browse the repository at this point in the history
  • Loading branch information
emcconville committed Feb 20, 2017
1 parent 2fc8bf1 commit f51784b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,46 @@ $reducedPoints = $reducer->reduce($threshold, $lookAhead);
The original polygon of 2151 points has been reduced to 293.

![Lang example](http://emcconville.com/point-reduction-algorithms/examples/dayton_Lang.svg)

### Zhao-Saalfeld

Added in version `1.2.0`.

```php
use PointReduction\Common\Point,
PointReduction\Algorithms\ZhaoSAalfeld;
$givenPoints = array(
new Point(-84.158640, -39.822480),
new Point(-84.159250, -39.820120),
// ... and so one
);
$degree = 7;
$lookAhead = 7;
$reducer = new ZhaoSAalfeld($givenPoints);
$reducedPoints = $reducer->reduce($degree, $lookAhead);
```

The original polygon of 2151 points has been reduced to 1493.

![Zhao-Saalfeld example](http://emcconville.com/point-reduction-algorithms/examples/dayton_ZhaoSaalfeld.svg)

### Radial Distance

Added in version `1.2.0`.

```php
use PointReduction\Common\Point,
PointReduction\Algorithms\RadialDistance;
$givenPoints = array(
new Point(-84.158640, -39.822480),
new Point(-84.159250, -39.820120),
// ... and so one
);
$tolerance = 0.0025;
$reducer = new RadialDistance($givenPoints);
$reducedPoints = $reducer->reduce($tolerance);
```

The original polygon of 2151 points has been reduced to 449.

![Radial Distance example](http://emcconville.com/point-reduction-algorithms/examples/dayton_RadialDistance.svg)
28 changes: 25 additions & 3 deletions src/Algorithms/RadialDistance.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
*/
class RadialDistance extends Abstraction
{
/**
* Tolerance property to compare radial distance.
* @var double
*/
private $_tolerance = 0.0;

/**
* Remove points with a distance under a given tolerance.
*
Expand All @@ -55,13 +61,12 @@ class RadialDistance extends Abstraction
*/
public function reduce( $tolerance )
{
$this->_tolerance = $tolerance;
$sentinelKey = 0;
while ($sentinelKey < $this->count()-1) {
$testKey = $sentinelKey + 1;
while ( $sentinelKey < $this->count()-1
&& $this->distanceBetweenPoints($this->points[$sentinelKey],
$this->points[$testKey]) < $tolerance )
{
&& $this->_isInTolerance($sentinelKey, $testKey) ) {
unset($this->points[$testKey]);
$testKey++;
}
Expand All @@ -70,4 +75,21 @@ public function reduce( $tolerance )
}
return $this->reindex();
}

/**
* Helper method to ensure clean code.
*
* @param integer $basePointIndex Sentinel point index.
* @param integer $subjectPointIndex Test point index.
*
* @return bool True if subject point is under tolerance, false otherwise.
*/
private function _isInTolerance($basePointIndex, $subjectPointIndex)
{
$radius = $this->distanceBetweenPoints(
$this->points[$basePointIndex],
$this->points[$subjectPointIndex]
);
return $radius < $this->_tolerance;
}
}
1 change: 1 addition & 0 deletions tests/RadialDistanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class RadialDistanceTest extends PHPUnit_Framework_TestCase
* @test
* @covers PointReduction\Algorithms\Abstraction::setPoints
* @covers PointReduction\Algorithms\RadialDistance::reduce
* @covers PointReduction\Algorithms\RadialDistance::_isInTolerance
* @covers PointReduction\Algorithms\Abstraction::lastKey
* @covers PointReduction\Algorithms\Abstraction::count
*
Expand Down

0 comments on commit f51784b

Please sign in to comment.