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\TwoDCoordinateInterface; |
8
|
|
|
use Samsara\Fermat\Types\Coordinate; |
9
|
|
|
use Samsara\Fermat\Values\ImmutableDecimal; |
10
|
|
|
|
11
|
|
|
class PolarCoordinate extends Coordinate implements TwoDCoordinateInterface |
12
|
|
|
{ |
13
|
|
|
/** @var CartesianCoordinate */ |
14
|
|
|
protected $cachedCartesian; |
15
|
|
|
|
16
|
4 |
|
public function __construct($rho, $theta) |
17
|
|
|
{ |
18
|
4 |
|
$theta = Numbers::makeOrDont(Numbers::IMMUTABLE, $theta); |
19
|
|
|
|
20
|
4 |
|
$theta = $theta->continuousModulo(Numbers::TAU); |
|
|
|
|
21
|
|
|
|
22
|
4 |
|
if ($theta->isNegative()) { |
23
|
|
|
$theta = Numbers::makeTau($theta->getScale())->add($theta); |
24
|
|
|
} |
25
|
|
|
|
26
|
4 |
|
$data = [ |
27
|
4 |
|
'rho' => $rho, |
28
|
4 |
|
'theta' => $theta |
29
|
|
|
]; |
30
|
|
|
|
31
|
4 |
|
parent::__construct($data); |
32
|
4 |
|
} |
33
|
|
|
|
34
|
|
|
public function getDistanceFromOrigin(): ImmutableDecimal |
35
|
|
|
{ |
36
|
|
|
return $this->getAxis('rho'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function distanceTo(CoordinateInterface $coordinate): ImmutableDecimal |
40
|
|
|
{ |
41
|
|
|
return $this->asCartesian()->distanceTo($coordinate); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function asCartesian(): CartesianCoordinate |
45
|
|
|
{ |
46
|
1 |
|
if (is_null($this->cachedCartesian)) { |
47
|
1 |
|
$x = $this->getAxis('rho')->multiply($this->getAxis('theta')->cos()); |
48
|
1 |
|
$y = $this->getAxis('rho')->multiply($this->getAxis('theta')->sin()); |
49
|
|
|
|
50
|
1 |
|
$this->cachedCartesian = new CartesianCoordinate($x, $y); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return $this->cachedCartesian; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public function getPolarAngle(): ImmutableDecimal |
57
|
|
|
{ |
58
|
2 |
|
return $this->getAxis('theta'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function asPolar(): PolarCoordinate |
62
|
|
|
{ |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
} |