|
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
|
|
|
public function __construct($rho, $theta) |
|
17
|
|
|
{ |
|
18
|
|
|
$theta = Numbers::makeOrDont(Numbers::IMMUTABLE, $theta); |
|
19
|
|
|
|
|
20
|
|
|
$theta = $theta->continuousModulo(Numbers::TAU); |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
if ($theta->isNegative()) { |
|
23
|
|
|
$theta = Numbers::makeTau($theta->getPrecision())->add($theta); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$data = [ |
|
27
|
|
|
'rho' => $rho, |
|
28
|
|
|
'theta' => $theta |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
parent::__construct($data); |
|
32
|
|
|
} |
|
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
|
|
|
public function asCartesian(): CartesianCoordinate |
|
45
|
|
|
{ |
|
46
|
|
|
if (is_null($this->cachedCartesian)) { |
|
47
|
|
|
$x = $this->getAxis('rho')->multiply($this->getAxis('theta')->cos()); |
|
48
|
|
|
$y = $this->getAxis('rho')->multiply($this->getAxis('theta')->sin()); |
|
49
|
|
|
|
|
50
|
|
|
$this->cachedCartesian = new CartesianCoordinate($x, $y); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->cachedCartesian; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getPolarAngle(): ImmutableDecimal |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->getAxis('theta'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function asPolar(): PolarCoordinate |
|
62
|
|
|
{ |
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |