1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Location; |
6
|
|
|
|
7
|
|
|
use Location\Bearing\BearingInterface; |
8
|
|
|
use Location\Distance\DistanceInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Line Implementation |
12
|
|
|
* |
13
|
|
|
* @author Marcus Jaschen <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Line implements GeometryInterface |
16
|
|
|
{ |
17
|
|
|
use GetBoundsTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Coordinate |
21
|
|
|
*/ |
22
|
|
|
protected $point1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Coordinate |
26
|
|
|
*/ |
27
|
|
|
protected $point2; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Coordinate $point1 |
31
|
|
|
* @param Coordinate $point2 |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Coordinate $point1, Coordinate $point2) |
34
|
|
|
{ |
35
|
|
|
$this->point1 = $point1; |
36
|
|
|
$this->point2 = $point2; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Coordinate $point1 |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function setPoint1(Coordinate $point1) |
45
|
|
|
{ |
46
|
|
|
$this->point1 = $point1; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return Coordinate |
51
|
|
|
*/ |
52
|
|
|
public function getPoint1(): Coordinate |
53
|
|
|
{ |
54
|
|
|
return $this->point1; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Coordinate $point2 |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function setPoint2(Coordinate $point2) |
63
|
|
|
{ |
64
|
|
|
$this->point2 = $point2; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return Coordinate |
69
|
|
|
*/ |
70
|
|
|
public function getPoint2(): Coordinate |
71
|
|
|
{ |
72
|
|
|
return $this->point2; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns an array containing the two points. |
77
|
|
|
* |
78
|
|
|
* @return Coordinate[] |
79
|
|
|
*/ |
80
|
|
|
public function getPoints(): array |
81
|
|
|
{ |
82
|
|
|
return [$this->point1, $this->point2]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Calculates the length of the line (distance between the two |
87
|
|
|
* coordinates). |
88
|
|
|
* |
89
|
|
|
* @param DistanceInterface $calculator instance of distance calculation class |
90
|
|
|
* |
91
|
|
|
* @return float |
92
|
|
|
*/ |
93
|
|
|
public function getLength(DistanceInterface $calculator): float |
94
|
|
|
{ |
95
|
|
|
return $calculator->getDistance($this->point1, $this->point2); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param BearingInterface $bearingCalculator |
100
|
|
|
* |
101
|
|
|
* @return float |
102
|
|
|
*/ |
103
|
|
|
public function getBearing(BearingInterface $bearingCalculator): float |
104
|
|
|
{ |
105
|
|
|
return $bearingCalculator->calculateBearing($this->point1, $this->point2); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param BearingInterface $bearingCalculator |
110
|
|
|
* |
111
|
|
|
* @return float |
112
|
|
|
*/ |
113
|
|
|
public function getFinalBearing(BearingInterface $bearingCalculator): float |
114
|
|
|
{ |
115
|
|
|
return $bearingCalculator->calculateFinalBearing($this->point1, $this->point2); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Create a new instance with reversed point order, i. e. reversed direction. |
120
|
|
|
* |
121
|
|
|
* @return Line |
122
|
|
|
*/ |
123
|
|
|
public function getReverse(): Line |
124
|
|
|
{ |
125
|
|
|
return new static($this->point2, $this->point1); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the midpoint of a Line segment |
130
|
|
|
* |
131
|
|
|
* @return Coordinate |
132
|
|
|
*/ |
133
|
|
|
public function getMidpoint() : Coordinate |
134
|
|
|
{ |
135
|
|
|
return new Coordinate( |
136
|
|
|
(float) ($this->getPoint1()->getLat() + $this->getPoint2()->getLat()) / 2, |
137
|
|
|
(float) ($this->getPoint1()->getLng() + $this->getPoint2()->getLng()) / 2, |
138
|
|
|
); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|