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