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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
| #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <utility> #include <array> #define pb push_back using namespace std; const double pi = acos(-1), eps = 1e-12; int Cmp (double a, double b) { if (a - b > eps) return 1; if (b - a > eps) return -1; return 0; } int Sign (double a) { return Cmp(a, 0); } struct Vector { double x, y; Vector () { } Vector (double a, double b) { x = a, y = b; } void Input () { scanf("%lf%lf", &x, &y); } void Output () { printf("%.10lf %.10lf\n", x + eps, y + eps); } bool operator < (const Vector &_) const { return x != _.x ? x < _.x : y < _.y; } Vector operator + (const Vector &_) const { return Vector(x + _.x, y + _.y); } Vector operator - (const Vector &_) const { return Vector(x - _.x, y - _.y); } Vector operator * (double _) const { return Vector(x * _, y * _); } Vector operator / (double _) const { return Vector(x / _, y / _); } double operator ^ (const Vector &_) const { return x * _.x + y * _.y; } double operator & (const Vector &_) const { return x * _.y - y * _.x; } double Len () const { return hypot(x, y); } Vector Unit () const { return *this / Len(); } Vector Perp () const { return Vector(-y, x); } Vector Rotate (double th) const { return Vector(x * cos(th) - y * sin(th), x * sin(th) + y * cos(th)); } }; using Point = Vector; Point Mid (Point a, Point b) { return (a + b) / 2; } double Area (Point a, Point b, Point c) { return (b - a) & (c - a); } double Angle (Vector a, Vector b) { return atan2(abs(a & b), a ^ b); } double Dis (Point a, Point b) { return (a - b).Len(); } bool PolarLess (Vector a, Vector b) { if ((!a.x && !a.y) || (!b.x && !b.y)) return (a ^ a) < (b ^ b); bool x = a.y < 0 || (a.y == 0 && a.x < 0); bool y = b.y < 0 || (b.y == 0 && b.x < 0); return x != y ? x < y : (a & b) > 0; } bool OnSegment (Point q, Point a, Point b) { if (a.x == b.x && a.y == b.y) return !Sign(Dis(q, a)); return !Sign(Area(a, b, q)) && Sign((q - a) ^ (q - b)) <= 0; } bool IntersectSegment (Point a, Point b, Point c, Point d, bool strict = false) { int x = Sign(Area(a, b, c)), y = Sign(Area(a, b, d)); int z = Sign(Area(c, d, a)), w = Sign(Area(c, d, b)); if (x * y < 0 && z * w < 0) return true; return !strict && (OnSegment(a, c, d) || OnSegment(b, c, d) || OnSegment(c, a, b) || OnSegment(d, a, b)); } struct Segment //表示有向直线,不限制在线段内;方向向量非零 { Point p; Vector t; Segment (Point _p, Vector _t) { p = _p, t = _t; } double Angle () const { return atan2(t.y, t.x); } int On (Point _) const { return Sign(t.Unit() & (_ - p)); } bool operator < (const Segment &_) const { if (PolarLess(t, _.t)) return true; if (PolarLess(_.t, t)) return false; return (t.Unit() & p) > (_.t.Unit() & _.p); } Point Proj (Point _) const { Vector v = t.Unit(); return p + v * (v ^ (_ - p)); } Point Reflect (Point _) const { return Proj(_) * 2 - _; } double Dis (Point _) const { return abs(t.Unit() & (_ - p)); } }; Segment Vertical (Point a, Point b) { return Segment(Mid(a, b), (b - a).Perp()); } Point Intersection (Segment a, Segment b) { Vector u = a.t.Unit(), v = b.t.Unit(); return a.p + u * ((v & (a.p - b.p)) / (u & v)); } double Dis (Point q, Point a, Point b) { Vector v = b - a; double z = v ^ v; if (!z) return Dis(q, a); double t = max(0.0, min(1.0, (v ^ (q - a)) / z)); return Dis(q, a + v * t); } double Dis (Point a, Point b, Point c, Point d) { if (IntersectSegment(a, b, c, d)) return 0; return min(min(Dis(a, c, d), Dis(b, c, d)), min(Dis(c, a, b), Dis(d, a, b))); } struct Circle { Point p; double r; Circle () { } Circle (Point _p, double _r) { p = _p, r = _r; } Circle (Point a, Point b, Point c) { if (!Sign(Area(a, b, c))) { p = Mid(a, b), r = Dis(p, a); if (Cmp(r, Dis(p, c)) < 0) p = Mid(a, c), r = Dis(p, a); if (Cmp(r, Dis(p, b)) < 0) p = Mid(b, c), r = Dis(p, b); return; } p = Intersection(Vertical(a, b), Vertical(a, c)); r = Dis(p, a); } int On (Point q) { return Cmp(r, Dis(p, q)); } vector <Point> Tangent (Point q) { Vector v = q - p; double d = v.Len(); int s = Cmp(d, r); if (s < 0 || !d) return {}; if (!s) return {p + v * (r / d)}; double t = r / d; Point h = p + v * (t * t); Vector w = v.Perp() * (t * sqrt(max(0.0, 1 - t * t))); return {h - w, h + w}; } }; vector <Point> Intersection (Circle c, Segment l) { Point h = l.Proj(c.p); double d = Dis(h, c.p); int s = Cmp(d, c.r); if (s > 0) return {}; if (!s) return {h}; Vector v = l.t.Unit() * sqrt(max(0.0, (c.r - d) * (c.r + d))); return {h - v, h + v}; } vector <Point> Intersection (Circle a, Circle b) { Vector v = b.p - a.p; double d = v.Len(); int s = Cmp(d, a.r + b.r), t = Cmp(d, abs(a.r - b.r)); if (!d || s > 0 || t < 0) return {}; double x = (d + (a.r - b.r) * (a.r + b.r) / d) / 2; Point h = a.p + v * (x / d); if (!s || !t) return {h}; v = v.Perp() * (sqrt(max(0.0, (a.r - x) * (a.r + x))) / d); return {h - v, h + v}; } struct Polygon { vector <Point> p; void Input () { int n; scanf("%d", &n); p.resize(n); for (int i = 0; i < n; ++i) p[i].Input(); } double PolygonArea () { double res = 0; for (int i = 1; i + 1 < p.size(); ++i) res += Area(p[0], p[i], p[i + 1]); return abs(res) / 2; } double PolygonCircumference () { double res = 0; for (int i = 0; i < p.size(); ++i) res += Dis(p[i], p[(i + 1) % p.size()]); return res; } Point Centroid () { Point o = p[0], res(0, 0); double s = 0; for (int i = 0; i < p.size(); ++i) { Vector a = p[i] - o, b = p[(i + 1) % p.size()] - o; double t = a & b; res = res + (a + b) * t, s += t; } return o + res / (3 * s); } int InPolygon (Point q) { int w = 0; for (int i = 0; i < p.size(); ++i) { Point a = p[i], b = p[(i + 1) % p.size()]; if (OnSegment(q, a, b)) return 0; int x = Cmp(a.y, q.y), y = Cmp(b.y, q.y), z = Sign(Area(a, b, q)); if (x <= 0 && y > 0 && z > 0) ++w; if (y <= 0 && x > 0 && z < 0) --w; } return w ? 1 : -1; } int InConvex (Point q) { int n = p.size(); if (!n) return -1; if (n <= 2) return OnSegment(q, p[0], p[n - 1]) ? 0 : -1; int a = Sign(Area(p[0], p[1], q)), b = Sign(Area(p[0], p[n - 1], q)); if (a < 0 || b > 0) return -1; if (!a) return OnSegment(q, p[0], p[1]) ? 0 : -1; if (!b) return OnSegment(q, p[0], p[n - 1]) ? 0 : -1; int l = 1, r = n - 1; while (r - l > 1) { int m = (l + r) / 2; if (Sign(Area(p[0], p[m], q)) >= 0) l = m; else r = m; } return Sign(Area(p[l], p[r], q)); } Polygon Cut (Segment l) { Polygon res; auto add = [&] (Point q) { if (res.p.empty() || Sign(Dis(res.p.back(), q))) res.p.pb(q); }; for (int i = 0; i < p.size(); ++i) { Point a = p[i], b = p[(i + 1) % p.size()]; int x = l.On(a), y = l.On(b); if (x >= 0) add(a); if (x * y < 0) add(Intersection(Segment(a, b - a), l)); } if (res.p.size() > 1 && !Sign(Dis(res.p.front(), res.p.back()))) res.p.pop_back(); return res; } Polygon ConvexHull () { sort(p.begin(), p.end()); p.erase(unique(p.begin(), p.end(), [] (Point a, Point b) { return a.x == b.x && a.y == b.y; }), p.end()); if (p.size() < 3) return *this; vector <int> stk(p.size() * 2); int top = 0; for (int i = 0; i < p.size(); ++i) { while (top >= 2 && Sign(Area(p[stk[top - 1]], p[stk[top]], p[i])) <= 0) --top; stk[++top] = i; } int t = top; for (int i = (int)p.size() - 2; ~i; --i) { while (top > t && Sign(Area(p[stk[top - 1]], p[stk[top]], p[i])) <= 0) --top; stk[++top] = i; } Polygon res; for (int i = 1; i < top; ++i) res.p.pb(p[stk[i]]); return res; } void RotateConvex () { if (p.size() < 2) return; int s = 0; for (int i = 1; i < p.size(); ++i) if (p[i].y < p[s].y || (p[i].y == p[s].y && p[i].x < p[s].x)) s = i; rotate(p.begin(), p.begin() + s, p.end()); } double Diameter () { if (p.size() <= 2) return Dis(p.front(), p.back()); double res = 0; for (int i = 0, j = 1; i < p.size(); ++i) { Point d = p[i], e = p[(i + 1) % p.size()]; while (Cmp(Area(d, e, p[j]), Area(d, e, p[(j + 1) % p.size()])) == -1) j = (j + 1) % p.size(); res = max(res, max(Dis(d, p[j]), Dis(e, p[j]))); } return res; } Circle CircleCover () { random_shuffle(p.begin(), p.end()); Circle c; c.p = p[0], c.r = 0; for (int i = 1; i < p.size(); ++i) { if (Cmp(c.r, Dis(c.p, p[i])) >= 0) continue; c.p = p[i], c.r = 0; for (int j = 0; j < i; ++j) { if (Cmp(c.r, Dis(c.p, p[j])) >= 0) continue; c.p = Mid(p[i], p[j]), c.r = Dis(p[i], p[j]) / 2; for (int k = 0; k < j; ++k) if (Cmp(c.r, Dis(c.p, p[k])) < 0) c = Circle(p[i], p[j], p[k]); } } return c; } Polygon RectangleCover () { double mn = 0; Polygon res; res.p.resize(4); for (int i = 0, a = 2, b = 1, c = 2; i < p.size(); ++i) { Segment l(p[i], p[(i + 1) % p.size()] - p[i]); while (Cmp(l.Dis(p[a]), l.Dis(p[(a + 1) % p.size()])) < 0) a = (a + 1) % p.size(); while (Cmp(l.t ^ (p[b] - l.p), l.t ^ (p[(b + 1) % p.size()] - l.p)) < 0) b = (b + 1) % p.size(); if (!i) c = a; while (Cmp(l.t ^ (p[c] - l.p), l.t ^ (p[(c + 1) % p.size()] - l.p)) > 0) c = (c + 1) % p.size(); double h = l.Dis(p[a]), w = Dis(l.Proj(p[b]), l.Proj(p[c])); if (!i || h * w < mn) { mn = h * w; res.p[0] = l.Proj(p[c]), res.p[1] = l.Proj(p[b]); Vector u = l.t.Unit().Perp() * h; res.p[2] = res.p[1] + u, res.p[3] = res.p[0] + u; } } return res; } }; Polygon Intersection (Polygon a, Polygon b) { for (int i = 0; i < b.p.size() && !a.p.empty(); ++i) a = a.Cut(Segment(b.p[i], b.p[(i + 1) % b.p.size()] - b.p[i])); return a; } Polygon Minkowski (Polygon a, Polygon b) { Polygon res; a.RotateConvex(), b.RotateConvex(); if (a.p.empty() || b.p.empty()) return res; int n = a.p.size(), m = b.p.size(); res.p.pb(a.p[0] + b.p[0]); a.p.pb(a.p[0]), b.p.pb(b.p[0]); for (int i = 0; i < n; ++i) a.p[i] = a.p[i + 1] - a.p[i]; for (int i = 0; i < m; ++i) b.p[i] = b.p[i + 1] - b.p[i]; a.p.resize(n > 1 ? n : 0), b.p.resize(m > 1 ? m : 0); res.p.resize(a.p.size() + b.p.size() + 1); merge(a.p.begin(), a.p.end(), b.p.begin(), b.p.end(), res.p.begin() + 1, PolarLess); for (int i = 1; i < res.p.size(); ++i) res.p[i] = res.p[i - 1] + res.p[i]; if (res.p.size() > 1) res.p.pop_back(); return res; } Polygon HalfPlaneIntersection (vector <Segment> &l) { sort(l.begin(), l.end()); vector <int> q(l.size() + 1); int hd = 0, tl = -1; for (int i = 0; i < l.size(); ++i) { if (i && !PolarLess(l[i - 1].t, l[i].t)) continue; while (hd < tl && l[i].On(Intersection(l[q[tl - 1]], l[q[tl]])) <= 0) --tl; while (hd < tl && l[i].On(Intersection(l[q[hd + 1]], l[q[hd]])) <= 0) ++hd; q[++tl] = i; } while (hd + 1 < tl && l[q[hd]].On(Intersection(l[q[tl - 1]], l[q[tl]])) <= 0) --tl; while (hd + 1 < tl && l[q[tl]].On(Intersection(l[q[hd + 1]], l[q[hd]])) <= 0) ++hd; q[++tl] = q[hd]; Polygon res; for (int i = hd; i < tl; ++i) res.p.pb(Intersection(l[q[i]], l[q[i + 1]])); return res; } bool InCircle (Point a, Point b, Point c, Point d) { a = a - d, b = b - d, c = c - d; return Sign((a ^ a) * (b & c) + (b ^ b) * (c & a) + (c ^ c) * (a & b)) > 0; } struct Delaunay //点集剖分,非多边形剖分;时间 O(n log n),空间 O(n) { struct Edge { int u, next; }; vector <Point> p; vector <int> id, pool; vector <Edge> e; int Rot (int x) { return (x & ~3) | ((x + 1) & 3); } int Rev (int x) { return x ^ 2; } int Org (int x) { return e[x].u; } int Dest (int x) { return Org(Rev(x)); } int Next (int x) { return e[x].next; } int Prev (int x) { return Rot(Next(Rot(x))); } int LNext (int x) { return Rot(Next(Rev(Rot(x)))); } int Side (int v, int x) { return Sign(Area(p[Org(x)], p[Dest(x)], p[v])); } int New (int u, int v) { int x; if (pool.empty()) x = e.size(), e.resize(x + 4); else x = pool.back(), pool.pop_back(); e[x] = {u, x}, e[x + 1] = {-1, x + 3}; e[x + 2] = {v, x + 2}, e[x + 3] = {-1, x + 1}; return x; } void Splice (int a, int b) { int u = Rot(Next(a)), v = Rot(Next(b)); swap(e[a].next, e[b].next), swap(e[u].next, e[v].next); } void Del (int x) { Splice(x, Prev(x)), Splice(Rev(x), Prev(Rev(x))); x &= ~3; e[x].u = e[x + 2].u = -1; pool.pb(x); } int Link (int a, int b) { int x = New(Dest(a), Org(b)); Splice(x, LNext(a)), Splice(Rev(x), b); return x; } pair <int, int> Divide (int l, int r) { if (r - l == 2) { int a = New(l, l + 1); return {a, Rev(a)}; } if (r - l == 3) { int a = New(l, l + 1), b = New(l + 1, l + 2); Splice(Rev(a), b); int t = Sign(Area(p[l], p[l + 1], p[l + 2])); if (!t) return {a, Rev(b)}; int c = Link(b, a); return t > 0 ? make_pair(a, Rev(b)) : make_pair(Rev(c), c); } int m = (l + r) / 2; auto a = Divide(l, m), b = Divide(m, r); int u = a.second, v = b.first; while (true) { if (Side(Org(v), u) > 0) u = LNext(u); else if (Side(Org(u), v) < 0) v = Next(Rev(v)); else break; } int base = Link(Rev(v), u); if (Org(u) == Org(a.first)) a.first = Rev(base); if (Org(v) == Org(b.second)) b.second = base; while (true) { int x = Next(Rev(base)); if (Side(Dest(x), base) < 0) while (InCircle(p[Dest(base)], p[Org(base)], p[Dest(x)], p[Dest(Next(x))])) { int t = Next(x); Del(x), x = t; } int y = Prev(base); if (Side(Dest(y), base) < 0) while (InCircle(p[Dest(base)], p[Org(base)], p[Dest(y)], p[Dest(Prev(y))])) { int t = Prev(y); Del(y), y = t; } bool vx = Side(Dest(x), base) < 0, vy = Side(Dest(y), base) < 0; if (!vx && !vy) break; if (!vx || (vy && InCircle(p[Dest(x)], p[Org(x)], p[Org(y)], p[Dest(y)]))) base = Link(y, Rev(base)); else base = Link(Rev(base), Rev(x)); } return {a.first, b.second}; } void Init (const vector <Point> &a) { int n = a.size(); id.resize(n), p.resize(n), e.clear(), pool.clear(); for (int i = 0; i < n; ++i) id[i] = i; sort(id.begin(), id.end(), [&] (int x, int y) { return a[x] < a[y]; }); for (int i = 0; i < n; ++i) p[i] = a[id[i]]; if (n >= 2) Divide(0, n); } vector <pair <int, int>> GetEdge () { vector <pair <int, int>> res; for (int i = 0; i < e.size(); i += 4) if (Org(i) != -1) res.pb({id[Org(i)], id[Dest(i)]}); return res; } vector <array <int, 3>> GetTriangle () { vector <array <int, 3>> res; for (int i = 0; i < e.size(); i += 2) { if (Org(i) == -1) continue; int j = LNext(i), k = LNext(j); if (LNext(k) == i && i < j && i < k && Side(Org(k), i) > 0) res.pb({id[Org(i)], id[Org(j)], id[Org(k)]}); } return res; } }; int main () { return 0; }
|