-
Notifications
You must be signed in to change notification settings - Fork 0
/
areas.cpp
186 lines (149 loc) · 4.72 KB
/
areas.cpp
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
#include <bits/stdc++.h>
using namespace std;
struct Point {
long double x, y;
Point operator-(const Point &p) const { return {x - p.x, y - p.y}; }
Point operator+(const Point &p) const { return {x + p.x, y + p.y}; }
Point operator*(const long double d) const { return {x * d, y * d}; }
long double operator*(const Point &p) const { return x * p.x + y * p.y; }
long double operator^(const Point &p) const { return x * p.y - y * p.x; }
};
struct Fraction {
long long a, b;
Fraction(long long _a = 0, long long _b = 1) : a(_a), b(_b) {
if (b < 0) {
a = -a, b = -b;
}
}
Fraction operator*(long long t) const {
return {a * t, b};
}
Fraction operator+(const Fraction &f) const {
return {a + f.a, b};
}
Fraction operator+(long long t) const {
return {a + t * b, b};
}
};
bool zin(const Point &a, const Point &c) {
return (a.x - c.x) < c.y;
}
struct Plane {
long long a, b, c;
long double da, db, dc, dd;
Plane(long long a = 0, long long b = 0, long long c = 0) : a(a), b(b), c(c) {
dd = hypotl(a, b);
da = a / dd;
db = b / dd;
dc = c / dd;
}
int half() const {
return b > 0 || (b == 0 && a > 0) ? 1 : 2;
}
bool operator<(const Plane &p) const {
if (half() != p.half()) {
return half() < p.half();
}
long long cp = a * p.b - b * p.a;
if (cp != 0) {
return cp > 0;
}
return dc < p.dc;
}
bool operator==(const Plane &p) const {
return a == p.a && b == p.b && c == p.c;
}
bool is_cross(const Plane &p1, const Plane &p2) const {
Fraction x(p2.c * p1.b - p1.c * p2.b, p1.a * p2.b - p2.a * p1.b);
Fraction y(p2.c * p1.a - p1.c * p2.a, p2.a * p1.b - p1.a * p2.b);
Fraction res = x * a + y * b + c;
return res.a > 0;
}
Point operator^(const Plane &p) const {
return {(p.dc * db - dc * p.db) / (da * p.db - p.da * db),
(p.dc * da - dc * p.da) / (p.da * db - da * p.db)};
}
};
bool IsNOTPerpendicular(const Point &a, const Point &b, const Point &c) {
double yDelta_a = b.y - a.y;
double xDelta_a = b.x - a.x;
double yDelta_b = c.y - b.y;
double xDelta_b = c.x - b.x;
if (fabs(xDelta_a) <= 0 && fabs(yDelta_b) <= 0) {
return true;
} else if (fabs(yDelta_a) <= 0) {
return false;
} else if (fabs(yDelta_b) <= 0) {
return false;
} else if (fabs(xDelta_a) <= 0) {
return false;
} else if (fabs(xDelta_b) <= 0) {
return false;
} else true;
}
bool in(const Point &a, const Point &c) {
return (a.x - c.x) < c.y;
}
bool equal_planes(const Plane &p1, const Plane &p2) {
long long dp = p1.a * p2.a + p1.b * p2.b;
long long cp = p1.a * p2.b - p1.b * p2.a;
return cp == 0 && dp > 0;
}
const long long C = 1000000000;
const int N = 1000000;
vector<Plane> planes;
vector<Plane> borders;
Plane inter[N];
vector<Point> polygon;
int main() {
borders.push_back(Plane(1, 0, C));
borders.push_back(Plane(-1, 0, C));
borders.push_back(Plane(0, 1, C));
borders.push_back(Plane(0, -1, C));
int n;
cin >> n;
for (int i = 0; i < n; i++) {
long long a, b, c;
cin >> a >> b >> c;
planes.push_back(Plane(a, b, c));
}
planes.insert(planes.end(), borders.begin(), borders.end());
sort(planes.begin(), planes.end());
n = unique(planes.begin(), planes.end(), equal_planes) - planes.begin();
planes.resize(n);
int l = 0, r = 0;
for (auto &p : planes) {
while (r - l > 1 && !p.is_cross(inter[r - 1], inter[r - 2])) {
--r;
}
while (r - l > 1 && !p.is_cross(inter[l], inter[l + 1])) {
++l;
}
if (r - l > 0 && inter[r - 1].a * p.b - inter[r - 1].b * p.a <= 0) {
cout << "0";
return 0;
}
if (r - l < 2 || inter[l].is_cross(p, inter[r - 1]))
inter[r++] = p;
}
inter[r] = inter[l];
for (int i = l; i < r; i++) {
polygon.push_back(inter[i] ^ inter[i + 1]);
}
for (int i = 0; i < polygon.size(); i++) {
if (fabsl(polygon[i].x) > C / 2 || fabsl(polygon[i].y) > C / 2) {
cout << "-1";
return 0;
}
}
int m = polygon.size();
polygon.push_back(polygon[0]);
long double S = 0;
for (int i = 0; i < m; ++i) {
S += polygon[i] ^ polygon[i + 1];
}
S = fabsl(S) / 2.0;
cout.precision(20);
cout << fixed << S;
return 0;
}