题意
给出 N N N点,其中第 i i i个点连向第 i + 1 i+1 i+1条边,计算其组成的多边形的面积,如果不能组成多边形,输出 I m p o s s i b l e Impossible Impossible。
思路
判断不能组成多边形的情况,只用判断两条线是否相交即可。
面积我们可以利用叉积的求法,每次取出连续的三个点计算面积,答案为它们的和的绝对值。
代码
#include<cmath>
#include<cstdio>
struct P{
double x, y;
}p[1001];
double cp(P a, P b, P c) {
return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);
}
int across(P a, P b, P c, P d) {
return (cp(a, b, c) * cp(a, b, d) < 0 && cp(c, d, a) * cp(c, d, b) < 0);
}
int main() {
int n;
scanf("%d", &n);
if (n < 3) {
printf("Impossible");
return 0;
}
for (int i = 1; i <= n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y);
for (int i = 1; i < n; i++)
for (int j = 1; j < n; j++) {
if (i != j && j != i + 1 && across(p[i], p[i + 1], p[j], p[j + 1])) {
printf("Impossible");
return 0;
}
}
double ans = 0;
for (int i = 2; i < n; i++)
ans += 0.5 * cp(p[1], p[i], p[i + 1]);
printf("%.2lf", std::abs(ans));
}