38 public function __construct(
44 public static function zero() :
Vector3{
46 return new self(0, 0, 0);
49 public function getX() :
float|
int{
53 public function getY() :
float|
int{
57 public function getZ() :
float|
int{
61 public function getFloorX() :
int{
62 return (
int) floor($this->x);
65 public function getFloorY() :
int{
66 return (
int) floor($this->y);
69 public function getFloorZ() :
int{
70 return (
int) floor($this->z);
73 public function add(
float|
int $x,
float|
int $y,
float|
int $z) :
Vector3{
74 return new Vector3($this->x + $x, $this->y + $y, $this->z + $z);
78 return $this->add($v->x, $v->y, $v->z);
81 public function subtract(
float|
int $x,
float|
int $y,
float|
int $z) :
Vector3{
82 return $this->add(-$x, -$y, -$z);
86 return $this->add(-$v->x, -$v->y, -$v->z);
89 public function multiply(
float $number) :
Vector3{
90 return new Vector3($this->x * $number, $this->y * $number, $this->z * $number);
93 public function divide(
float $number) :
Vector3{
94 return new Vector3($this->x / $number, $this->y / $number, $this->z / $number);
97 public function ceil() :
Vector3{
98 return new Vector3((
int) ceil($this->x), (
int) ceil($this->y), (
int) ceil($this->z));
101 public function floor() :
Vector3{
102 return new Vector3((
int) floor($this->x), (
int) floor($this->y), (
int) floor($this->z));
108 public function round(
int $precision = 0,
int $mode = PHP_ROUND_HALF_UP) :
Vector3{
109 return $precision > 0 ?
110 new
Vector3(
round($this->x, $precision, $mode),
round($this->y, $precision, $mode),
round($this->z, $precision, $mode)) :
111 new
Vector3((int)
round($this->x, $precision, $mode), (int)
round($this->y, $precision, $mode), (int)
round($this->z, $precision, $mode));
114 public function abs() :
Vector3{
115 return new
Vector3(abs($this->x), abs($this->y), abs($this->z));
122 [$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$side->value] ?? [0, 0, 0];
124 return $this->add($offsetX * $step, $offsetY * $step, $offsetZ * $step);
130 public function down(
int $step = 1){
131 return $this->getSide(Facing::DOWN, $step);
137 public function up(
int $step = 1){
138 return $this->getSide(Facing::UP, $step);
144 public function north(
int $step = 1){
145 return $this->getSide(Facing::NORTH, $step);
151 public function south(
int $step = 1){
152 return $this->getSide(Facing::SOUTH, $step);
158 public function west(
int $step = 1){
159 return $this->getSide(Facing::WEST, $step);
165 public function east(
int $step = 1){
166 return $this->getSide(Facing::EAST, $step);
178 foreach(
Facing::ALL as $facing){
179 yield $facing->value => $this->getSide($facing, $step);
188 public function sidesArray(
bool $keys =
false,
int $step = 1) : array{
189 return iterator_to_array($this->sides($step), $keys);
199 foreach(
Facing::ALL as $facing){
200 if(Facing::axis($facing) !== $axis){
201 yield $facing->value => $this->getSide($facing, $step);
210 return new
Vector3($this->x, $this->y, $this->z);
213 public function distance(
Vector3 $pos) : float{
214 return sqrt($this->distanceSquared($pos));
217 public function distanceSquared(Vector3 $pos) : float{
218 $dx = $this->x - $pos->x;
219 $dy = $this->y - $pos->y;
220 $dz = $this->z - $pos->z;
221 return ($dx * $dx) + ($dy * $dy) + ($dz * $dz);
224 public function length() : float{
225 return sqrt($this->lengthSquared());
228 public function lengthSquared() : float{
229 return $this->x * $this->x + $this->y * $this->y + $this->z * $this->z;
232 public function normalize() : Vector3{
233 $len = $this->lengthSquared();
235 return $this->divide(sqrt($len));
238 return new Vector3(0, 0, 0);
241 public function dot(Vector3 $v) : float{
242 return $this->x * $v->x + $this->y * $v->y + $this->z * $v->z;
245 public function cross(Vector3 $v) : Vector3{
247 $this->y * $v->z - $this->z * $v->y,
248 $this->z * $v->x - $this->x * $v->z,
249 $this->x * $v->y - $this->y * $v->x
253 public function equals(Vector3 $v) : bool{
255 floatval($this->x) === floatval($v->x) and
256 floatval($this->y) === floatval($v->y) and
257 floatval($this->z) === floatval($v->z);
265 $xDiff = $v->x - $this->x;
266 if(($xDiff * $xDiff) < 0.0000001){
270 $f = ($x - $this->x) / $xDiff;
272 if($f < 0 or $f > 1){
275 return new Vector3($x, $this->y + ($v->y - $this->y) * $f, $this->z + ($v->z - $this->z) * $f);
284 $yDiff = $v->y - $this->y;
285 if(($yDiff * $yDiff) < 0.0000001){
289 $f = ($y - $this->y) / $yDiff;
291 if($f < 0 or $f > 1){
294 return new Vector3($this->x + ($v->x - $this->x) * $f, $y, $this->z + ($v->z - $this->z) * $f);
303 $zDiff = $v->z - $this->z;
304 if(($zDiff * $zDiff) < 0.0000001){
308 $f = ($z - $this->z) / $zDiff;
310 if($f < 0 or $f > 1){
313 return new Vector3($this->x + ($v->x - $this->x) * $f, $this->y + ($v->y - $this->y) * $f, $z);
317 public function __toString(){
318 return "Vector3(x=" . $this->x .
",y=" . $this->y .
",z=" . $this->z .
")";
327 if($x !== null || $y !== null || $z !== null){
328 return new self($x ?? $this->x, $y ?? $this->y, $z ?? $this->z);
342 foreach($vectors as $position){
343 $x = max($x, $position->x);
344 $y = max($y, $position->y);
345 $z = max($z, $position->z);
347 return new Vector3($x, $y, $z);
359 foreach($vectors as $position){
360 $x = min($x, $position->x);
361 $y = min($y, $position->y);
362 $z = min($z, $position->z);
364 return new Vector3($x, $y, $z);
367 public static function sum(Vector3 ...$vector3s) : Vector3{
369 foreach($vector3s as $vector3){
374 return new Vector3($x, $y, $z);