Skip to content

Commit

Permalink
feat: start work on corridor around a line
Browse files Browse the repository at this point in the history
  • Loading branch information
mjaschen committed Apr 1, 2016
1 parent aec0354 commit 22a1ab9
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Location/Utility/Corridor/LineCorridor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Location\Utility\Corridor;

use Location\Bearing\BearingInterface;
use Location\Line;
use Location\Polygon;

class LineCorridor
{
/**
* @var float distance between line and the corridor border, in meters
*/
private $distance;

/**
* @var \Location\Bearing\BearingInterface
*/
private $bearingCalculator;

/**
* LineCorridor constructor.
*
* @param float $distance
*/
public function __construct($distance, BearingInterface $bearingCalculator)
{
if ($distance <= 0.0) {
throw new \InvalidArgumentException("distance must be greater than 0");
}

$this->distance = $distance;
$this->bearingCalculator = $bearingCalculator;
}

public function createCorridor(Line $line)
{
throw new \RuntimeException("Method not implemented yet.");

$corridor = new Polygon();

$bearingForward = $line->getBearing($this->bearingCalculator);
$bearingReverse = $line->getReverse()->getBearing($this->bearingCalculator);

$corridor->addPoint($this->bearingCalculator->calculateDestination($line->getPoint1(), fmod($bearingForward + 135, 360), $this->distance));
$corridor->addPoint($this->bearingCalculator->calculateDestination($line->getPoint1(), fmod($bearingForward - 135, 360), $this->distance));
$corridor->addPoint($this->bearingCalculator->calculateDestination($line->getPoint2(), fmod($bearingReverse - 135, 360), $this->distance));
$corridor->addPoint($this->bearingCalculator->calculateDestination($line->getPoint2(), fmod($bearingReverse + 135, 360), $this->distance));

return $corridor;
}
}
37 changes: 37 additions & 0 deletions tests/Location/Utility/Corridor/LineCorridorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Location\Bearing\BearingEllipsoidal;
use Location\Coordinate;
use Location\Line;
use Location\Utility\Corridor\LineCorridor;

class LineCorridorTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \InvalidArgumentException
*/
public function testIfZeroDistanceThrowsAnException()
{
$instance = new LineCorridor(0, new BearingEllipsoidal());
}

/**
* @expectedException \InvalidArgumentException
*/
public function testIfNegativeDistanceThrowsAnException()
{
$instance = new LineCorridor(-1, new BearingEllipsoidal());
}

public function testIfSimpleLineCorridorWorksAsExpected()
{
$line = new Line(
new Coordinate(0, 0),
new Coordinate(0.1, 0.1)
);

$instance = new LineCorridor(1000, new BearingEllipsoidal());

$corridor = $instance->createCorridor($line);
}
}

0 comments on commit 22a1ab9

Please sign in to comment.