您的位置:首页 > 其它

FZU 2148(计算几何)叉积模板

2014-05-10 15:55 316 查看
题目链接:点击打开链接

题目分析:凸四边形个数判断,枚举法。

Sacb+Sabd+Sacd=Sbcd;

则为凹多边形。

题目总结:i,j,k,l一多,j++写成i++ wa了好几炮

此题可为为模板了

#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
const double eps = 1e-8;
struct point{
  int x, y;
   point (int x=0, int y=0):x(x),y(y){};
};
point data[50];
typedef point  vector;
vector operator + (vector A, vector B){ return vector(A.x+B.x, A.y+B.y); }
vector operator - (vector A, vector B){ return vector(A.x-B.x, A.y-B.y); }
vector operator * (vector A, double p){ return vector(A.x*p, A.y*p); }
vector operator / (vector A, double p){ return vector(A.x/p, A.y/p); }
bool operator < (const point &a, const point &b){
   return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
int dcmp( double x){
   if(fabs(x)<eps) return 0;
   else return x<0 ?-1 :1;
}
bool operator == (const point&a, const point&b){
   return dcmp(a.x-b.x)==0 &&dcmp(a.y-b.y)==0;
}
double cross(vector a, vector b) { return a.x*b.y- a.y*b.x; }
double area(point a, point b, point c) {return cross(b-a, c-a);}

bool solve(point a, point b, point c, point d){
   double s1,s2,s3,s4;
   s1 = fabs(area(a,b,c));
   s2 = fabs(area(a,b,d));
   s3 = fabs(area(b,c,d));
   s4 = fabs(area(a,c,d));
   if((s1+s2+s3-s4==0)||(s1+s2+s4-s3==0)||(s1+s4+s3-s2==0)||(s4+s2+s3-s1==0))
   return false;
   return true;
}
int main()
{
     int t,tt=1, n, ans, i, j, k, l;
     cin>>t;
     while(t--){
        ans=0;
        cin>>n;
        for (i=1; i<=n; i++)
            scanf("%d%d",&data[i].x, &data[i].y);
        for(i=1; i<=n; i++)
           for(j=i+1; j<=n; j++)
              for(k=j+1; k<=n; k++)
                 for(l=k+1; l<=n; l++)
                    if(solve(data[i], data[j], data[k], data[l]))
                       ans++;
        printf("Case %d: %d\n",tt++, ans);
     }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: