-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec3.php
More file actions
121 lines (107 loc) · 2.66 KB
/
vec3.php
File metadata and controls
121 lines (107 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
require './vec.php';
class vec3 extends vec
{
/**
* @param float $x
* @param float $y
* @param float $z
*/
public function __construct($x = 0.0, $y = 0.0, $z = 0.0)
{
parent::__construct(3);
$this->x = $x;
$this->y = $y;
$this->z = $z;
}
public function offsetExists($offset): bool
{
return ($offset >= 0 && $offset <= 2);
}
public function offsetGet($offset)
{
switch ($offset) {
case 0:
return $this->x;
case 1:
return $this->y;
case 2:
return $this->z;
}
}
public function offsetSet($offset, $value)
{
}
public function offsetUnset($offset)
{
}
public function ceil()
{
$this->x = ceil($this->x);
$this->y = ceil($this->y);
$this->z = ceil($this->z);
}
public function subtract(vec3 $other): vec3
{
return new vec3(
$this->x - $other->x,
$this->y - $other->y,
$this->z - $other->z
);
}
public function add(vec3 $other): vec3
{
return new vec3(
$this->x + $other->x,
$this->y + $other->y,
$this->z + $other->z
);
}
public function length(): float
{
return sqrt($this->x * $this->x + $this->y * $this->y + $this->z * $this->z);
}
public function lengthSq()
{
return ($this->x * $this->x + $this->y * $this->y + $this->z * $this->z);
}
public function normalize()
{
$length = $this->length();
if ($length == 0) {
trigger_error('Vector length is 0, returning without modifying components', E_USER_NOTICE);
return $this;
}
$this->x = $this->x / $length;
$this->y = $this->y / $length;
$this->z = $this->z / $length;
return $this;
}
public function cross(vec3 $other): vec3
{
return new vec3(
$this->y * $other->z - $this->z * $other->y,
$this->z * $other->x - $this->x * $other->z,
$this->x * $other->y - $this->y * $other->x
);
}
/**
* @param number $number
* @return vec3
*/
public function scale($number)
{
if (!is_numeric($number)) {
throw new \Exception('Invalid scalar: ' . $number);
}
return new vec3(
$this->x * $number,
$this->y * $number,
$this->z * $number
);
}
public function dot(vec3 $other)
{
return ($this->x * $other->x + $this->y * $other->y + $this->z * $other->z);
}
}