题意:给出30个点,判断这些点组成凸四边形的数量;
思路:对于a,b,c,d,如果组成的是 凹四边形,他们的组成的三角形的面积就会存在S abc = S abd + S acd + S bcd 的情况,所以直接判断是否会出现这种情况就行。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 100 + 10;
#define INF 0x3f3f3f3f
int n;
struct Node
{
double x,y;
}a[maxn];
double distances(Node x,Node y)
{
return sqrt((x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y));
}
double area(Node x,Node y,Node z)//叉积求三角形面积
{
return fabs( (z.x - y.x) * (x.y - y.y) - (x.x - y.x) * (z.y - y.y) );
}
double b[maxn];
bool is_(Node t1,Node t2,Node t3,Node t4)
{
b[0] = area(t1,t2,t3);b[1] = area(t1,t2,t4);b[2] = area(t1,t3,t4);b[3] = area(t2,t3,t4);
sort(b,b + 4);
if(b[3] == b[0] + b[1] + b[2])
return false;
return true;
}
int main()
{
// freopen("ins.text","r",stdin);
int Tcase;
scanf("%d",&Tcase);
for(int ii = 1; ii <= Tcase; ii ++)
{
scanf("%d",&n);
for(int i = 1; i <= n; i ++)
scanf("%lf%lf",&a[i].x,&a[i].y);
int ans = 0;
for(int i = 1; i <= n - 2; i ++)
{
for(int j = i + 1; j <= n - 1; j ++)
{
for(int x = j + 1; x <= n; x ++)
{
for(int y = x + 1; y <= n; y ++)
{
if(is_(a[i],a[j],a[x],a[y]))
ans ++;
}
}
}
}
printf("Case %d: ",ii);
cout << ans << endl;
}
return 0;
}