Test Failed
Pull Request — master (#47)
by Jordan
14:20
created

PolarCoordinate   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A getPolarAngle() 0 3 1
A distanceTo() 0 3 1
A asPolar() 0 3 1
A asCartesian() 0 10 2
A getDistanceFromOrigin() 0 3 1
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);
0 ignored issues
show
Bug introduced by
The method continuousModulo() does not exist on Samsara\Fermat\Types\Bas...Numbers\NumberInterface. It seems like you code against a sub-type of Samsara\Fermat\Types\Bas...Numbers\NumberInterface such as Samsara\Fermat\Types\Bas...umbers\DecimalInterface or Samsara\Fermat\Types\Decimal. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        /** @scrutinizer ignore-call */ 
21
        $theta = $theta->continuousModulo(Numbers::TAU);
Loading history...
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
}