-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.hpp
executable file
·259 lines (207 loc) · 5.96 KB
/
vector.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* MIT License
* Copyright (c) 2024 BlockyDeer<blockydeer@outlook.com>
* Licensed under the MIT License. See LICENSE file in the project root for full
* license information.
*/
#pragma once
#include <cmath>
#include <string>
#include "result.hpp"
namespace AhogeUtil {
template <typename T>
class Vector2 {
public:
T x;
T y;
Vector2() {
x = 0;
y = 0;
}
Vector2(T x, T y) {
this->x = x;
this->y = y;
}
~Vector2() = default;
bool operator==(Vector2<T> const &other) const {
return ((*this) == other);
}
bool operator!=(Vector2<T> const &other) const {
return !((*this) == other);
}
bool is_parallel(Vector2<T> const &other) const {
if (other.x == 0 && other.y == 0) {
return true;
}
return x / other.x == y / other.y;
}
Vector2<T> const add(Vector2<T> const &other) const {
return Vector2<T>(x + other.x, y + other.y);
}
Vector2<T> const operator+(Vector2<T> const &other) const {
return add(other);
}
Vector2<T> const sub(Vector2 const &other) const {
return Vector2<T>(x - other.x, y - other.y);
}
Vector2<T> const operator-(Vector2 const &other) const {
return sub(other);
}
Vector2<T> const opposite() const { return Vector2<T>(-(x), -(y)); }
Vector2<T> const operator-() const { return opposite(); }
Vector2<T> const multiply(double rate) const {
return Vector2<T>(x * rate, y * rate);
}
Vector2<T> const operator*(double rate) const { return multiply(rate); }
double dot(Vector2<T> const &other) const {
return x * other.x + y * other.y;
}
double operator*(Vector2<T> const &other) const {
return x * other.x + y * other.y;
}
Vector2<T> cross(Vector2<T> const &other) const {
return Vector2<T>(x * other.y, y * other.x);
}
double mod() const { return std::sqrt((double)x * x + y * y); }
void operator+=(Vector2 const &other) { *this = *this + other; }
void operator-=(Vector2 const &other) { *this = *this - other; }
Vector2<T> const normalize() const {
double mod_val = this->mod();
if (mod_val != 0) {
return Vector2<T>(x / (T)mod_val, y / (T)mod_val);
} else {
return Vector2<T>(0, 0);
}
};
};
template <typename T>
class Vector3 {
public:
T x;
T y;
T z;
Vector3() {
x = 0;
y = 0;
z = 0;
}
Vector3(T x, T y) {
this->x = x;
this->y = y;
this->z = 0;
}
Vector3(T x, T y, T z) {
this->x = x;
this->y = y;
this->z = z;
}
Vector3(Vector2<T> const &vec2, T z = 0) {
x = vec2.x;
y = vec2.y;
this->z = z;
}
~Vector3() = default;
bool operator==(Vector3<T> const &other) const {
return ((*this) == other);
}
bool operator!=(Vector3<T> const &other) const {
return !((*this) == other);
}
bool is_parallel(Vector3<T> const &other) {
if (other.x == 0 && other.y == 0 && other.z == 0) {
return true;
}
return (x / other.x == y / other.y) &&
(z / other.z == y / other.y);
}
Vector3<T> const add(T v) const {
return Vector3<T>(x + v, y + v, z + v);
}
Vector3<T> const add(Vector3<T> const &other) const {
return Vector3<T>(x + other.x, y + other.y, z + other.z);
}
Vector3<T> const add(Vector2<T> const &vec) const {
return add(Vector3<T>(vec));
}
Vector3<T> const operator+(Vector3<T> const &other) const {
return add(other);
}
Vector3<T> const sub(Vector3<T> const &other) const {
return Vector3<T>(x - other.x, y - other.y, z - other.z);
}
Vector3<T> const sub(Vector2<T> const &other) const {
return sub(Vector3<T>(other));
}
Vector3<T> opposite() { return Vector3<T>(-x, -y, -z); }
Vector3<T> operator-(Vector3<T> const &other) { return sub(other); }
Vector3<T> const operator-() { return opposite(); }
Vector3<T> const multiply(double rate) const {
return Vector3<T>(x * rate, y * rate, z * rate);
}
Vector3<T> const operator*(double rate) const { return multiply(rate); }
double dot(Vector3<T> const &other) const {
return x * other.x + y * other.y + z * other.z;
}
double operator*(Vector3<T> const &other) const { return dot(other); }
Vector3 cross(Vector3<T> const &other) const {
return Vector3<T>(y * other.z + other.y * z,
-(x * other.z - other.x * z),
x * other.y + other.x * y);
}
double mod() const {
return std::sqrt((double)(x * x + y * y + z * z));
}
void operator+=(Vector3<T> const &other) { *this = add(other); }
void operator-=(Vector3<T> const &other) { *this = sub(other); }
Vector3<T> normalize() const {
double mod = this->mod();
if (mod != 0) {
return Vector3<T>(x / (T)mod, y / (T)mod, z / (T)mod);
} else {
return Vector3<T>(0, 0, 0);
}
}
};
using Vector2d = Vector2<double>;
using Vector2f = Vector2<float>;
using Vector2i = Vector2<int>;
using Vector2l = Vector2<long>;
using Vector3d = Vector3<double>;
using Vector3f = Vector3<float>;
using Vector3i = Vector2<int>;
using Vector3l = Vector3<long>;
Result<size_t> array_vec_to_index(Vector2i const &array_vec, size_t x_width);
template <typename SrcType, typename DestType>
Vector2<DestType> __base_vector_convert(Vector2<SrcType> const &vec) {
return Vector2<DestType>((DestType)vec.x, (DestType)vec.y);
}
#define vector2i2d __base_vector_convert<int, double>
#define vector2d2i __base_vector_convert<double, int>
#define vector2l2d __base_vector_convert<long, double>
#define vector2d2l __base_vector_convert<double, long>
template <typename T>
std::string vec22std(Vector2<T> const &vec) {
return "(" + std::to_string(vec.x) + ", " + std::to_string(vec.y) + ")";
}
template <typename T>
std::string vec32std(Vector2<T> const &vec) {
return "(" + std::to_string(vec.x) + ", " + std::to_string(vec.y) +
", " + std::to_string(vec.z) + ")";
}
template <typename T>
T rooftop(T v, T roof = 0) {
return v > roof ? roof : v;
}
template <typename T>
T lowest_floor(T v, T floor = 0) {
return v < floor ? floor : v;
}
template <typename T>
Vector2<T> vec_rooftop(Vector2<T> const &vec, T roof = 0) {
bool neg_x = vec.x < 0;
bool neg_y = vec.y < 0;
T abs_x = rooftop(std::abs(vec.x), roof);
T abs_y = rooftop(std::abs(vec.y), roof);
return Vector2<T>(neg_x ? -abs_x : abs_x, neg_y ? -abs_y : abs_y);
}
} // namespace AhogeUtil