forked from ifsmirnov/jngen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.h
359 lines (289 loc) · 8.6 KB
/
geometry.h
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#pragma once
#include "array.h"
#include "common.h"
#include "hash.h"
#include "printers.h"
#include "random.h"
#include "repr.h"
#include "rnda.h"
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <set>
#include <tuple>
#include <type_traits>
#include <unordered_set>
namespace jngen {
#ifdef JNGEN_DECLARE_ONLY
extern long double eps;
#else
long double eps = 1e-9;
#endif
inline void setEps(long double value) {
eps = value;
}
template<typename T, typename U, typename Enable = void>
struct Comparator {
static bool eq(T a, U b) { return a == b; }
static bool lt(T a, U b) { return a < b; }
};
template<typename T, typename U>
struct Comparator<T, U, enable_if_t<
std::is_floating_point<T>::value || std::is_floating_point<U>::value,
void>>
{
static bool eq(T a, U b) { return std::abs(b - a) < eps; }
static bool lt(T a, U b) { return a < b - eps; }
};
template<typename T, typename U>
bool eq(T t, U u) {
return Comparator<T, U>().eq(t, u);
}
template<typename T, typename U>
bool lt(T t, U u) {
return Comparator<T, U>().lt(t, u);
}
template<typename T, typename U> bool ne(T t, U u) { return !eq(t, u); }
template<typename T, typename U> bool le(T t, U u) { return !lt(u, t); }
template<typename T, typename U> bool gt(T t, U u) { return lt(u, t); }
template<typename T, typename U> bool ge(T t, U u) { return !lt(t, u); }
template<typename T>
struct TPoint : public ReprProxy<TPoint<T>> {
T x, y;
TPoint() : x(0), y(0) {}
TPoint(T x, T y) : x(x), y(y) {}
template<typename U>
TPoint(const TPoint<U>& other) : x(other.x), y(other.y) {}
TPoint<T> operator+(const TPoint<T>& other) const {
return TPoint<T>(x + other.x, y + other.y);
}
TPoint<T>& operator+=(const TPoint<T>& other) {
x += other.x;
y += other.y;
return *this;
}
TPoint<T> operator-(const TPoint<T>& other) const {
return TPoint<T>(x - other.x, y - other.y);
}
TPoint<T>& operator-=(const TPoint<T>& other) {
x -= other.x;
y -= other.y;
return *this;
}
TPoint<T> operator-() const {
return TPoint<T>(-x, -y);
}
TPoint<T> operator*(T factor) const {
return TPoint<T>(x * factor, y * factor);
}
TPoint<T>& operator*=(T factor) {
x *= factor;
y *= factor;
return *this;
}
T operator*(const TPoint<T>& other) const {
return x * other.x + y * other.y;
}
T operator%(const TPoint<T>& other) const {
return x * other.y - y * other.x;
}
bool operator==(const TPoint<T>& other) const {
return eq(x, other.x) && eq(y, other.y);
}
bool operator!=(const TPoint<T>& other) const {
return !(*this == other);
}
bool operator<(const TPoint<T>& other) const {
if (eq(x, other.x)) {
return lt(y, other.y);
}
return lt(x, other.x);
}
};
using Point = TPoint<long long>;
using Pointf = TPoint<long double>;
template<>
struct Hash<Point> {
uint64_t operator()(const Point& point) const {
uint64_t h = 0;
impl::hashCombine(h, Hash<long long>{}(point.x));
impl::hashCombine(h, Hash<long long>{}(point.y));
return h;
}
};
template<typename T>
JNGEN_DECLARE_SIMPLE_PRINTER(TPoint<T>, 3) {
(void)mod;
out << t.x << " " << t.y;
}
template<typename T>
class TPolygon : public GenericArray<TPoint<T>> {
public:
using Base = GenericArray<TPoint<T>>;
using Base::Base;
TPolygon<T>& shift(const TPoint<T>& vector) {
for (auto &pt: *this) {
pt += vector;
}
return *this;
}
TPolygon<T> shifted(const TPoint<T>& vector) const {
auto res = *this;
res.shift(vector);
return res;
}
TPolygon<T>& reflect() {
for (auto& pt: *this) {
pt = -pt;
}
return *this;
}
TPolygon<T> reflected() const {
auto res = *this;
res.reflect();
return res;
}
};
using Polygon = TPolygon<long long>;
using Polygonf = TPolygon<long double>;
template<>
struct Hash<Polygon> {
uint64_t operator()(const Polygon& p) const {
return Hash<TArray<Point>>{}(p);
}
};
template<typename T>
JNGEN_DECLARE_SIMPLE_PRINTER(TArray<TPoint<T>>, 5) {
// I should avoid copy-paste from array printer here but need to output
// points with '\n' separator. Maybe 'mod' should be made non-const?
if (mod.printN) {
out << t.size() << "\n";
}
bool first = true;
for (const auto& x: t) {
if (first) {
first = false;
} else {
out << '\n';
}
JNGEN_PRINT(x);
}
}
namespace detail {
template<typename T>
TPolygon<T> convexHull(TArray<TPoint<T>> points) {
points.sort().unique();
if (points.size() <= 2u) {
return points;
}
TArray<TPoint<T>> upper(points.begin(), points.begin() + 2);
upper.reserve(points.size());
int top = 1;
for (size_t i = 2; i < points.size(); ++i) {
while (top >= 1 && ge(
(upper[top] - upper[top-1]) % (points[i] - upper[top]), 0ll))
{
upper.pop_back();
--top;
}
upper.push_back(points[i]);
++top;
}
TArray<TPoint<T>> lower(points.begin(), points.begin() + 2);
lower.reserve(points.size());
top = 1;
for (size_t i = 2; i < points.size(); ++i) {
while (top >= 1 && le(
(lower[top] - lower[top-1]) % (points[i] - lower[top]), 0ll))
{
lower.pop_back();
--top;
}
lower.push_back(points[i]);
++top;
}
upper.pop_back();
upper.erase(upper.begin());
return lower + upper.reversed();
}
template<typename T>
TPolygon<T> convexPolygonByEllipse(
int n, Pointf center, Pointf xAxis, Pointf yAxis)
{
return convexHull(rnda.randomf(
n,
[center, xAxis, yAxis] () -> TPoint<T> {
static const long double PI = acosl(-1.0);
long double angle = rnd.next(0., PI*2);
long double sina = sinl(angle);
long double cosa = cosl(angle);
return center + xAxis * cosa + yAxis * sina;
}
));
}
} // namespace detail
class GeometryRandom {
public:
GeometryRandom() {
static bool created = false;
ensure(!created, "jngen::GeometryRandom should be created only once");
created = true;
}
// point in [0, C] x [0, C]
static Point point(long long C);
// point in [min, max] x [min, max]
static Point point(long long min, long long max);
// point in [X1, Y1] x [X2, Y2]
static Point point(
long long X1, long long Y1,
long long X2, long long Y2);
// point in [0, C] x [0, C]
static Point pointf(long double C);
// point in [min, max] x [min, max]
static Point pointf(long double min, long double max);
// point in [X1, Y1] x [X2, Y2]
static Point pointf(
long double X1, long double Y1,
long double X2, long double Y2);
static Polygon convexPolygon(int n, long long C);
static Polygon convexPolygon(int n, long long min, long long max);
static Polygon convexPolygon(
int n,
long long X1, long long Y1,
long long X2, long long Y2);
static TArray<Point> pointsInGeneralPosition(int n, long long C);
static TArray<Point> pointsInGeneralPosition(
int n, long long min, long long max);
static TArray<Point> pointsInGeneralPosition(
int n,
long long X1, long long Y1,
long long X2, long long Y2);
};
JNGEN_EXTERN GeometryRandom rndg;
JNGEN_EXTERN template struct jngen::TPoint<long long>;
JNGEN_EXTERN template struct jngen::TPoint<long double>;
JNGEN_EXTERN template class jngen::TPolygon<long long>;
JNGEN_EXTERN template class jngen::TPolygon<long double>;
JNGEN_EXTERN template TPolygon<long long> detail::convexHull<long long>(
TArray<TPoint<long long>> points);
JNGEN_EXTERN template TPolygon<long double> detail::convexHull<long double>(
TArray<TPoint<long double>> points);
} // namespace jngen
JNGEN_DEFINE_STD_HASH(jngen::Point);
JNGEN_DEFINE_STD_HASH(jngen::Polygon);
using jngen::Point;
using jngen::Pointf;
using jngen::Polygon;
using jngen::Polygonf;
using jngen::rndg;
using jngen::setEps;
// workaround for g++-7
namespace std {
JNGEN_EXTERN template class std::allocator<Point>;
JNGEN_EXTERN template class std::allocator<Pointf>;
} // namespace std
#ifndef JNGEN_DECLARE_ONLY
#define JNGEN_INCLUDE_GEOMETRY_INL_H
#include "impl/geometry_inl.h"
#undef JNGEN_INCLUDE_GEOMETRY_INL_H
#endif // JNGEN_DECLARE_ONLY