1
1
// class XY - 2D Point
2
2
// class XYZ - 3D Point
3
3
#pragma once
4
+ #ifndef _pln_util_geo_XYZ_H_1c192a5bf5646b833_
5
+ #define _pln_util_geo_XYZ_H_1c192a5bf5646b833_
6
+
4
7
#include " util/geo/iv.h"
5
8
9
+ #undef XY
10
+ #undef XYZ
11
+
6
12
namespace pln {
7
13
14
+ using ipair = std::pair<int , int >;
15
+
8
16
struct XY {
9
17
int x_ = INT_MIN, y_ = INT_MIN;
10
18
11
19
XY () noexcept = default ;
12
20
XY (int a, int b) noexcept : x_(a), y_(b) {}
21
+ XY (ipair p) noexcept : x_(p.first), y_(p.second) {}
13
22
14
23
bool valid () const noexcept { return x_ != INT_MIN; }
15
24
bool nonNeg () const noexcept { return x_ >= 0 ; }
@@ -25,6 +34,12 @@ struct XY {
25
34
y_ = 0 ;
26
35
}
27
36
37
+ void moveRel (int dx, int dy) noexcept {
38
+ x_ += dx;
39
+ y_ += dy;
40
+ }
41
+ void moveRel (XY d) noexcept { moveRel (d.x_ , d.y_ ); }
42
+
28
43
void negate () noexcept {
29
44
x_ = -x_;
30
45
y_ = -y_;
@@ -68,6 +83,11 @@ struct XY {
68
83
y_ -= p.y_ ;
69
84
}
70
85
86
+ void operator = (ipair p) noexcept {
87
+ x_ = p.first ;
88
+ y_ = p.second ;
89
+ }
90
+
71
91
static constexpr int p_round (double x) noexcept {
72
92
if (x >= 0 ) {
73
93
if (x >= double (INT_MAX) - 0.5 ) return INT_MAX;
@@ -107,14 +127,41 @@ struct XY {
107
127
return double (p.x_ - x_) * (p.x_ - x_) + double (p.y_ - y_) * (p.y_ - y_);
108
128
}
109
129
110
- std::string toString () const noexcept {
111
- std::string s{" (" };
112
- s += std::to_string (x_);
113
- s.push_back (' ' );
114
- s += std::to_string (y_);
115
- s.push_back (' )' );
116
- return s;
130
+ uint64_t cantorHash () const noexcept {
131
+ uint64_t a = x_;
132
+ uint64_t b = y_;
133
+ return ((a + b) >> 1 ) * (a + b + 1 ) + b;
134
+ }
135
+ uint64_t fnvHash () const noexcept {
136
+ uint64_t a = x_;
137
+ uint64_t b = y_;
138
+ constexpr uint64_t m = 0xc6a4a7935bd1e995 ;
139
+ return (a ^ (b * m + (a << 6 ) + (a >> 2 ))) + 0xe6546b64 ;
117
140
}
141
+
142
+ static inline std::string i2s (int i) noexcept { return std::to_string (i); }
143
+
144
+ static inline std::string conc (const std::string& a, char ch, const std::string& b) noexcept {
145
+ std::string z;
146
+ z.reserve (a.length () + b.length () + 2 );
147
+ z = a;
148
+ z.push_back (ch);
149
+ z += b;
150
+ return z;
151
+ }
152
+
153
+ static inline std::string conc (char a, const std::string& s, char b) noexcept {
154
+ std::string z;
155
+ z.reserve (s.length () + 3 );
156
+ z.push_back (a);
157
+ z += s;
158
+ z.push_back (b);
159
+ return z;
160
+ }
161
+
162
+ std::string x_spc_y () const noexcept { return conc ( i2s (x_), ' ' , i2s (y_) ); }
163
+ std::string x_com_y () const noexcept { return conc ( i2s (x_), ' ,' , i2s (y_) ); }
164
+ std::string toString () const noexcept { return conc ( ' (' , x_spc_y (), ' )' ); }
118
165
};
119
166
120
167
struct XYZ : public XY {
@@ -137,6 +184,7 @@ struct XYZ : public XY {
137
184
z_ = c;
138
185
}
139
186
void set3 (XYZ u) noexcept { *this = u; }
187
+ void setZ (int zz) noexcept { z_ = zz; }
140
188
141
189
bool operator ==(XYZ u) const noexcept {
142
190
return x_ == u.x_ && y_ == u.y_ && z_ == u.z_ ;
@@ -152,6 +200,17 @@ struct XYZ : public XY {
152
200
}
153
201
154
202
int dist3D (XYZ u) const noexcept { return dist (u) + std::abs (z_ - u.z_ ); }
203
+
204
+ std::string toString () const noexcept {
205
+ std::string s{" (" };
206
+ s += std::to_string (x_);
207
+ s.push_back (' ' );
208
+ s += std::to_string (y_);
209
+ s.push_back (' ' );
210
+ s += std::to_string (z_);
211
+ s.push_back (' )' );
212
+ return s;
213
+ }
155
214
};
156
215
157
216
inline std::ostream& operator <<(std::ostream& os, XY p) {
@@ -167,11 +226,21 @@ inline std::ostream& operator<<(std::ostream& os, const XYZ& u) {
167
226
inline int64_t dot (XY a, XY b) noexcept { return a.dot (b); }
168
227
inline int64_t det (XY a, XY b) noexcept { return a.det (b); }
169
228
229
+ inline XY operator -(XY a, XY b) noexcept {
230
+ return XY (a.x_ - b.x_ , a.y_ - b.y_ );
231
+ }
232
+ inline XY operator +(XY a, XY b) noexcept {
233
+ return XY (a.x_ + b.x_ , a.y_ + b.y_ );
234
+ }
235
+
170
236
inline int abc_ori (XY a, XY b, XY c) noexcept {
171
237
int64_t det = XY::det3 (a, b, c);
172
238
if (det > 0 ) return 1 ;
173
239
if (det < 0 ) return -1 ;
174
240
return 0 ;
175
241
}
176
242
177
- } // namespace pln
243
+ }
244
+
245
+ #endif
246
+
0 commit comments