diff --git a/src/misc/math/Vector2.php b/src/misc/math/Vector2.php new file mode 100644 index 000000000..211aad03d --- /dev/null +++ b/src/misc/math/Vector2.php @@ -0,0 +1,110 @@ +__construct($x->x, $x->y); + }else{ + $this->x = $x; + $this->y = $y; + } + } + + public function getX(){ + return $this->x; + } + + public function getY(){ + return $this->y; + } + + public function getFloorX(){ + return (int) $this->x; + } + + public function getFloorY(){ + return (int) $this->y; + } + + public function add($x = 0, $y = 0){ + if(($x instanceof Vector2) === true){ + return $this->add($x->x, $x->y); + }else{ + $this->x += $x; + $this->y += $y; + return new Vector3($this->x + $x, $this->y + $y); + } + } + + public function subtract($x = 0, $y = 0){ + if(($x instanceof Vector2) === true){ + return $this->add(-$x->x, -$x->y); + }else{ + return $this->add(-$x, -$y); + } + } + + public function ceil(){ + return new Vector2((int) ($this->x + 1), (int) ($this->y + 1)); + } + + public function floor(){ + return new Vector2((int) $this->x, (int) $this->y); + } + + public function round(){ + return new Vector2(round($this->x), round($this->y)); + } + + public function abs(){ + return new Vector2(abs($this->x), abs($this->y)); + } + + public function distance($x = 0, $y = 0){ + if(($x instanceof Vector2) === true){ + return sqrt($this->distanceSquared($x->x, $x->y)); + }else{ + return sqrt($this->distanceSquared($x, $y)); + } + } + + public function distanceSquared($x = 0, $y = 0){ + if(($x instanceof Vector2) === true){ + return $this->distanceSquared($x->x, $x->y); + }else{ + return pow($this->x - $x, 2) + pow($this->y - $y, 2); + } + } + + public function __toString(){ + return "Vector2(x=".$this->x.",y=".$this->y.")"; + } + +} \ No newline at end of file diff --git a/src/misc/math/Vector3.php b/src/misc/math/Vector3.php new file mode 100644 index 000000000..c8d1184d4 --- /dev/null +++ b/src/misc/math/Vector3.php @@ -0,0 +1,140 @@ +__construct($x->x, $x->y, $x->z); + }else{ + $this->x = $x; + $this->y = $y; + $this->z = $z; + } + } + + public function getX(){ + return $this->x; + } + + public function getY(){ + return $this->y; + } + + public function getZ(){ + return $this->z; + } + + public function getFloorX(){ + return (int) $this->x; + } + + public function getFloorY(){ + return (int) $this->y; + } + + public function getFloorZ(){ + return (int) $this->z; + } + + public function getRight(){ + return $this->getX(); + } + + public function getUp(){ + return $this->getY(); + } + + public function getForward(){ + return $this->getZ(); + } + + public function getSouth(){ + return $this->getX(); + } + + public function getWest(){ + return $this->getZ(); + } + + public function add($x = 0, $y = 0, $z = 0){ + if(($x instanceof Vector3) === true){ + return $this->add($x->x, $x->y, $x->z); + }else{ + $this->x += $x; + $this->y += $y; + $this->z += $z; + return new Vector3($this->x + $x, $this->y + $y, $this->z + $z); + } + } + + public function subtract($x = 0, $y = 0, $z = 0){ + if(($x instanceof Vector3) === true){ + return $this->add(-$x->x, -$x->y, -$x->z); + }else{ + return $this->add(-$x, -$y, -$z); + } + } + + public function ceil(){ + return new Vector3((int) ($this->x + 1), (int) ($this->y + 1), (int) ($this->z + 1)); + } + + public function floor(){ + return new Vector3((int) $this->x, (int) $this->y, (int) $this->z); + } + + public function round(){ + return new Vector3(round($this->x), round($this->y), round($this->z)); + } + + public function abs(){ + return new Vector3(abs($this->x), abs($this->y), abs($this->z)); + } + + public function distance($x = 0, $y = 0, $z = 0){ + if(($x instanceof Vector3) === true){ + return sqrt($this->distanceSquared($x->x, $x->y, $x->z)); + }else{ + return sqrt($this->distanceSquared($x, $y, $z)); + } + } + + public function distanceSquared($x = 0, $y = 0, $z = 0){ + if(($x instanceof Vector3) === true){ + return $this->distanceSquared($x->x, $x->y, $x->z); + }else{ + return pow($this->x - $x, 2) + pow($this->y - $y, 2) + pow($this->z - $z, 2); + } + } + + public function __toString(){ + return "Vector3(x=".$this->x.",y=".$this->y.",z=".$this->z.")"; + } + +} \ No newline at end of file