1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\DataTypes; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Vector |
7
|
|
|
* @package SixtyNine\DataTypes |
8
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
9
|
|
|
*/ |
10
|
|
|
class Vector |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var float |
14
|
|
|
*/ |
15
|
|
|
protected $x; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var float |
19
|
|
|
*/ |
20
|
|
|
protected $y; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor |
24
|
|
|
* @param float $x |
25
|
|
|
* @param float $y |
26
|
|
|
*/ |
27
|
30 |
|
public function __construct($x = 0.0, $y = 0.0) |
28
|
|
|
{ |
29
|
30 |
|
$this->x = $x; |
30
|
30 |
|
$this->y = $y; |
31
|
30 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Factory method. |
35
|
|
|
* @param float $x |
36
|
|
|
* @param float $y |
37
|
|
|
* @return Vector |
38
|
|
|
*/ |
39
|
9 |
|
public static function create($x = 0.0, $y = 0.0) : Vector |
40
|
|
|
{ |
41
|
9 |
|
return new self($x, $y); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get Y coordinate. |
46
|
|
|
* @param float $x |
47
|
|
|
* @return Vector |
48
|
|
|
*/ |
49
|
1 |
|
public function setX($x) : Vector |
50
|
|
|
{ |
51
|
1 |
|
$this->x = $x; |
52
|
1 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get X coordinate. |
57
|
|
|
* @return float |
58
|
|
|
*/ |
59
|
24 |
|
public function getX() : float |
60
|
|
|
{ |
61
|
24 |
|
return $this->x; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set Y coordinate. |
66
|
|
|
* @param float $y |
67
|
|
|
* @return Vector |
68
|
|
|
*/ |
69
|
1 |
|
public function setY($y) : Vector |
70
|
|
|
{ |
71
|
1 |
|
$this->y = $y; |
72
|
1 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get Y coordinate. |
77
|
|
|
* @return float |
78
|
|
|
*/ |
79
|
24 |
|
public function getY() : float |
80
|
|
|
{ |
81
|
24 |
|
return $this->y; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Length of vector (magnitude) |
86
|
|
|
* @return float |
87
|
|
|
*/ |
88
|
2 |
|
public function length() : float |
89
|
|
|
{ |
90
|
2 |
|
return sqrt($this->x * $this->x + $this->y * $this->y); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Scalar displacement. |
95
|
|
|
* @param float $deltaX |
96
|
|
|
* @param float $deltaY |
97
|
|
|
* @return Vector |
98
|
|
|
*/ |
99
|
1 |
|
public function move($deltaX, $deltaY) : Vector |
100
|
|
|
{ |
101
|
1 |
|
return new self($this->x + $deltaX, $this->y + $deltaY); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Scalar multiplication. |
106
|
|
|
* @param float $factor |
107
|
|
|
* @return Vector |
108
|
|
|
*/ |
109
|
1 |
|
public function mult($factor) : Vector |
110
|
|
|
{ |
111
|
1 |
|
return new self($this->x * $factor, $this->y * $factor); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Vector addition. |
116
|
|
|
* @param Vector $other |
117
|
|
|
* @return Vector |
118
|
|
|
*/ |
119
|
1 |
|
public function add(Vector $other) : Vector |
120
|
|
|
{ |
121
|
1 |
|
return new self($this->x + $other->getX(), $this->y + $other->getY()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Dot product. |
126
|
|
|
* @param Vector $other |
127
|
|
|
* @return float |
128
|
|
|
*/ |
129
|
1 |
|
public function dot(Vector $other) : float |
130
|
|
|
{ |
131
|
1 |
|
return $this->x * $other->getX() + $this->y * $other->getY(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param Box $box |
136
|
|
|
* @param bool $strict |
137
|
|
|
* @return bool |
138
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
139
|
|
|
*/ |
140
|
4 |
|
public function inside(Box $box, $strict = false) : bool |
141
|
|
|
{ |
142
|
4 |
|
$class = Comparators::class; |
143
|
4 |
|
$comparator = $strict ? array($class, 'strictComparator') : array($class, 'nonStrictComparator'); |
144
|
|
|
|
145
|
4 |
|
return $comparator($box->getLeft(), $this->x) |
146
|
4 |
|
&& $comparator($this->x, $box->getRight()) |
147
|
4 |
|
&& $comparator($box->getTop(), $this->y) |
148
|
4 |
|
&& $comparator($this->y, $box->getBottom()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
1 |
|
public function __toString() : string |
155
|
|
|
{ |
156
|
1 |
|
return sprintf('[%s, %s]', $this->x, $this->y); |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
public function serialize() : string |
160
|
|
|
{ |
161
|
1 |
|
return json_encode([ |
162
|
1 |
|
'x' => $this->x, |
163
|
1 |
|
'y' => $this->y, |
164
|
|
|
]); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|