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