|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Samsara\Fermat\Values\Geometry\CoordinateSystems; |
|
4
|
|
|
|
|
5
|
|
|
use Samsara\Exceptions\SystemError\LogicalError\IncompatibleObjectState; |
|
6
|
|
|
use Samsara\Exceptions\UsageError\IntegrityConstraint; |
|
7
|
|
|
use Samsara\Fermat\Numbers; |
|
8
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Coordinates\CoordinateInterface; |
|
9
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Coordinates\ThreeDCoordinateInterface; |
|
10
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Coordinates\TwoDCoordinateInterface; |
|
11
|
|
|
use Samsara\Fermat\Types\Coordinate; |
|
12
|
|
|
use Samsara\Fermat\Values\ImmutableDecimal; |
|
13
|
|
|
|
|
14
|
|
|
class CartesianCoordinate extends Coordinate implements TwoDCoordinateInterface, ThreeDCoordinateInterface |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
4 |
|
public function __construct($x, $y = null, $z = null) |
|
18
|
|
|
{ |
|
19
|
|
|
$data = [ |
|
20
|
4 |
|
'x' => $x |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
4 |
|
if (!is_null($y)) { |
|
24
|
4 |
|
$data['y'] = $y; |
|
25
|
4 |
|
if (!is_null($z)) { |
|
26
|
4 |
|
$data['z'] = $z; |
|
27
|
|
|
} |
|
28
|
|
|
} else { |
|
29
|
|
|
if (!is_null($z)) { |
|
30
|
|
|
$data['y'] = $z; |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
4 |
|
parent::__construct($data); |
|
35
|
4 |
|
} |
|
36
|
|
|
|
|
37
|
4 |
|
public function getAxis($axis): ImmutableDecimal |
|
38
|
|
|
{ |
|
39
|
4 |
|
if (is_int($axis)) { |
|
40
|
3 |
|
$axisIndex = $axis; |
|
41
|
|
|
} else { |
|
42
|
1 |
|
$axisIndex = $this->parameters[$axis]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
4 |
|
if (!$this->values->hasIndex($axisIndex)) { |
|
46
|
|
|
return Numbers::makeZero(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
4 |
|
return $this->getAxisByIndex($axisIndex); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
public function getDistanceFromOrigin(): ImmutableDecimal |
|
53
|
|
|
{ |
|
54
|
3 |
|
$x = 0; |
|
55
|
|
|
|
|
56
|
3 |
|
if ($this->numberOfDimensions() > 1) { |
|
57
|
3 |
|
$y = 0; |
|
58
|
|
|
} else { |
|
59
|
|
|
$y = null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
if ($this->numberOfDimensions() > 2) { |
|
63
|
|
|
$z = 0; |
|
64
|
|
|
} else { |
|
65
|
3 |
|
$z = null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
return $this->distanceTo(new CartesianCoordinate($x, $y, $z)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
public function distanceTo(CoordinateInterface $coordinate): ImmutableDecimal |
|
72
|
|
|
{ |
|
73
|
3 |
|
if (!($coordinate instanceof CartesianCoordinate)) { |
|
74
|
|
|
$coordinate = $coordinate->asCartesian(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** @var ImmutableDecimal $n */ |
|
78
|
3 |
|
$n = Numbers::makeZero(); |
|
79
|
|
|
|
|
80
|
3 |
|
$firstValues = ($this->numberOfDimensions() >= $coordinate->numberOfDimensions()) ? $this->axesValues() : $coordinate->axesValues(); |
|
81
|
3 |
|
$secondValues = ($this->numberOfDimensions() >= $coordinate->numberOfDimensions()) ? $coordinate : $this; |
|
82
|
|
|
|
|
83
|
3 |
|
foreach ($firstValues as $index => $value) { |
|
84
|
3 |
|
$n = $n->add($secondValues->getAxis($index)->subtract($value)->pow(2)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
$n = $n->sqrt(); |
|
88
|
|
|
|
|
89
|
3 |
|
return $n; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function asCartesian(): CartesianCoordinate |
|
93
|
|
|
{ |
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getPolarAngle(): ImmutableDecimal |
|
98
|
|
|
{ |
|
99
|
|
|
if ($this->numberOfDimensions() == 2) { |
|
100
|
|
|
return $this->asPolar()->getAngleOfRotation(); |
|
|
|
|
|
|
101
|
|
|
} elseif ($this->numberOfDimensions() == 3) { |
|
102
|
|
|
return $this->asSpherical()->getPolarAngle(); |
|
103
|
|
|
} else { |
|
104
|
|
|
throw new IncompatibleObjectState( |
|
105
|
|
|
'Can only get a polar angle for a CartesianCoordinate of 2 or 3 dimensions.' |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getPlanarAngle(): ImmutableDecimal |
|
111
|
|
|
{ |
|
112
|
|
|
if ($this->numberOfDimensions() == 2) { |
|
113
|
|
|
return $this->getPolarAngle(); |
|
114
|
|
|
} elseif ($this->numberOfDimensions() == 3) { |
|
115
|
|
|
return $this->asSpherical()->getPlanarAngle(); |
|
116
|
|
|
} else { |
|
117
|
|
|
throw new IncompatibleObjectState( |
|
118
|
|
|
'Can only get a planar angle for a CartesianCoordinate of 2 or 3 dimensions.' |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function asSpherical(): SphericalCoordinate |
|
124
|
|
|
{ |
|
125
|
|
|
if ($this->numberOfDimensions() != 3) { |
|
126
|
|
|
throw new IncompatibleObjectState( |
|
127
|
|
|
'Attempted to get CartesianCoordinate as SphericalCoordinate without the correct number of dimensions.' |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$rho = $this->getDistanceFromOrigin(); |
|
132
|
|
|
$theta = ''; |
|
133
|
|
|
$phi = ''; |
|
134
|
|
|
|
|
135
|
|
|
return new SphericalCoordinate($rho, $theta, $phi); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function asCylindrical(): CylindricalCoordinate |
|
139
|
|
|
{ |
|
140
|
|
|
if ($this->numberOfDimensions() != 3) { |
|
141
|
|
|
throw new IncompatibleObjectState( |
|
142
|
|
|
'Attempted to get CartesianCoordinate as CylindricalCoordinate without the correct number of dimensions.' |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$z = $this->values->get(2); |
|
147
|
|
|
$r = $this->distanceTo(new CartesianCoordinate([0, 0, $z])); |
|
148
|
|
|
$theta = $this->values->get(1)->divide($this->values->get(0))->arctan(); |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
return new CylindricalCoordinate($r, $theta, $z); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
3 |
|
public function asPolar(): PolarCoordinate |
|
154
|
|
|
{ |
|
155
|
3 |
|
if ($this->numberOfDimensions() != 2) { |
|
156
|
|
|
throw new IncompatibleObjectState( |
|
157
|
|
|
'Attempted to get CartesianCoordinate as PolarCoordinate without the correct number of dimensions.' |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
3 |
|
$rho = $this->getDistanceFromOrigin(); |
|
162
|
|
|
|
|
163
|
3 |
|
if ($rho->isEqual(0)) { |
|
164
|
|
|
throw new IncompatibleObjectState( |
|
165
|
|
|
'Attempted to convert a CartesianCoordinate at the origin into PolarCoordinate' |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** @var ImmutableDecimal $theta */ |
|
170
|
3 |
|
$theta = $this->values->get(0)->divide($rho)->arccos(); |
|
|
|
|
|
|
171
|
3 |
|
if ($this->values->get(1)->isNegative()) { |
|
172
|
1 |
|
$theta = $theta->multiply(-1); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
3 |
|
return new PolarCoordinate($rho, $theta); |
|
176
|
|
|
} |
|
177
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.