|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the core-library package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2020 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Library\Core\Geometry; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Line. |
|
16
|
|
|
* |
|
17
|
|
|
* @author webeweb <https://github.com/webeweb> |
|
18
|
|
|
* @package WBW\Library\Core\Geometry |
|
19
|
|
|
*/ |
|
20
|
|
|
class Line { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Point "A". |
|
24
|
|
|
* |
|
25
|
|
|
* @var Point |
|
26
|
|
|
*/ |
|
27
|
|
|
private $a; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Point "B". |
|
31
|
|
|
* |
|
32
|
|
|
* @var Point |
|
33
|
|
|
*/ |
|
34
|
|
|
private $b; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Point $a The point "A". |
|
40
|
|
|
* @param Point $b The point "B". |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(Point $a = null, Point $b = null) { |
|
43
|
|
|
$this->setA($a); |
|
44
|
|
|
$this->setB($b); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Distance d = sqrt(a² + b²). |
|
49
|
|
|
* |
|
50
|
|
|
* @return float Returns the distance. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function d() { |
|
53
|
|
|
return $this->getB()->d($this->getA()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Degree. |
|
58
|
|
|
* |
|
59
|
|
|
* @return float Returns the degree. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function deg() { |
|
62
|
|
|
return $this->getB()->deg($this->getA()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get the point "A". |
|
67
|
|
|
* |
|
68
|
|
|
* @return Point Returns the point "A". |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getA() { |
|
71
|
|
|
return $this->a; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the point "B". |
|
76
|
|
|
* |
|
77
|
|
|
* @return Point Returns the point "B". |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getB() { |
|
80
|
|
|
return $this->b; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Slope m = (y2 - y1) / (x2 - x1). |
|
85
|
|
|
* |
|
86
|
|
|
* @return float Returns the slope. |
|
87
|
|
|
*/ |
|
88
|
|
|
public function m() { |
|
89
|
|
|
|
|
90
|
|
|
return $this->getB()->m($this->getA()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Originally abscissa y = mx + b. |
|
95
|
|
|
* |
|
96
|
|
|
* @return Point Returns the originally abscissa. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function originallyAbscissa() { |
|
99
|
|
|
|
|
100
|
|
|
$m = $this->getB()->m($this->getA()); |
|
101
|
|
|
$b = $this->originallyOrdered()->getY(); |
|
102
|
|
|
|
|
103
|
|
|
// x = (y - b) / m |
|
104
|
|
|
$x = (0 - $b) / $m; |
|
105
|
|
|
|
|
106
|
|
|
return new Point($x, 0); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Originally ordered y = mx + b. |
|
111
|
|
|
* |
|
112
|
|
|
* @return Point Returns the originally ordered. |
|
113
|
|
|
*/ |
|
114
|
|
|
public function originallyOrdered() { |
|
115
|
|
|
|
|
116
|
|
|
$m = $this->getB()->m($this->getA()); |
|
117
|
|
|
$mx = $m * $this->getB()->getX(); |
|
118
|
|
|
|
|
119
|
|
|
// b = y - mx |
|
120
|
|
|
$b = $this->getB()->getY() - $mx; |
|
121
|
|
|
|
|
122
|
|
|
return new Point(0, $b); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Radian. |
|
127
|
|
|
* |
|
128
|
|
|
* @return float Returns the radian. |
|
129
|
|
|
*/ |
|
130
|
|
|
public function rad() { |
|
131
|
|
|
return $this->getB()->rad($this->getA()); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Rotate. |
|
136
|
|
|
* |
|
137
|
|
|
* @param Point $o The point. |
|
138
|
|
|
* @param float $deg The degrees. |
|
139
|
|
|
* @return Line Returns the rotated line. |
|
140
|
|
|
*/ |
|
141
|
|
|
public function rotate(Point $o, $deg) { |
|
142
|
|
|
|
|
143
|
|
|
$a2 = $this->getA()->rotate($o, $deg); |
|
144
|
|
|
$b2 = $this->getB()->rotate($o, $deg); |
|
145
|
|
|
|
|
146
|
|
|
return new Line($a2, $b2); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Set the point "A". |
|
151
|
|
|
* |
|
152
|
|
|
* @param Point $a The point "A". |
|
153
|
|
|
* @return Line Returns this line. |
|
154
|
|
|
*/ |
|
155
|
|
|
public function setA(Point $a = null) { |
|
156
|
|
|
$this->a = $a; |
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Set the point "B". |
|
162
|
|
|
* |
|
163
|
|
|
* @param Point $b The point "B". |
|
164
|
|
|
* @return Line Returns this line. |
|
165
|
|
|
*/ |
|
166
|
|
|
public function setB(Point $b = null) { |
|
167
|
|
|
$this->b = $b; |
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|