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
| const double MAX_NUM = 999999999;
class Solution { public: double dist(vector<int> p1, vector<int> p2) { return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); } double cal(vector<int> p1, vector<int> p2, vector<int> p3) { return sqrt(dist(p1, p2) * dist(p1, p3)); } bool isValid(vector<int> p1, vector<int> p2, vector<int> p3) { if (abs(dist(p1, p2) + dist(p1, p3) - dist(p2, p3)) < 0.001) return true; return false; } double minAreaFreeRect(vector<vector<int>>& points) { if (points.size() < 4) return 0; double ans = MAX_NUM; int N = points.size(); set<pair<int, int>> hmap; for (int i = 0; i < N; i++) hmap.insert({points[i][0], points[i][1]}); for (int i1 = 0; i1 < N; i1++) for (int i2 = i1 + 1; i2 < N; i2++) for (int i3 = i2 + 1; i3 < N; i3++) { if (isValid(points[i1], points[i2], points[i3])) { int x = points[i2][0] + points[i3][0] - points[i1][0]; int y = points[i2][1] + points[i3][1] - points[i1][1]; if (hmap.find({x, y}) != hmap.end()) { ans = min(ans, cal(points[i1], points[i2], points[i3])); } } if (isValid(points[i2], points[i1], points[i3])) { int x = points[i1][0] + points[i3][0] - points[i2][0]; int y = points[i1][1] + points[i3][1] - points[i2][1]; if (hmap.find({x, y}) != hmap.end()) { ans = min(ans, cal(points[i2], points[i1], points[i3])); } } if (isValid(points[i3], points[i1], points[i2])) { int x = points[i1][0] + points[i2][0] - points[i3][0]; int y = points[i1][1] + points[i2][1] - points[i3][1]; if (hmap.find({x, y}) != hmap.end()) { ans = min(ans, cal(points[i3], points[i1], points[i2])); } } } if (abs(ans - MAX_NUM) < 0.001) return 0; else return ans; } };
|