1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHP\Math\Vector; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use PHP\Math\BigNumber\Utils as BigNumberUtils; |
7
|
|
|
|
8
|
|
|
class Vector3 extends Vector2 |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Initializes a new instance of this class. |
12
|
|
|
* |
13
|
|
|
* @param float $x The X-component to set. |
14
|
|
|
* @param float $y The Y-component to set. |
15
|
|
|
* @param float $z The Z-component to set. |
16
|
|
|
*/ |
17
|
5 |
|
public function __construct($x, $y, $z) |
18
|
|
|
{ |
19
|
5 |
|
parent::__construct($x, $y); |
20
|
|
|
|
21
|
5 |
|
$this->setZ($z); |
22
|
5 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Calculates the cross product between this vector and the given vector. |
26
|
|
|
* |
27
|
|
|
* @param Vector3 $vector The vector to calculate with. |
28
|
|
|
* @return Vector3 |
29
|
|
|
*/ |
30
|
1 |
|
public function crossProduct(Vector3 $vector) |
31
|
|
|
{ |
32
|
1 |
|
$x = BigNumberUtils::multiply($this->getY(), $vector->getZ()); |
33
|
1 |
|
$x->subtract(BigNumberUtils::multiply($this->getZ(), $vector->getY())); |
34
|
|
|
|
35
|
1 |
|
$y = BigNumberUtils::multiply($this->getZ(), $vector->getX()); |
36
|
1 |
|
$y->subtract(BigNumberUtils::multiply($this->getX(), $vector->getZ())); |
37
|
|
|
|
38
|
1 |
|
$z = BigNumberUtils::multiply($this->getX(), $vector->getY()); |
39
|
1 |
|
$z->subtract(BigNumberUtils::multiply($this->getY(), $vector->getX())); |
40
|
|
|
|
41
|
1 |
|
return new Vector3($x, $y, $z); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Gets the X-component from this vector. |
46
|
|
|
* |
47
|
|
|
* @return float |
48
|
|
|
*/ |
49
|
3 |
|
public function getZ() |
50
|
|
|
{ |
51
|
3 |
|
return $this->getElement(2); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets the Z-component in this vector. |
56
|
|
|
* |
57
|
|
|
* @param float $z The Z-component to set |
58
|
|
|
*/ |
59
|
5 |
|
public function setZ($z) |
60
|
|
|
{ |
61
|
5 |
|
$this->setElement(2, $z); |
62
|
5 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Validates the index. |
66
|
|
|
* |
67
|
|
|
* @param int $index The index to validate. |
68
|
|
|
* @param bool $indexShouldExists Whether or not the index should exists. |
69
|
|
|
* @throws InvalidArgumentException Thrown when the index is invalid. |
70
|
|
|
*/ |
71
|
5 |
|
protected function validateIndex($index, $indexShouldExists) |
72
|
|
|
{ |
73
|
5 |
|
if ($index < 0 || $index >= 3) { |
74
|
2 |
|
throw new InvalidArgumentException(sprintf('The index %d is invalid.', $index)); |
75
|
|
|
} |
76
|
5 |
|
} |
77
|
|
|
} |
78
|
|
|
|
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: