Passed
Push — master ( 372311...b093ab )
by Jordan
02:26
created

SphericalCoordinate::getPlanarAngle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Samsara\Fermat\Values\Geometry\CoordinateSystems;
4
5
use Samsara\Fermat\Types\Base\Interfaces\Coordinates\CoordinateInterface;
6
use Samsara\Fermat\Types\Base\Interfaces\Coordinates\ThreeDCoordinateInterface;
7
use Samsara\Fermat\Types\Coordinate;
8
use Samsara\Fermat\Values\ImmutableDecimal;
9
10
class SphericalCoordinate extends Coordinate implements ThreeDCoordinateInterface
11
{
12
    /** @var CartesianCoordinate */
13
    protected $cachedCartesian;
14
    /** @var CylindricalCoordinate */
15
    protected $cachedCylindrical;
16
17 4
    public function __construct($rho, $theta, $phi)
18
    {
19 4
        $data = [
20 4
            'rho' => $rho,
21 4
            'theta' => $theta,
22 4
            'phi' => $phi
23
        ];
24
25 4
        parent::__construct($data);
26 4
    }
27
28
    public function getDistanceFromOrigin(): ImmutableDecimal
29
    {
30
        return $this->getAxis('rho');
31
    }
32
33
    public function distanceTo(CoordinateInterface $coordinate): ImmutableDecimal
34
    {
35
        return $this->asCartesian()->distanceTo($coordinate);
36
    }
37
38 1
    public function getPolarAngle(): ImmutableDecimal
39
    {
40 1
        return $this->getAxis('phi');
41
    }
42
43 1
    public function getPlanarAngle(): ImmutableDecimal
44
    {
45 1
        return $this->getAxis('theta');
46
    }
47
48 1
    public function asCartesian(): CartesianCoordinate
49
    {
50 1
        if (is_null($this->cachedCartesian)) {
51 1
            $sinTheta = $this->getAxis('theta')->sin();
52 1
            $cosTheta = $this->getAxis('theta')->cos();
53 1
            $sinPhi = $this->getAxis('phi')->sin();
54 1
            $cosPhi = $this->getAxis('phi')->cos();
55
56 1
            $x = $this->getAxis('rho')->multiply($sinPhi)->multiply($cosTheta);
57 1
            $y = $this->getAxis('rho')->multiply($sinPhi)->multiply($sinTheta);
58 1
            $z = $this->getAxis('rho')->multiply($cosPhi);
59
60 1
            $this->cachedCartesian = new CartesianCoordinate($x, $y, $z);
0 ignored issues
show
Bug introduced by
It seems like $y can also be of type Samsara\Fermat\Values\ImmutableDecimal; however, parameter $y of Samsara\Fermat\Values\Ge...ordinate::__construct() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

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

60
            $this->cachedCartesian = new CartesianCoordinate($x, /** @scrutinizer ignore-type */ $y, $z);
Loading history...
Bug introduced by
It seems like $z can also be of type Samsara\Fermat\Values\ImmutableDecimal; however, parameter $z of Samsara\Fermat\Values\Ge...ordinate::__construct() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

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

60
            $this->cachedCartesian = new CartesianCoordinate($x, $y, /** @scrutinizer ignore-type */ $z);
Loading history...
61
        }
62
63 1
        return $this->cachedCartesian;
64
    }
65
66
    public function asSpherical(): SphericalCoordinate
67
    {
68
        return $this;
69
    }
70
71
    public function asCylindrical(): CylindricalCoordinate
72
    {
73
        if (is_null($this->cachedCylindrical)) {
74
            $sinPhi = $this->getAxis('phi')->sin();
75
            $cosPhi = $this->getAxis('phi')->cos();
76
77
            $rho = $this->getAxis('rho')->multiply($sinPhi);
78
            $theta = $this->getAxis('theta');
79
            $z = $this->getAxis('rho')->multiply($cosPhi);
80
81
            $this->cachedCylindrical = new CylindricalCoordinate($rho, $theta, $z);
82
        }
83
84
        return $this->cachedCylindrical;
85
    }
86
}