Completed
Push — master ( 5d186e...d62f94 )
by Jordan
16s queued 13s
created

SphericalCoordinate   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 75
ccs 18
cts 33
cp 0.5455
rs 10
c 1
b 0
f 0
eloc 31
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getDistanceFromOrigin() 0 3 1
A getPolarAngle() 0 3 1
A distanceTo() 0 3 1
A asCartesian() 0 16 2
A getPlanarAngle() 0 3 1
A asSpherical() 0 3 1
A asCylindrical() 0 14 2
1
<?php
2
3
namespace Samsara\Fermat\Coordinates\Values;
4
5
use Samsara\Fermat\Coordinates\Types\Base\Interfaces\Coordinates\CoordinateInterface;
6
use Samsara\Fermat\Coordinates\Types\Base\Interfaces\Coordinates\ThreeDCoordinateInterface;
7
use Samsara\Fermat\Coordinates\Types\Coordinate;
8
use Samsara\Fermat\Core\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 8
    public function __construct($rho, $theta, $phi)
18
    {
19 8
        $data = [
20
            'rho' => $rho,
21
            'theta' => $theta,
22
            'phi' => $phi
23
        ];
24
25 8
        parent::__construct($data);
26
    }
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 2
    public function getPolarAngle(): ImmutableDecimal
39
    {
40 2
        return $this->getAxis('phi');
41
    }
42
43 2
    public function getPlanarAngle(): ImmutableDecimal
44
    {
45 2
        return $this->getAxis('theta');
46
    }
47
48 2
    public function asCartesian(): CartesianCoordinate
49
    {
50 2
        if (is_null($this->cachedCartesian)) {
51 2
            $sinTheta = $this->getAxis('theta')->sin();
52 2
            $cosTheta = $this->getAxis('theta')->cos();
53 2
            $sinPhi = $this->getAxis('phi')->sin();
54 2
            $cosPhi = $this->getAxis('phi')->cos();
55
56 2
            $x = $this->getAxis('rho')->multiply($sinPhi)->multiply($cosTheta);
57 2
            $y = $this->getAxis('rho')->multiply($sinPhi)->multiply($sinTheta);
58 2
            $z = $this->getAxis('rho')->multiply($cosPhi);
59
60 2
            $this->cachedCartesian = new CartesianCoordinate($x, $y, $z);
61
        }
62
63 2
        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
}