#include <iostream> #include <iomanip> #include <algorithm> #include <vector> #include <string> #include <unordered_map> #include <unordered_set> #include <set> #include <math.h> using namespace std; struct point { double x; double y; bool operator== (const point & rhs) const{ return rhs.x == x and rhs.y == y; } bool operator!= (const point & rhs) const{ return ! (rhs == *this); } }; struct tpoint { int mage; point loc; }; struct mage { int numb; point t1; point t2; }; struct anglesort { bool operator()(const pair<double,int>& t1, const pair<double,int>& t2) const { return t1.first < t2.first; } }; struct pointsort { bool operator()(const tpoint& t1, const tpoint& t2) const { if(t1.loc.y == t2.loc.y){ return t1.loc.x < t2.loc.x; } return t1.loc.y > t2.loc.y; } }; double calcarea(const vector<point> & points){ point avarage; for(const auto p : points){ avarage.x += p.x; avarage.y += p.y; } avarage.x /= points.size(); avarage.y /= points.size(); double area = 0; for(int i = 0; i < points.size() - 1; i ++){ double x1 = points[i].x, y1 = points[i].y, x2 = points[(i + 1)].x, y2 = points[(i + 1)].y; double areainc =( avarage.x * (y1 - y2) + x1 * (y2 - avarage.y) + x2 * (avarage.y - y1)) / 2.0; area += abs(areainc); } return abs(area); } double getangle(point orig, point toget){ double angle =atan2(toget.x - orig.x, toget.y - orig.y); while(angle < 0){ angle += 2 * M_PI; } return angle; } vector<pair<double, int>> calcangles(const vector<tpoint> & points, point cur, double sangle, int index = -1){ vector<pair<double, int>> tbyangle; for(int i = 0; i < points.size(); i++){ if(points[i].mage != index){ double angle = getangle(cur, points[i].loc); angle -= sangle; while(angle < 0 ){ angle += 2 * M_PI; } tbyangle.push_back({angle, points[i].mage}); } } return tbyangle; } pair<bool, point> intersect(point a, point b, point c, point d){ double x1 = a.x, x2 = b.x, x3 = c.x, x4 = d.x; double y1 = a.y, y2 = b.y, y3 = c.y, y4 = d.y; double denom = (x4 - x3) * (y1 - y2) - (x1 - x2) * (y4 - y3); if(denom == 0.0){ return {false, {0.0, 0.0}}; } double nom1 = (y3 - y4) * (x1 - x3) + (x4 - x3) * (y1 - y3); double frac1 = nom1 / denom; double nom2 = (y1 - y2) * (x1 - x3) + (x2 - x1) * (y1 - y3); double frac2 = nom2 / denom; if(frac1 >= 1.0 or frac1 <= 0.0 or frac2 >= 1.0 or frac2 <= 0.0){ return {false, {0.0, 0.0}}; } return {true, {x1 + frac1 * (x2 - x1), y1 + frac1 * (y2 - y1)}}; } int main (){ cout << fixed << setprecision(9); int n; cin >> n; vector<mage> mages; vector<tpoint> tpoints; unordered_map<int, point> tbynumb; for(int i = 0; i < n; i ++){ mage cur; cur.numb = i; cin >> cur.t1.x >> cur.t1.y >> cur.t2.x >> cur.t2.y; mages.push_back(cur); tpoints.push_back({2 * i, cur.t1}); tpoints.push_back({2 * i + 1, cur.t2}); tbynumb[2 * i] = cur.t1; tbynumb[2 * i + 1] = cur.t2; } sort(tpoints.begin(), tpoints.end(), pointsort()); int curpoint = tpoints[0].mage; { unordered_set<int> seen; for(auto cur : tpoints){ if(seen.find(cur.mage / 2) != seen.end()){ curpoint = cur.mage; break; } seen.insert(cur.mage / 2); } } double direction = 0.0; vector<point> shape; shape.push_back(tbynumb[curpoint]); vector<int> candidates = {}; while(shape.size() == 1 or shape[0] != shape[shape.size() - 1]) { auto angles = calcangles(tpoints, tbynumb[curpoint], direction, curpoint); sort(angles.begin(), angles.end(), anglesort()); unordered_set<int> seen; for (auto a : angles){ int curmagenumber = a.second / 2; if(seen.find(curmagenumber) != seen.end()){ if(shape.size() > 1){ int rest = curpoint % 2; rest = 1 - rest; // swap it! auto othertower = tbynumb[(curpoint/ 2) * 2 + rest]; auto intersection = intersect(othertower, tbynumb[a.second], shape[shape.size() - 2], tbynumb[curpoint]); if(intersection.first){ shape[shape.size() - 1] = intersection.second; } } shape.push_back(tbynumb[a.second]); if(shape.size() > 2){ auto intersection = intersect(shape[0], shape[1], shape[shape.size() -1], shape[shape.size() - 2]); if(intersection.first){ shape[0] = intersection.second; shape[shape.size() - 1] = intersection.second; } } curpoint = a.second; direction += a.first; break; } seen.insert(a.second / 2); } set<pair<double,double>> seenp; for(int j = 0; j < shape.size() - 2; j ++){ auto a = shape[j]; if(seenp.find({a.x, a.y}) != seenp.end()){ int first = 0, second = 0; for(int i = 0; i < shape.size(); i ++){ if(shape[i].x == a.x and shape[i].y == a.y){ first = second; second = i; } } vector<point> newshape = {shape.begin() + first, shape.begin() + second + 1}; cout << calcarea(newshape) << endl; return 0; } seenp.insert({a.x, a.y}); } } cout << calcarea(shape) << endl; }
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 | #include <iostream> #include <iomanip> #include <algorithm> #include <vector> #include <string> #include <unordered_map> #include <unordered_set> #include <set> #include <math.h> using namespace std; struct point { double x; double y; bool operator== (const point & rhs) const{ return rhs.x == x and rhs.y == y; } bool operator!= (const point & rhs) const{ return ! (rhs == *this); } }; struct tpoint { int mage; point loc; }; struct mage { int numb; point t1; point t2; }; struct anglesort { bool operator()(const pair<double,int>& t1, const pair<double,int>& t2) const { return t1.first < t2.first; } }; struct pointsort { bool operator()(const tpoint& t1, const tpoint& t2) const { if(t1.loc.y == t2.loc.y){ return t1.loc.x < t2.loc.x; } return t1.loc.y > t2.loc.y; } }; double calcarea(const vector<point> & points){ point avarage; for(const auto p : points){ avarage.x += p.x; avarage.y += p.y; } avarage.x /= points.size(); avarage.y /= points.size(); double area = 0; for(int i = 0; i < points.size() - 1; i ++){ double x1 = points[i].x, y1 = points[i].y, x2 = points[(i + 1)].x, y2 = points[(i + 1)].y; double areainc =( avarage.x * (y1 - y2) + x1 * (y2 - avarage.y) + x2 * (avarage.y - y1)) / 2.0; area += abs(areainc); } return abs(area); } double getangle(point orig, point toget){ double angle =atan2(toget.x - orig.x, toget.y - orig.y); while(angle < 0){ angle += 2 * M_PI; } return angle; } vector<pair<double, int>> calcangles(const vector<tpoint> & points, point cur, double sangle, int index = -1){ vector<pair<double, int>> tbyangle; for(int i = 0; i < points.size(); i++){ if(points[i].mage != index){ double angle = getangle(cur, points[i].loc); angle -= sangle; while(angle < 0 ){ angle += 2 * M_PI; } tbyangle.push_back({angle, points[i].mage}); } } return tbyangle; } pair<bool, point> intersect(point a, point b, point c, point d){ double x1 = a.x, x2 = b.x, x3 = c.x, x4 = d.x; double y1 = a.y, y2 = b.y, y3 = c.y, y4 = d.y; double denom = (x4 - x3) * (y1 - y2) - (x1 - x2) * (y4 - y3); if(denom == 0.0){ return {false, {0.0, 0.0}}; } double nom1 = (y3 - y4) * (x1 - x3) + (x4 - x3) * (y1 - y3); double frac1 = nom1 / denom; double nom2 = (y1 - y2) * (x1 - x3) + (x2 - x1) * (y1 - y3); double frac2 = nom2 / denom; if(frac1 >= 1.0 or frac1 <= 0.0 or frac2 >= 1.0 or frac2 <= 0.0){ return {false, {0.0, 0.0}}; } return {true, {x1 + frac1 * (x2 - x1), y1 + frac1 * (y2 - y1)}}; } int main (){ cout << fixed << setprecision(9); int n; cin >> n; vector<mage> mages; vector<tpoint> tpoints; unordered_map<int, point> tbynumb; for(int i = 0; i < n; i ++){ mage cur; cur.numb = i; cin >> cur.t1.x >> cur.t1.y >> cur.t2.x >> cur.t2.y; mages.push_back(cur); tpoints.push_back({2 * i, cur.t1}); tpoints.push_back({2 * i + 1, cur.t2}); tbynumb[2 * i] = cur.t1; tbynumb[2 * i + 1] = cur.t2; } sort(tpoints.begin(), tpoints.end(), pointsort()); int curpoint = tpoints[0].mage; { unordered_set<int> seen; for(auto cur : tpoints){ if(seen.find(cur.mage / 2) != seen.end()){ curpoint = cur.mage; break; } seen.insert(cur.mage / 2); } } double direction = 0.0; vector<point> shape; shape.push_back(tbynumb[curpoint]); vector<int> candidates = {}; while(shape.size() == 1 or shape[0] != shape[shape.size() - 1]) { auto angles = calcangles(tpoints, tbynumb[curpoint], direction, curpoint); sort(angles.begin(), angles.end(), anglesort()); unordered_set<int> seen; for (auto a : angles){ int curmagenumber = a.second / 2; if(seen.find(curmagenumber) != seen.end()){ if(shape.size() > 1){ int rest = curpoint % 2; rest = 1 - rest; // swap it! auto othertower = tbynumb[(curpoint/ 2) * 2 + rest]; auto intersection = intersect(othertower, tbynumb[a.second], shape[shape.size() - 2], tbynumb[curpoint]); if(intersection.first){ shape[shape.size() - 1] = intersection.second; } } shape.push_back(tbynumb[a.second]); if(shape.size() > 2){ auto intersection = intersect(shape[0], shape[1], shape[shape.size() -1], shape[shape.size() - 2]); if(intersection.first){ shape[0] = intersection.second; shape[shape.size() - 1] = intersection.second; } } curpoint = a.second; direction += a.first; break; } seen.insert(a.second / 2); } set<pair<double,double>> seenp; for(int j = 0; j < shape.size() - 2; j ++){ auto a = shape[j]; if(seenp.find({a.x, a.y}) != seenp.end()){ int first = 0, second = 0; for(int i = 0; i < shape.size(); i ++){ if(shape[i].x == a.x and shape[i].y == a.y){ first = second; second = i; } } vector<point> newshape = {shape.begin() + first, shape.begin() + second + 1}; cout << calcarea(newshape) << endl; return 0; } seenp.insert({a.x, a.y}); } } cout << calcarea(shape) << endl; } |