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