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

CylindricalCoordinate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 10
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\ThreeDCoordinateInterface;
8
use Samsara\Fermat\Types\Coordinate;
9
use Samsara\Fermat\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 2
    public function __construct($r, $theta, $z)
19
    {
20 2
        $data = [
21 2
            'r' => $r,
22 2
            'theta' => $theta,
23 2
            'z' => $z
24
        ];
25
26 2
        parent::__construct($data);
27 2
    }
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 1
    public function asCartesian(): CartesianCoordinate
50
    {
51 1
        if (is_null($this->cachedCartesian)) {
52 1
            $x = $this->getAxis('r')->multiply($this->getAxis('theta')->cos());
53 1
            $y = $this->getAxis('r')->multiply($this->getAxis('theta')->sin());
54 1
            $z = $this->getAxis('z');
55
56 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

56
            $this->cachedCartesian = new CartesianCoordinate($x, /** @scrutinizer ignore-type */ $y, $z);
Loading history...
57
        }
58
59 1
        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
}