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
| #include <bits/stdc++.h> using namespace std; typedef long long ll;
const double eps = 1e-8; const double PI = acos(-1.0); int sign(double x) { if (fabs(x) < eps) return 0; if (x < 0) return -1; return 1; } struct Point { double x, y; Point(double a = 0, double b = 0) { x = a, y = b; } Point operator+(const Point &a) const { return Point(x + a.x, y + a.y); } Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); } Point operator*(const double &a) const { return Point(x * a, y * a); } Point operator/(const double &a) const { return Point(x / a, y / a); } bool operator==(const Point &a) const { return !sign(x - a.x) && !sign(y - a.y); } bool operator<(const Point &a) const { return (fabs(x - a.x) < eps) ? (y < a.y) : (x < a.x); } Point left90() { return Point(-y, x); } }; struct Line { Point a, v; Line(Point x = Point(), Point y = Point()) { a = x, v = y; } }; struct Segm { Point a, b; Segm(Point x = Point(), Point y = Point()) { a = x, b = y; } }; struct Circle { Point o; double r; Circle(Point x = Point(), double y = 0) { o = x, r = y; } }; const int dots_num = 2003; struct Poly { int num; Point dots[dots_num]; Poly(int x = 0) { num = x; } };
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double cross(Point a, Point b) { return a.x * b.y - b.x * a.y; } double get_length(Point a) { return sqrt(dot(a, a)); } Point get_line_intersection(Line m, Line n) { Point u = m.a - n.a; if (sign(cross(m.v, n.v)) == 0) return Point(0, 0); double t = cross(n.v, u) / cross(m.v, n.v); return m.a + m.v * t; }
bool cmp(const Line &a, const Line &b) { double da = atan2(a.v.y, a.v.x), db = atan2(b.v.y, b.v.x); if (!sign(da - db)) { return cross(a.v, b.a + b.v - a.a) < 0; } else return da < db; } bool point_on_line_right(Point p, Line l) { return sign(cross(l.v, p - l.a)) < 0; } Line lne[4006]; Poly half_plane_intersection(int cnt) { sort(lne, lne + cnt, cmp); int hh = 0, tt = -1; Line dque[4006]; for (int i = 0; i < cnt; i++) { if (i && !sign(atan2(lne[i].v.y, lne[i].v.x) - atan2(lne[i - 1].v.y, lne[i - 1].v.x))) continue; while (hh + 1 <= tt && point_on_line_right(get_line_intersection(dque[tt - 1], dque[tt]), lne[i])) tt--; while (hh + 1 <= tt && point_on_line_right(get_line_intersection(dque[hh], dque[hh + 1]), lne[i])) hh++; dque[++tt] = lne[i]; } while (hh <= tt && point_on_line_right(get_line_intersection(dque[tt], dque[tt - 1]), dque[hh])) tt--; Poly ans = Poly(); if (tt - hh + 1 >= 3) for (int i = 0; i <= tt - hh; i++) ans.dots[ans.num++] = get_line_intersection(dque[hh + i], dque[hh + (i + 1) % (tt - hh + 1)]); return ans; }
Poly Polygon_shrinkage(Poly pl, double d) { for (int i = 0; i < pl.num; i++) { Line tmp = Line(pl.dots[i], pl.dots[(i + 1) % pl.num] - pl.dots[i]); Point dir = tmp.v.left90(); dir = dir / get_length(dir) * d; lne[i] = Line(tmp.a + dir, tmp.v); } return half_plane_intersection(pl.num); }
double polygon_square(Poly m) { double ans = 0; for (int i = 0; i < m.num; i++) { ans += cross(m.dots[i], m.dots[(i + 1) % m.num]); } return ans / 2; }
double Poly_area_intersection(Poly a, Poly b) { int ct = 0; for (int i = 0; i < a.num; i++) { lne[ct++] = Line(a.dots[i], a.dots[(i + 1) % a.num] - a.dots[i]); } for (int i = 0; i < b.num; i++) { lne[ct++] = Line(b.dots[i], b.dots[(i + 1) % b.num] - b.dots[i]); } return polygon_square(half_plane_intersection(ct)); }
bool on_segment(Point p, Segm m) { Point u = m.a - p, v = m.b - p; return sign(cross(u, v)) == 0 && sign(dot(u, v)) <= 0; } bool point_in_polygon(Point p, Poly m) { for (int i = 0; i < m.num; i++) { if (on_segment(p, Segm(m.dots[(i + 1) % m.num], m.dots[i]))) return 1; if (sign(cross(p - m.dots[i], m.dots[(i + 1) % m.num] - m.dots[i])) > 0) return 0; } return 1; }
int main() { int t; cin >> t; while (t--) { int n; double h, w; scanf("%d%lf%lf", &n, &h, &w); Poly yrd = Poly(n); for (int i = 0; i < n; i++) { scanf("%lf%lf", &yrd.dots[i].x, &yrd.dots[i].y); } yrd = Polygon_shrinkage(yrd, w); Point blb; double hb; scanf("%lf%lf%lf", &blb.x, &blb.y, &hb); if (point_in_polygon(blb, yrd)) { printf("%.10lf\n", polygon_square(yrd)); continue; } else if (sign(hb - h) <= 0 || !yrd.num) { printf("0\n"); continue; } Poly nyrd = Poly(yrd.num); for (int i = 0; i < yrd.num; i++) { Point d = yrd.dots[i] - blb; nyrd.dots[i] = yrd.dots[i] + d * h / (hb - h); } printf("%.10lf\n", Poly_area_intersection(yrd, nyrd)); } return 0; }
|