Completed
Push — master ( 2b6dad...c372fe )
by WEBEWEB
01:12
created

Point::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
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) {
46
47
        $x = $this->getX() - $p->getY();
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 float Returns the degree.
61
     */
62
    public function deg(Point $p) {
63
        return $this->rad($p) * 180 / pi();
64
    }
65
66
    /**
67
     * Slope m = (y2 - y1) / (x2 - x1).
68
     *
69
     * @param Point $p The point.
70
     * @return float Returns the slope.
71
     */
72
    public function m(Point $p) {
73
74
        $x = $this->getX() - $p->getX();
75
        $y = $this->getY() - $p->getY();
76
77
        if (0 === $x) {
78
            return 0.0;
79
        }
80
81
        return $y / $x;
82
    }
83
84
    /**
85
     * Radian.
86
     *
87
     * @param Point $p The point.
88
     * @return float Returns the radian.
89
     */
90
    public function rad(Point $p) {
91
        return atan($this->m($p));
92
    }
93
94
    /**
95
     * Rotate.
96
     *
97
     * @param Point $o The point.
98
     * @param float $deg The degrees.
99
     * @return Point Returns the rotated point.
100
     */
101
    public function rotate(Point $o, $deg) {
102
103
        $rad = $deg * pi() / 180;
104
105
        $dX = $this->getX() - $o->getX();
106
        $dY = $this->getY() - $o->getY();
107
108
        $x = $dX * cos($rad) + $dY * sin($rad) + $o->getX();
109
        $y = -$dX * sin($rad) + $dY * cos($rad) + $o->getY();
110
111
        return new Point($x, $y);
112
    }
113
}
114