-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2104-lasers.cpp
More file actions
95 lines (75 loc) · 2.04 KB
/
2104-lasers.cpp
File metadata and controls
95 lines (75 loc) · 2.04 KB
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
#include <bits/stdc++.h>
using namespace std;
struct Point {
double x, y, z;
Point(double x, double y, double z) : x(x), y(y), z(z) {}
Point() {}
Point operator+(const Point& p) { return Point(x + p.x, y + p.y, z + p.z); }
Point operator-(const Point& p) { return Point(x - p.x, y - p.y, z - p.z); }
bool operator<(Point p) {
if (y == p.y) return x < p.x;
return y < p.y;
}
Point scale(double factor) {
return Point(x * factor, y * factor, z * factor);
}
};
int N;
double cross(Point& O, Point& A, Point& B) {
return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
}
vector<Point> convex_hull(vector<Point>& A) {
int n = A.size(), k = 0;
if (n <= 3) return A;
vector<Point> ans(2 * n);
sort(A.begin(), A.end());
for (int i = 0; i < n; ++i) {
while (k >= 2 && cross(ans[k - 2], ans[k - 1], A[i]) <= 0) k--;
ans[k++] = A[i];
}
for (int i = n - 1, t = k + 1; i > 0; --i) {
while (k >= t && cross(ans[k - 2], ans[k - 1], A[i - 1]) <= 0) k--;
ans[k++] = A[i - 1];
}
ans.resize(k - 1);
return ans;
}
long double convex_hull_area(vector<Point> pts) {
vector<Point> hull = convex_hull(pts);
long double total = 0;
for (int i = 2; i < (int)hull.size(); i++)
total += fabs(cross(hull[0], hull[i - 1], hull[i])) / 2L;
return total;
}
Point intersection_soil(Point alien, Point platform) {
double left = 0;
double right = 100000;
int iters = 500;
Point laser = platform - alien;
Point ret;
while (iters--) {
double mid = (left + right) / 2.0;
ret = laser.scale(mid) + alien;
if (ret.z > 0)
left = mid;
else
right = mid;
}
return ret;
}
void solve() {
Point alien;
vector<Point> platforms;
cin >> alien.x >> alien.y >> alien.z;
for (int i = 0; i < N; i++) {
Point p;
cin >> p.x >> p.y >> p.z;
platforms.push_back(p);
}
vector<Point> soil;
for (Point p : platforms) soil.push_back(intersection_soil(alien, p));
printf("%.2Lf\n", convex_hull_area(soil));
}
int main() {
while (scanf("%d", &N) == 1 && N > 0) solve();
}