Guest User

Untitled

a guest
Nov 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <queue>
  5. #include <deque>
  6. #include <stack>
  7. #include <map>
  8. #include <vector>
  9. #include <string>
  10. #include <set>
  11. #include <cstdio>
  12. #include <cmath>
  13. #include <ctime>
  14. #include <cstdlib>
  15. #include <cstring>
  16. #include <cassert>
  17.  
  18. #define sz(a) (int)a.size()
  19. #define all(a) a.begin(), a.end()
  20. #define rall(a) a.rbegin(), a.rend()
  21. #define llong long long
  22. #define zero(a) fabs(a) < 1e-9
  23. #define resz(a, n) a.clear(), a.resize(n)
  24. #define same(a, n) memset(a, n, sizeof(a))
  25. #define make(a, b) make_pair(a, b)
  26.  
  27. using namespace std;
  28.  
  29. const int MAXN = 405;
  30. const int MAXM = 1005;
  31. const int INF = 1000000;
  32.  
  33. struct Point {
  34.     double x, y, ang;
  35.     Point(double X = 0, double Y = 0): x(X), y(Y) {}
  36.     void center(int cx, int cy) {
  37.         x -= cx, y -= cy;
  38.     }
  39.     bool operator < (const Point &q) const {
  40.         return ang < q.ang;
  41.     }
  42. };
  43.  
  44. int n, m;
  45. Point p[MAXN];
  46.  
  47. bool equal(double a, double b) {
  48.     return abs(a - b) < 1e-9;
  49. }
  50.  
  51. double dist(Point a, Point b) {
  52.     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
  53. }
  54.  
  55. void order(Point &a, Point &b) {
  56.     if (b.y < a.y)
  57.         swap(a, b);
  58. }
  59.  
  60. bool belong(Point a, Point b, Point c) {
  61.     return (a.y <= c.y && c.y <= b.y && ((a.x <= c.x && c.x <= b.x) || (b.x <= c.x && c.x <= a.x)));
  62. }
  63.  
  64. Point intersect(Point a, Point b, Point c, Point d) {
  65.     if (equal(c.x, d.x)) {
  66.         swap(a, c);
  67.         swap(b, d);
  68.     }
  69.     order(a, b), order(c, d);
  70.     if (equal(c.x, d.x)) {
  71.         if (equal(a.x, c.x) && (belong(a, b, c) || belong(a, b, d)))
  72.             return Point(-INF);
  73.         return Point(INF);
  74.     }
  75.     double t2 = (d.y - c.y) / (d.x - c.x), o2 = c.y - t2 * c.x;
  76.     if (equal(a.x, b.x)) {
  77.         Point ret(a.x, t2 * a.x + o2);
  78.         if (belong(a, b, ret) && belong(c, d, ret))
  79.             return ret;
  80.         return Point(INF);
  81.     }
  82.     double t1 = (b.y - a.y) / (b.x - a.x), o1 = a.y - t1 * a.x, ix = (o2 - o1) / (t1 - t2);
  83.     if (equal(t1, t2) && equal(o1, o2))
  84.         return Point(-INF);
  85.     Point ret(ix, t1 * ix + o1);
  86.     if (belong(a, b, ret) && belong(c, d, ret))
  87.         return ret;
  88.     return Point(INF);
  89. }
  90.  
  91. bool inside(Point q) {
  92.     for (int i = 0; i < n; i++) {
  93.         Point a = p[i], b = p[(i + 1) % n], c(b.x - a.x, b.y - a.y), d(q.x - a.x, q.y - a.y);
  94.         if (c.x * d.y - d.x * c.y < 0)
  95.             return false;
  96.     }
  97.     return true;
  98. }
  99.  
  100. int main() {
  101.     int cx = INF, cy = INF;
  102.     scanf("%d", &n);
  103.     for (int i = 0; i < n; i++) {
  104.         scanf("%lf %lf", &p[i].x, &p[i].y);
  105.         if (p[i].x < cx)
  106.             cx = p[i].x, cy = p[i].y;
  107.         else if (p[i].x == cx && p[i].y < cy)
  108.             cy = p[i].y;
  109.     }
  110.     for (int i = 0; i < n; i++) {
  111.         p[i].center(cx, cy);
  112.         if (p[i].x == 0 && p[i].y == 0)
  113.             p[i].ang = 4;
  114.         else
  115.             p[i].ang = atan2(p[i].y, p[i].x);
  116.     }
  117.     sort(p, p + n);
  118.     scanf("%d", &m);
  119.     for (int i = 0; i < m; i++) {
  120.         Point a, b;
  121.         scanf("%lf %lf %lf %lf", &a.x, &a.y, &b.x, &b.y);
  122.         bool good = true;
  123.         a.center(cx, cy), b.center(cx, cy);
  124.         Point i1(INF), i2(INF);
  125.         for (int j = 0; j < n; j++) {
  126.             Point now = intersect(a, b, p[j], p[(j + 1) % n]);
  127.             if (equal(now.x, INF))
  128.                 continue;
  129.             if (equal(now.x, -INF)) {
  130.                 good = false;
  131.                 break;
  132.             }
  133.             if (equal(i1.x, INF) || (equal(i1.x, now.x) && equal(i1.y, now.y)))
  134.                 i1 = now;
  135.             else
  136.                 i2 = now;
  137.         }
  138.         if (!good) {
  139.             printf("%.6lf\n", 0.0);
  140.             continue;
  141.         }
  142.         bool u = inside(a), v = inside(b);
  143.         if (u && v)
  144.             printf("%.6lf\n", dist(a, b));
  145.         else if (!equal(i2.x, INF))
  146.             printf("%.6lf\n", dist(i1, i2));
  147.         else if (!equal(i1.x, INF) && (u || v)) {
  148.             if (!u)
  149.                 swap(a, b);
  150.             printf("%.6lf\n", dist(i1, a));
  151.         }
  152.         else
  153.             printf("%.6lf\n", 0.0);
  154.     }
  155.     return 0;
  156. }
Add Comment
Please, Sign In to add comment