Completed
Push — master ( defad8...41fcb8 )
by Marcus
02:50
created

PerpendicularDistance::getPerpendicularDistance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 30

Duplication

Lines 47
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 47
loc 47
rs 9.0303
cc 1
eloc 30
nc 1
nop 2
1
<?php
2
/**
3
 * Calculate the perpendicular distance between a Line and a Point
4
 *
5
 * @author   Marcus Jaschen <[email protected]>
6
 * @license  https://opensource.org/licenses/GPL-3.0 GPL
7
 * @link     https://github.com/mjaschen/phpgeo
8
 */
9
10
namespace Location\Utility;
11
12
use Location\Coordinate;
13
use Location\Line;
14
15
/**
16
 * Calculate the perpendicular distance between a Line and a Point
17
 *
18
 * @author   Marcus Jaschen <[email protected]>
19
 * @license  https://opensource.org/licenses/GPL-3.0 GPL
20
 * @link     https://github.com/mjaschen/phpgeo
21
 */
22
class PerpendicularDistance
23
{
24
    /**
25
     * @param Coordinate $point
26
     * @param Line $line
27
     *
28
     * @return float
29
     */
30 View Code Duplication
    public function getPerpendicularDistance(Coordinate $point, Line $line)
0 ignored issues
show
Duplication introduced by Marcus Jaschen
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        $ellipsoid = $point->getEllipsoid();
33
34
        $ellipsoidRadius = $ellipsoid->getArithmeticMeanRadius();
35
36
        $firstLinePointLat = $this->deg2radLatitude($line->getPoint1()->getLat());
37
        $firstLinePointLng = $this->deg2radLongitude($line->getPoint1()->getLng());
38
39
        $firstLinePointX = $ellipsoidRadius * cos($firstLinePointLng) * sin($firstLinePointLat);
40
        $firstLinePointY = $ellipsoidRadius * sin($firstLinePointLng) * sin($firstLinePointLat);
41
        $firstLinePointZ = $ellipsoidRadius * cos($firstLinePointLat);
42
43
        $secondLinePointLat = $this->deg2radLatitude($line->getPoint2()->getLat());
44
        $secondLinePointLng = $this->deg2radLongitude($line->getPoint2()->getLng());
45
46
        $secondLinePointX = $ellipsoidRadius * cos($secondLinePointLng) * sin($secondLinePointLat);
47
        $secondLinePointY = $ellipsoidRadius * sin($secondLinePointLng) * sin($secondLinePointLat);
48
        $secondLinePointZ = $ellipsoidRadius * cos($secondLinePointLat);
49
50
        $pointLat = $this->deg2radLatitude($point->getLat());
51
        $pointLng = $this->deg2radLongitude($point->getLng());
52
53
        $pointX = $ellipsoidRadius * cos($pointLng) * sin($pointLat);
54
        $pointY = $ellipsoidRadius * sin($pointLng) * sin($pointLat);
55
        $pointZ = $ellipsoidRadius * cos($pointLat);
56
57
        $normalizedX = $firstLinePointY * $secondLinePointZ - $firstLinePointZ * $secondLinePointY;
58
        $normalizedY = $firstLinePointZ * $secondLinePointX - $firstLinePointX * $secondLinePointZ;
59
        $normalizedZ = $firstLinePointX * $secondLinePointY - $firstLinePointY * $secondLinePointX;
60
61
        $length = sqrt($normalizedX * $normalizedX + $normalizedY * $normalizedY + $normalizedZ * $normalizedZ);
62
63
        $normalizedX /= $length;
64
        $normalizedY /= $length;
65
        $normalizedZ /= $length;
66
67
        $thetaPoint = $normalizedX * $pointX + $normalizedY * $pointY + $normalizedZ * $pointZ;
68
69
        $length = sqrt($pointX * $pointX + $pointY * $pointY + $pointZ * $pointZ);
70
71
        $thetaPoint /= $length;
72
73
        $distance = abs((M_PI / 2) - acos($thetaPoint));
74
75
        return $distance * $ellipsoidRadius;
76
    }
77
78
    /**
79
     * @param float $latitude
80
     *
81
     * @return float
82
     */
83
    protected function deg2radLatitude($latitude)
84
    {
85
        return deg2rad(90 - $latitude);
86
    }
87
88
    /**
89
     * @param float $longitude
90
     *
91
     * @return float
92
     */
93
    protected function deg2radLongitude($longitude)
94
    {
95
        if ($longitude > 0) {
96
            return deg2rad($longitude);
97
        }
98
99
        return deg2rad($longitude + 360);
100
    }
101
}
102