#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef pair <double,double> dd; typedef pair <ii,int> iii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 105; const double eps = 1e-9; struct pI { int x, y; }; struct pD { double x, y; }; struct line { pI a1; pI a2; }; int detI(pI& a, pI& b, pI& c) { return a.x * b.y + b.x * c.y + c.x * a.y - a.x * c.y - b.x * a.y - c.x * b.y; } double detD(pD& a, pD& b, pD& c) { return a.x * b.y + b.x * c.y + c.x * a.y - a.x * c.y - b.x * a.y - c.x * b.y; } // funkcja sprawdza czy przechodzac z a do b i c skrecamy w lewo(1), w prawo(-1) czy punkty są wspollniowe(0) int ccwI(pI& a, pI& b, pI& c) { int d = detI(a, b, c); return (d > 0) ? 1 : ((d < 0) ? -1 : 0); } // funkcja sprawdza czy przechodząc z a do b i c skręcamy w lewo(1), w prawo(-1) czy punkty są współlniowe(0) int ccwD(pD& a, pD& b, pD& c) { double d = detD(a, b, c); return (d > eps) ? 1 : ((d < -eps) ? -1 : 0); } // funkcja sprawdza położenie punktu c wspolliniowego z odcinkiem a-b // zwraca 1 - c lezy na odcinku, 0 - nie bool inside(pI& a, pI& b, pI& c) { return (c.x <= max(a.x,b.x) && c.x >= min(a.x,b.x) && c.y <= max(a.y,b.y) && c.y >= min(a.y,b.y)); } char get_line_intersection(int p0_x, int p0_y, int p1_x, int p1_y, int p2_x, int p2_y, int p3_x, int p3_y, double& i_x, double& i_y) { int s1_x, s1_y, s2_x, s2_y; s1_x = p1_x - p0_x; s1_y = p1_y - p0_y; s2_x = p3_x - p2_x; s2_y = p3_y - p2_y; double s, t; s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) * 1.0 / (-s2_x * s1_y + s1_x * s2_y); t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) * 1.0 / (-s2_x * s1_y + s1_x * s2_y); if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { // Collision detected i_x = p0_x + (t * s1_x); i_y = p0_y + (t * s1_y); return 1; } return 0; // No collision } pI p1[N], p2[N]; int i, j, n; double xx, yy; vector<line> vec; vector<int> vecI; vector<pD> crosses; VI res; void check2(pI& p_1, pI& p_2, pI& p_3, pI& p_4, int a, int b) { int w1 = ccwI(p_1, p_2, p_3); int w2 = ccwI(p_1, p_2, p_4); if (w1 * w2 == -1) return; if (w1 == 0 && inside(p_1, p_2, p_3)) return; if (w2 == 0 && inside(p_1, p_2, p_4)) return; for (int i=0;i<n;i++) if (i != a && i != b) { int w3 = ccwI(p_1, p_2, p1[i]); int w4 = ccwI(p_1, p_2, p2[i]); if (w3 * w4 == 1) { if (w1 == w3 || w2 == w3) return; } } line l; l.a1 = p_1; l.a2 = p_2; vec.pb(l); vecI.pb(w1 != 0 ? w1 : w2); } void check(int a, int b) { check2(p1[a], p1[b], p2[a], p2[b], a, b); check2(p1[a], p2[b], p2[a], p1[b], a, b); check2(p2[a], p1[b], p1[a], p2[b], a, b); check2(p2[a], p2[b], p1[a], p1[b], a, b); } void convexHull(VI& res) { int n = crosses.size(); int ind = 0, i, kol[n]; double dx, dy; for (i = 1; i < n; i++) if (dd(crosses[i].y, crosses[i].x) < dd(crosses[ind].y, crosses[ind].x)) ind = i; dx = crosses[ind].x; dy = crosses[ind].y; for (i = 0; i < n; i++) { crosses[i].x -= dx; crosses[i].y -= dy; kol[i] = i; } sort(kol, kol + n, [] (const int& lhs, const int& rhs) { double diff = crosses[lhs].x * crosses[rhs].y - crosses[rhs].x * crosses[lhs].y; if (diff != 0) return (diff > 0); if (abs(crosses[lhs].x - crosses[rhs].x) < eps) return abs(crosses[lhs].y) < abs(crosses[rhs].y); return abs(crosses[lhs].x) < abs(crosses[rhs].x); }); res.resize(0); res.pb(kol[0]); for (i = 1; i < n; i++) { while (res.size() > 1 && ccwD(crosses[res[res.size() - 2]], crosses[res[res.size() - 1]], crosses[kol[i]]) <= 0) res.pop_back(); res.pb(kol[i]); } for (i = 0; i < n; i++) { crosses[i].x += dx; crosses[i].y += dy; } } double getArea() { int i, j; double result = 0.0; for (i=0;i < res.size();i++) { j = (i + 1) % res.size(); result += crosses[res[i]].x * crosses[res[j]].y - crosses[res[j]].x * crosses[res[i]].y; } return result / 2; } bool onEdge(double xx, double yy, line& l1, line& l2) { pD p1, p2, p3; p3.x = xx; p3.y = yy; for (int i=0;i<vecI.size();i++) if (vecI[i] != 0) { p1.x = vec[i].a1.x; p1.y = vec[i].a1.y; p2.x = vec[i].a2.x; p2.y = vec[i].a2.y; int w = ccwD(p1, p2, p3); if (w == vecI[i]) return false; } return true; } int main() { srand(time(0)); scanf("%d", &n); vec.clear(); vecI.clear(); crosses.clear(); for (i=0;i<n;i++) scanf("%d %d %d %d", &p1[i].x, &p1[i].y, &p2[i].x, &p2[i].y); for (i=0;i<n;i++) for (j=i+1;j<n;j++) check(i, j); for (i=0;i<vec.size();i++) for (j=i+1;j<vec.size();j++) { if (get_line_intersection(vec[i].a1.x, vec[i].a1.y, vec[i].a2.x, vec[i].a2.y, vec[j].a1.x, vec[j].a1.y, vec[j].a2.x, vec[j].a2.y, xx, yy)) { bool already = false; for (pD& pd : crosses) if (abs(pd.x - xx) < eps && abs(pd.y - yy) < eps) { already = true; break; } if (!already && onEdge(xx, yy, vec[i], vec[j])) { pD p; p.x = xx; p.y = yy; crosses.pb(p); } } } if (crosses.size() < 3) { printf("0.000000000\n"); return 0; } res.clear(); convexHull(res); if (res.size() < 3) { printf("0.000000000\n"); return 0; } printf("%.16lf\n", getArea()); return 0; }
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 vector<int> VI; typedef pair <int,int> ii; typedef pair <double,double> dd; typedef pair <ii,int> iii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 105; const double eps = 1e-9; struct pI { int x, y; }; struct pD { double x, y; }; struct line { pI a1; pI a2; }; int detI(pI& a, pI& b, pI& c) { return a.x * b.y + b.x * c.y + c.x * a.y - a.x * c.y - b.x * a.y - c.x * b.y; } double detD(pD& a, pD& b, pD& c) { return a.x * b.y + b.x * c.y + c.x * a.y - a.x * c.y - b.x * a.y - c.x * b.y; } // funkcja sprawdza czy przechodzac z a do b i c skrecamy w lewo(1), w prawo(-1) czy punkty są wspollniowe(0) int ccwI(pI& a, pI& b, pI& c) { int d = detI(a, b, c); return (d > 0) ? 1 : ((d < 0) ? -1 : 0); } // funkcja sprawdza czy przechodząc z a do b i c skręcamy w lewo(1), w prawo(-1) czy punkty są współlniowe(0) int ccwD(pD& a, pD& b, pD& c) { double d = detD(a, b, c); return (d > eps) ? 1 : ((d < -eps) ? -1 : 0); } // funkcja sprawdza położenie punktu c wspolliniowego z odcinkiem a-b // zwraca 1 - c lezy na odcinku, 0 - nie bool inside(pI& a, pI& b, pI& c) { return (c.x <= max(a.x,b.x) && c.x >= min(a.x,b.x) && c.y <= max(a.y,b.y) && c.y >= min(a.y,b.y)); } char get_line_intersection(int p0_x, int p0_y, int p1_x, int p1_y, int p2_x, int p2_y, int p3_x, int p3_y, double& i_x, double& i_y) { int s1_x, s1_y, s2_x, s2_y; s1_x = p1_x - p0_x; s1_y = p1_y - p0_y; s2_x = p3_x - p2_x; s2_y = p3_y - p2_y; double s, t; s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) * 1.0 / (-s2_x * s1_y + s1_x * s2_y); t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) * 1.0 / (-s2_x * s1_y + s1_x * s2_y); if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { // Collision detected i_x = p0_x + (t * s1_x); i_y = p0_y + (t * s1_y); return 1; } return 0; // No collision } pI p1[N], p2[N]; int i, j, n; double xx, yy; vector<line> vec; vector<int> vecI; vector<pD> crosses; VI res; void check2(pI& p_1, pI& p_2, pI& p_3, pI& p_4, int a, int b) { int w1 = ccwI(p_1, p_2, p_3); int w2 = ccwI(p_1, p_2, p_4); if (w1 * w2 == -1) return; if (w1 == 0 && inside(p_1, p_2, p_3)) return; if (w2 == 0 && inside(p_1, p_2, p_4)) return; for (int i=0;i<n;i++) if (i != a && i != b) { int w3 = ccwI(p_1, p_2, p1[i]); int w4 = ccwI(p_1, p_2, p2[i]); if (w3 * w4 == 1) { if (w1 == w3 || w2 == w3) return; } } line l; l.a1 = p_1; l.a2 = p_2; vec.pb(l); vecI.pb(w1 != 0 ? w1 : w2); } void check(int a, int b) { check2(p1[a], p1[b], p2[a], p2[b], a, b); check2(p1[a], p2[b], p2[a], p1[b], a, b); check2(p2[a], p1[b], p1[a], p2[b], a, b); check2(p2[a], p2[b], p1[a], p1[b], a, b); } void convexHull(VI& res) { int n = crosses.size(); int ind = 0, i, kol[n]; double dx, dy; for (i = 1; i < n; i++) if (dd(crosses[i].y, crosses[i].x) < dd(crosses[ind].y, crosses[ind].x)) ind = i; dx = crosses[ind].x; dy = crosses[ind].y; for (i = 0; i < n; i++) { crosses[i].x -= dx; crosses[i].y -= dy; kol[i] = i; } sort(kol, kol + n, [] (const int& lhs, const int& rhs) { double diff = crosses[lhs].x * crosses[rhs].y - crosses[rhs].x * crosses[lhs].y; if (diff != 0) return (diff > 0); if (abs(crosses[lhs].x - crosses[rhs].x) < eps) return abs(crosses[lhs].y) < abs(crosses[rhs].y); return abs(crosses[lhs].x) < abs(crosses[rhs].x); }); res.resize(0); res.pb(kol[0]); for (i = 1; i < n; i++) { while (res.size() > 1 && ccwD(crosses[res[res.size() - 2]], crosses[res[res.size() - 1]], crosses[kol[i]]) <= 0) res.pop_back(); res.pb(kol[i]); } for (i = 0; i < n; i++) { crosses[i].x += dx; crosses[i].y += dy; } } double getArea() { int i, j; double result = 0.0; for (i=0;i < res.size();i++) { j = (i + 1) % res.size(); result += crosses[res[i]].x * crosses[res[j]].y - crosses[res[j]].x * crosses[res[i]].y; } return result / 2; } bool onEdge(double xx, double yy, line& l1, line& l2) { pD p1, p2, p3; p3.x = xx; p3.y = yy; for (int i=0;i<vecI.size();i++) if (vecI[i] != 0) { p1.x = vec[i].a1.x; p1.y = vec[i].a1.y; p2.x = vec[i].a2.x; p2.y = vec[i].a2.y; int w = ccwD(p1, p2, p3); if (w == vecI[i]) return false; } return true; } int main() { srand(time(0)); scanf("%d", &n); vec.clear(); vecI.clear(); crosses.clear(); for (i=0;i<n;i++) scanf("%d %d %d %d", &p1[i].x, &p1[i].y, &p2[i].x, &p2[i].y); for (i=0;i<n;i++) for (j=i+1;j<n;j++) check(i, j); for (i=0;i<vec.size();i++) for (j=i+1;j<vec.size();j++) { if (get_line_intersection(vec[i].a1.x, vec[i].a1.y, vec[i].a2.x, vec[i].a2.y, vec[j].a1.x, vec[j].a1.y, vec[j].a2.x, vec[j].a2.y, xx, yy)) { bool already = false; for (pD& pd : crosses) if (abs(pd.x - xx) < eps && abs(pd.y - yy) < eps) { already = true; break; } if (!already && onEdge(xx, yy, vec[i], vec[j])) { pD p; p.x = xx; p.y = yy; crosses.pb(p); } } } if (crosses.size() < 3) { printf("0.000000000\n"); return 0; } res.clear(); convexHull(res); if (res.size() < 3) { printf("0.000000000\n"); return 0; } printf("%.16lf\n", getArea()); return 0; } |