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

SphericalCoordinate   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 10

8 Methods

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