Point::rotate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2020 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Geometry;
13
14
use WBW\Library\Core\Model\Attribute\FloatXTrait;
15
use WBW\Library\Core\Model\Attribute\FloatYTrait;
16
17
/**
18
 * Point.
19
 *
20
 * @author webeweb <https://github.com/webeweb>
21
 * @package WBW\Library\Core\Geometry
22
 */
23
class Point {
24
25
    use FloatXTrait;
26
    use FloatYTrait;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param float $x The x.
32
     * @param float $y The y.
33
     */
34
    public function __construct($x = null, $y = null) {
35
        $this->setX($x);
36
        $this->setY($y);
37
    }
38
39
    /**
40
     * Distance d = sqrt(a² + b²).
41
     *
42
     * @param Point $p The point.
43
     * @return float Returns the distance.
44
     */
45
    public function d(Point $p): float {
46
47
        $x = $this->getX() - $p->getX();
48
        $y = $this->getY() - $p->getY();
49
50
        $x2 = $x * $x;
51
        $y2 = $y * $y;
52
53
        return sqrt($x2 + $y2);
54
    }
55
56
    /**
57
     * Degree.
58
     *
59
     * @param Point $p The point.
60
     * @return Angle Returns the degree.
61
     */
62
    public function deg(Point $p): Angle {
63
64
        $rad = $this->rad($p);
65
66
        return new Angle($rad->deg(), Angle::UNIT_DEGREE);
67
    }
68
69
    /**
70
     * Slope m = (y2 - y1) / (x2 - x1).
71
     *
72
     * @param Point $p The point.
73
     * @return float Returns the slope.
74
     */
75
    public function m(Point $p): float {
76
77
        $x = $this->getX() - $p->getX();
78
        $y = $this->getY() - $p->getY();
79
80
        if (0 == $x) {
81
            return INF;
82
        }
83
84
        return $y / $x;
85
    }
86
87
    /**
88
     * Radian.
89
     *
90
     * @param Point $p The point.
91
     * @return Angle Returns the radian.
92
     */
93
    public function rad(Point $p): Angle {
94
95
        $m = $this->m($p);
96
        if (INF !== $m) {
97
            return new Angle(atan($m));
98
        }
99
100
        $rad = pi() / 2;
101
102
        return new Angle($p->getY() < $this->getY() ? $rad : -$rad);
103
    }
104
105
    /**
106
     * Rotate.
107
     *
108
     * @param Point $o The point.
109
     * @param Angle $a The angle.
110
     * @return Point Returns the rotated point.
111
     */
112
    public function rotate(Point $o, Angle $a): Point {
113
114
        $rad = $a->rad();
115
116
        $dX = $this->getX() - $o->getX();
117
        $dY = $this->getY() - $o->getY();
118
119
        $x = $dX * cos($rad) + $dY * sin($rad) + $o->getX();
120
        $y = -$dX * sin($rad) + $dY * cos($rad) + $o->getY();
121
122
        return new Point($x, $y);
123
    }
124
}
125