1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHP\Math\Vector; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class VectorUtils |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Adds the two given vectors to each other. |
11
|
|
|
* |
12
|
|
|
* @param Vector $lft The left vector. |
13
|
|
|
* @param Vector $rgt The right vector. |
14
|
|
|
* @return Vector |
15
|
|
|
* @throws InvalidArgumentException Thrown when the given vector is of a different length. |
16
|
|
|
*/ |
17
|
2 |
|
public static function add(Vector $lft, Vector $rgt) |
18
|
|
|
{ |
19
|
2 |
|
if ($lft->getSize() != $rgt->getSize()) { |
20
|
1 |
|
throw new InvalidArgumentException('Invalid vectors provided, should be of the same size.'); |
21
|
|
|
} |
22
|
|
|
|
23
|
1 |
|
$result = new Vector($lft); |
|
|
|
|
24
|
|
|
|
25
|
1 |
|
for ($i = 0; $i < $lft->getSize(); ++$i) { |
26
|
1 |
|
$result[$i]->add($rgt[$i]); |
27
|
1 |
|
} |
28
|
|
|
|
29
|
1 |
|
return $result; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Calculates the angle between the two given vectors. |
34
|
|
|
* |
35
|
|
|
* @param Vector $lft The left vector. |
36
|
|
|
* @param Vector $rgt The right vector. |
37
|
|
|
* @return float Returns the angle in radians. |
38
|
|
|
*/ |
39
|
1 |
|
public static function angleBetween(Vector $lft, Vector $rgt) |
40
|
|
|
{ |
41
|
1 |
|
return acos($lft->dotProduct($rgt)->getValue()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Subtracts the two given vectors from each other. |
46
|
|
|
* |
47
|
|
|
* @param Vector $lft The left vector. |
48
|
|
|
* @param Vector $rgt The right vector. |
49
|
|
|
* @return Vector |
50
|
|
|
* @throws InvalidArgumentException Thrown when the given vector is of a different length. |
51
|
|
|
*/ |
52
|
2 |
|
public static function subtract(Vector $lft, Vector $rgt) |
53
|
|
|
{ |
54
|
2 |
|
if ($lft->getSize() != $rgt->getSize()) { |
55
|
1 |
|
throw new InvalidArgumentException('Invalid vectors provided, should be of the same size.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
$result = new Vector($lft); |
|
|
|
|
59
|
|
|
|
60
|
1 |
|
for ($i = 0; $i < $lft->getSize(); ++$i) { |
61
|
1 |
|
$result[$i]->subtract($rgt[$i]); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
1 |
|
return $result; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: