【計算幾何】Triangles HUST 1607

CN_swords發表於2017-05-08
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<map>
#include<queue>
#include<cmath>
#include<algorithm>
#include<deque>
typedef long long LL;
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int INF=0x3f3f3f3f;
const int N = 200+10;
//
struct point
{
    double x,y;
} poi[N];
int num;
typedef pair<double,double>PII;
PII temp[220];
map<PII,int>mp;
int tot;
int ma[N][N];

int GetId(PII s)
{
    if(mp.find(s)!=mp.end())
        return mp[s];
    mp[s]=++tot;
    return tot;
}

struct V   //兩點式
{
    point start;
    point end;
};
V change(V a)   //兩點式起點化為 0,0
{
    V b;
    b.start.x = 0;
    b.start.y = 0;
    b.end.x = a.end.x - a.start.x;
    b.end.y = a.end.y - a.start.y;
    return b;
}
double cross_mul(V a,V b)
{
    double result;
    V aa = change(a);
    V bb = change(b);
    result = aa.end.x*bb.end.y - aa.end.y*bb.end.x;
    return result;
}
const double eps=1e-6;
bool Judge(point a,point b,point c)
{
    V aa,bb;
    aa.start = a;
    aa.end = b;
    bb.start = a;
    bb.end = c;
    double ans = cross_mul(aa,bb);
    if(fabs(ans)<eps) return false;
    return true;
}
bool pan(point a,point b,point c)
{
    if((a.x - b.x) * (c.x - b.x) < 0 && (a.y - b.y) * (c.y - b.y) < 0)
        return true;
    return false;
}

int main()
{
    PII temp;
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        mp.clear();
        tot=0;
        num=0;
        for(int i = 1; i <= n; i++)
        {
            cin>>temp.first>>temp.second;
            int id = GetId(temp);
            poi[id].x=temp.first;
            poi[id].y=temp.second;
        }
//        printf("%d\n",tot);

        PII x,y;
        memset(ma,0,sizeof(ma));
        for(int i=0; i<m; ++i)
        {
            cin>>x.first>>x.second>>y.first>>y.second;
            int u=GetId(x);
            int v=GetId(y);
            ma[u][v]=ma[v][u]=1;
        }
        for(int k=1; k<=tot; k++)
            for(int i=1; i<=tot; i++)
                for(int j=1; j<=tot; j++)
                {
                    if(!Judge(poi[i],poi[j],poi[k])&&ma[i][k])
                    {
                        //printf("%d %d %d\n",i,k,j);
                        if(pan(poi[i],poi[j],poi[k]))
                        {
                            //puts("??");
                            ma[i][j]=ma[j][i]=ma[j][k]=ma[k][j]=1;
                        }
                        else if(ma[k][j])
                        {
                            //puts("**");
                            ma[i][j]=ma[j][i]=1;
                        }
                    }
                }
//        for(int i = 1; i <= tot; i++)
//        {
//            for(int j = 1; j <= tot; j++)
//                printf("ma[%d][%d] = %d  ",i,j,ma[i][j]);
//            puts("");
//        }
        int ans=0;
        for(int i=1; i<=n; i++)
            for(int j=i+1; j<=n; j++)
                for(int k=j+1; k<=n; k++)
                    if(ma[i][j] && ma[j][k] && ma[i][k] && Judge(poi[i],poi[j],poi[k]))
                    {
                        //printf("%d %d %d\n",i,j,k);
                        ans++;
                    }
        printf("%d\n",ans);
    }
    return 0;
}

相關文章