Treffynnon\Navigator\Distance\Calculator\Haversine::calculate PHP Method

calculate() public method

Supply instances of the coordinate class. http://en.wikipedia.org/wiki/Haversine_formula
public calculate ( Treffynnon\Navigator\LatLong $point1, Treffynnon\Navigator\LatLong $point2 ) : float
$point1 Treffynnon\Navigator\LatLong
$point2 Treffynnon\Navigator\LatLong
return float
    public function calculate(N\LatLong $point1, N\LatLong $point2)
    {
        $celestialBody = $this->getCelestialBody();
        $deltaLat = $point2->getLatitude()->get() - $point1->getLatitude()->get();
        $deltaLong = $point2->getLongitude()->get() - $point1->getLongitude()->get();
        $a = sin($deltaLat / 2) * sin($deltaLat / 2) + cos($point1->getLatitude()->get()) * cos($point2->getLatitude()->get()) * sin($deltaLong / 2) * sin($deltaLong / 2);
        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
        $d = $celestialBody->volumetricMeanRadius * $c * 1000;
        return $d;
    }

Usage Example

Example #1
0
 public function testCalculate()
 {
     $Haversine = new C\Haversine();
     $point1 = new N\LatLong(new N\Coordinate(80.90000000000001), new N\Coordinate(20.1));
     $point2 = new N\LatLong(new N\Coordinate(20.1), new N\Coordinate(80.90000000000001));
     $metres = $Haversine->calculate($point1, $point2);
     $this->assertEquals(7303552.8457791, $metres, '', 0.2);
 }
All Usage Examples Of Treffynnon\Navigator\Distance\Calculator\Haversine::calculate
Haversine