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

CylindricalCoordinate   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 37.04%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 67
ccs 10
cts 27
cp 0.3704
rs 10
c 1
b 0
f 0
eloc 25
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A distanceTo() 0 3 1
A getPolarAngle() 0 3 1
A asCartesian() 0 11 2
A asSpherical() 0 11 2
A getDistanceFromOrigin() 0 3 1
A asCylindrical() 0 3 1
A getPlanarAngle() 0 3 1
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\ThreeDCoordinateInterface;
8
use Samsara\Fermat\Coordinates\Types\Coordinate;
9
use Samsara\Fermat\Core\Values\ImmutableDecimal;
10
11
class CylindricalCoordinate extends Coordinate implements ThreeDCoordinateInterface
12
{
13
    /** @var CartesianCoordinate */
14
    protected $cachedCartesian;
15
    /** @var SphericalCoordinate */
16
    protected $cachedSpherical;
17
18 4
    public function __construct($r, $theta, $z)
19
    {
20 4
        $data = [
21
            'r' => $r,
22
            'theta' => $theta,
23
            'z' => $z
24
        ];
25
26 4
        parent::__construct($data);
27
    }
28
29
    public function getDistanceFromOrigin(): ImmutableDecimal
30
    {
31
        return $this->getAxis('r');
32
    }
33
34
    public function distanceTo(CoordinateInterface $coordinate): ImmutableDecimal
35
    {
36
        return $this->asCartesian()->distanceTo($coordinate);
37
    }
38
39
    public function getPolarAngle(): ImmutableDecimal
40
    {
41
        return Numbers::makeZero();
42
    }
43
44
    public function getPlanarAngle(): ImmutableDecimal
45
    {
46
        return $this->getAxis('theta');
47
    }
48
49 2
    public function asCartesian(): CartesianCoordinate
50
    {
51 2
        if (is_null($this->cachedCartesian)) {
52 2
            $x = $this->getAxis('r')->multiply($this->getAxis('theta')->cos());
53 2
            $y = $this->getAxis('r')->multiply($this->getAxis('theta')->sin());
54 2
            $z = $this->getAxis('z');
55
56 2
            $this->cachedCartesian = new CartesianCoordinate($x, $y, $z);
57
        }
58
59 2
        return $this->cachedCartesian;
60
    }
61
62
    public function asSpherical(): SphericalCoordinate
63
    {
64
        if (is_null($this->cachedSpherical)) {
65
            $rho = $this->getAxis('r')->pow(2)->add($this->getAxis('z')->pow(2))->sqrt($this->getAxis('z')->getScale());
66
            $theta = $this->getAxis('theta');
67
            $phi = $this->getAxis('z')->divide($rho);
68
69
            $this->cachedSpherical = new SphericalCoordinate($rho, $theta, $phi);
70
        }
71
72
        return $this->cachedSpherical;
73
    }
74
75
    public function asCylindrical(): CylindricalCoordinate
76
    {
77
        return $this;
78
    }
79
}