题意
给出NNN点,其中第iii个点连向第i+1i+1i+1条边,计算其组成的多边形的面积,如果不能组成多边形,输出ImpossibleImpossibleImpossible。
思路
判断不能组成多边形的情况,只用判断两条线是否相交即可。
面积我们可以利用叉积的求法,每次取出连续的三个点计算面积,答案为它们的和的绝对值。
代码
#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));
}
本文介绍了一种计算由一系列点构成的多边形面积的方法,通过判断线段是否相交来验证多边形的合法性。使用叉积计算连续三点形成的面积,并通过两线段相交的判断避免非法多边形的计算。
1629

被折叠的 条评论
为什么被折叠?



