PocketMine-MP 5.15.1 git-be6754494fdbbb9dd57c058ba0e33a4a78c4581f
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
22declare(strict_types=1);
23
24namespace pocketmine\math;
25
26use function abs;
27use function ceil;
28use function floor;
29use function round;
30use function sqrt;
31
32class Vector2{
33 public function __construct(
34 public float $x,
35 public float $y
36 ){}
37
38 public function getX() : float{
39 return $this->x;
40 }
41
42 public function getY() : float{
43 return $this->y;
44 }
45
46 public function getFloorX() : int{
47 return (int) floor($this->x);
48 }
49
50 public function getFloorY() : int{
51 return (int) floor($this->y);
52 }
53
54 public function add(float $x, float $y) : Vector2{
55 return new Vector2($this->x + $x, $this->y + $y);
56 }
57
58 public function addVector(Vector2 $vector2) : Vector2{
59 return $this->add($vector2->x, $vector2->y);
60 }
61
62 public function subtract(float $x, float $y) : Vector2{
63 return $this->add(-$x, -$y);
64 }
65
66 public function subtractVector(Vector2 $vector2) : Vector2{
67 return $this->add(-$vector2->x, -$vector2->y);
68 }
69
70 public function ceil() : Vector2{
71 return new Vector2((int) ceil($this->x), (int) ceil($this->y));
72 }
73
74 public function floor() : Vector2{
75 return new Vector2((int) floor($this->x), (int) floor($this->y));
76 }
77
78 public function round() : Vector2{
79 return new Vector2(round($this->x), round($this->y));
80 }
81
82 public function abs() : Vector2{
83 return new Vector2(abs($this->x), abs($this->y));
84 }
85
86 public function multiply(float $number) : Vector2{
87 return new Vector2($this->x * $number, $this->y * $number);
88 }
89
90 public function divide(float $number) : Vector2{
91 return new Vector2($this->x / $number, $this->y / $number);
92 }
93
94 public function distance(Vector2 $pos) : float{
95 return sqrt($this->distanceSquared($pos));
96 }
97
98 public function distanceSquared(Vector2 $pos) : float{
99 return (($this->x - $pos->x) ** 2) + (($this->y - $pos->y) ** 2);
100 }
101
102 public function length() : float{
103 return sqrt($this->lengthSquared());
104 }
105
106 public function lengthSquared() : float{
107 return $this->x * $this->x + $this->y * $this->y;
108 }
109
110 public function normalize() : Vector2{
111 $len = $this->lengthSquared();
112 if($len > 0){
113 return $this->divide(sqrt($len));
114 }
115
116 return new Vector2(0, 0);
117 }
118
119 public function dot(Vector2 $v) : float{
120 return $this->x * $v->x + $this->y * $v->y;
121 }
122
123 public function __toString(){
124 return "Vector2(x=" . $this->x . ",y=" . $this->y . ")";
125 }
126
127}