1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Samsara\Fermat\Values\Geometry\CoordinateSystems; |
4
|
|
|
|
5
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Coordinates\CoordinateInterface; |
6
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Coordinates\ThreeDCoordinateInterface; |
7
|
|
|
use Samsara\Fermat\Types\Coordinate; |
8
|
|
|
use Samsara\Fermat\Values\ImmutableDecimal; |
9
|
|
|
|
10
|
|
|
class SphericalCoordinate extends Coordinate implements ThreeDCoordinateInterface |
11
|
|
|
{ |
12
|
|
|
/** @var CartesianCoordinate */ |
13
|
|
|
protected $cachedCartesian; |
14
|
|
|
/** @var CylindricalCoordinate */ |
15
|
|
|
protected $cachedCylindrical; |
16
|
|
|
|
17
|
4 |
|
public function __construct($rho, $theta, $phi) |
18
|
|
|
{ |
19
|
4 |
|
$data = [ |
20
|
4 |
|
'rho' => $rho, |
21
|
4 |
|
'theta' => $theta, |
22
|
4 |
|
'phi' => $phi |
23
|
|
|
]; |
24
|
|
|
|
25
|
4 |
|
parent::__construct($data); |
26
|
4 |
|
} |
27
|
|
|
|
28
|
|
|
public function getDistanceFromOrigin(): ImmutableDecimal |
29
|
|
|
{ |
30
|
|
|
return $this->getAxis('rho'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function distanceTo(CoordinateInterface $coordinate): ImmutableDecimal |
34
|
|
|
{ |
35
|
|
|
return $this->asCartesian()->distanceTo($coordinate); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function getPolarAngle(): ImmutableDecimal |
39
|
|
|
{ |
40
|
1 |
|
return $this->getAxis('phi'); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
public function getPlanarAngle(): ImmutableDecimal |
44
|
|
|
{ |
45
|
1 |
|
return $this->getAxis('theta'); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function asCartesian(): CartesianCoordinate |
49
|
|
|
{ |
50
|
1 |
|
if (is_null($this->cachedCartesian)) { |
51
|
1 |
|
$sinTheta = $this->getAxis('theta')->sin(); |
52
|
1 |
|
$cosTheta = $this->getAxis('theta')->cos(); |
53
|
1 |
|
$sinPhi = $this->getAxis('phi')->sin(); |
54
|
1 |
|
$cosPhi = $this->getAxis('phi')->cos(); |
55
|
|
|
|
56
|
1 |
|
$x = $this->getAxis('rho')->multiply($sinPhi)->multiply($cosTheta); |
57
|
1 |
|
$y = $this->getAxis('rho')->multiply($sinPhi)->multiply($sinTheta); |
58
|
1 |
|
$z = $this->getAxis('rho')->multiply($cosPhi); |
59
|
|
|
|
60
|
1 |
|
$this->cachedCartesian = new CartesianCoordinate($x, $y, $z); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
return $this->cachedCartesian; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function asSpherical(): SphericalCoordinate |
67
|
|
|
{ |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function asCylindrical(): CylindricalCoordinate |
72
|
|
|
{ |
73
|
|
|
if (is_null($this->cachedCylindrical)) { |
74
|
|
|
$sinPhi = $this->getAxis('phi')->sin(); |
75
|
|
|
$cosPhi = $this->getAxis('phi')->cos(); |
76
|
|
|
|
77
|
|
|
$rho = $this->getAxis('rho')->multiply($sinPhi); |
78
|
|
|
$theta = $this->getAxis('theta'); |
79
|
|
|
$z = $this->getAxis('rho')->multiply($cosPhi); |
80
|
|
|
|
81
|
|
|
$this->cachedCylindrical = new CylindricalCoordinate($rho, $theta, $z); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->cachedCylindrical; |
85
|
|
|
} |
86
|
|
|
} |