1 | /**************************************************************************/ |
2 | /* vector2i.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "vector2i.h" |
32 | |
33 | #include "core/math/vector2.h" |
34 | #include "core/string/ustring.h" |
35 | |
36 | Vector2i Vector2i::clamp(const Vector2i &p_min, const Vector2i &p_max) const { |
37 | return Vector2i( |
38 | CLAMP(x, p_min.x, p_max.x), |
39 | CLAMP(y, p_min.y, p_max.y)); |
40 | } |
41 | |
42 | Vector2i Vector2i::snapped(const Vector2i &p_step) const { |
43 | return Vector2i( |
44 | Math::snapped(x, p_step.x), |
45 | Math::snapped(y, p_step.y)); |
46 | } |
47 | |
48 | int64_t Vector2i::length_squared() const { |
49 | return x * (int64_t)x + y * (int64_t)y; |
50 | } |
51 | |
52 | double Vector2i::length() const { |
53 | return Math::sqrt((double)length_squared()); |
54 | } |
55 | |
56 | Vector2i Vector2i::operator+(const Vector2i &p_v) const { |
57 | return Vector2i(x + p_v.x, y + p_v.y); |
58 | } |
59 | |
60 | void Vector2i::operator+=(const Vector2i &p_v) { |
61 | x += p_v.x; |
62 | y += p_v.y; |
63 | } |
64 | |
65 | Vector2i Vector2i::operator-(const Vector2i &p_v) const { |
66 | return Vector2i(x - p_v.x, y - p_v.y); |
67 | } |
68 | |
69 | void Vector2i::operator-=(const Vector2i &p_v) { |
70 | x -= p_v.x; |
71 | y -= p_v.y; |
72 | } |
73 | |
74 | Vector2i Vector2i::operator*(const Vector2i &p_v1) const { |
75 | return Vector2i(x * p_v1.x, y * p_v1.y); |
76 | } |
77 | |
78 | Vector2i Vector2i::operator*(const int32_t &rvalue) const { |
79 | return Vector2i(x * rvalue, y * rvalue); |
80 | } |
81 | |
82 | void Vector2i::operator*=(const int32_t &rvalue) { |
83 | x *= rvalue; |
84 | y *= rvalue; |
85 | } |
86 | |
87 | Vector2i Vector2i::operator/(const Vector2i &p_v1) const { |
88 | return Vector2i(x / p_v1.x, y / p_v1.y); |
89 | } |
90 | |
91 | Vector2i Vector2i::operator/(const int32_t &rvalue) const { |
92 | return Vector2i(x / rvalue, y / rvalue); |
93 | } |
94 | |
95 | void Vector2i::operator/=(const int32_t &rvalue) { |
96 | x /= rvalue; |
97 | y /= rvalue; |
98 | } |
99 | |
100 | Vector2i Vector2i::operator%(const Vector2i &p_v1) const { |
101 | return Vector2i(x % p_v1.x, y % p_v1.y); |
102 | } |
103 | |
104 | Vector2i Vector2i::operator%(const int32_t &rvalue) const { |
105 | return Vector2i(x % rvalue, y % rvalue); |
106 | } |
107 | |
108 | void Vector2i::operator%=(const int32_t &rvalue) { |
109 | x %= rvalue; |
110 | y %= rvalue; |
111 | } |
112 | |
113 | Vector2i Vector2i::operator-() const { |
114 | return Vector2i(-x, -y); |
115 | } |
116 | |
117 | bool Vector2i::operator==(const Vector2i &p_vec2) const { |
118 | return x == p_vec2.x && y == p_vec2.y; |
119 | } |
120 | |
121 | bool Vector2i::operator!=(const Vector2i &p_vec2) const { |
122 | return x != p_vec2.x || y != p_vec2.y; |
123 | } |
124 | |
125 | Vector2i::operator String() const { |
126 | return "(" + itos(x) + ", " + itos(y) + ")" ; |
127 | } |
128 | |
129 | Vector2i::operator Vector2() const { |
130 | return Vector2((int32_t)x, (int32_t)y); |
131 | } |
132 | |