PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Vector2.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\math;
23 
28 class Vector2{
29  public $x;
30  public $y;
31 
32  public function __construct($x = 0, $y = 0){
33  $this->x = $x;
34  $this->y = $y;
35  }
36 
37  public function getX(){
38  return $this->x;
39  }
40 
41  public function getY(){
42  return $this->y;
43  }
44 
45  public function getFloorX(){
46  return (int) $this->x;
47  }
48 
49  public function getFloorY(){
50  return (int) $this->y;
51  }
52 
53  public function add($x, $y = 0){
54  if($x instanceof Vector2){
55  return $this->add($x->x, $x->y);
56  }else{
57  return new Vector2($this->x + $x, $this->y + $y);
58  }
59  }
60 
61  public function subtract($x, $y = 0){
62  if($x instanceof Vector2){
63  return $this->add(-$x->x, -$x->y);
64  }else{
65  return $this->add(-$x, -$y);
66  }
67  }
68 
69  public function ceil(){
70  return new Vector2((int) ($this->x + 1), (int) ($this->y + 1));
71  }
72 
73  public function floor(){
74  return new Vector2((int) $this->x, (int) $this->y);
75  }
76 
77  public function round(){
78  return new Vector2(round($this->x), round($this->y));
79  }
80 
81  public function abs(){
82  return new Vector2(abs($this->x), abs($this->y));
83  }
84 
85  public function multiply($number){
86  return new Vector2($this->x * $number, $this->y * $number);
87  }
88 
89  public function divide($number){
90  return new Vector2($this->x / $number, $this->y / $number);
91  }
92 
93  public function distance($x, $y = 0){
94  if($x instanceof Vector2){
95  return sqrt($this->distanceSquared($x->x, $x->y));
96  }else{
97  return sqrt($this->distanceSquared($x, $y));
98  }
99  }
100 
101  public function distanceSquared($x, $y = 0){
102  if($x instanceof Vector2){
103  return $this->distanceSquared($x->x, $x->y);
104  }else{
105  return pow($this->x - $x, 2) + pow($this->y - $y, 2);
106  }
107  }
108 
109  public function length(){
110  return sqrt($this->lengthSquared());
111  }
112 
113  public function lengthSquared(){
114  return $this->x * $this->x + $this->y * $this->y;
115  }
116 
117  public function normalize(){
118  $len = $this->length();
119  if($len != 0){
120  return $this->divide($len);
121  }
122 
123  return new Vector2(0, 0);
124  }
125 
126  public function dot(Vector2 $v){
127  return $this->x * $v->x + $this->y * $v->y;
128  }
129 
130  public function __toString(){
131  return "Vector2(x=" . $this->x . ",y=" . $this->y . ")";
132  }
133 
134 }