半平面交

1.处理不等式

2.处理平面直角坐标系问题

3.如何判断多边形

#include <bits/stdc++.h>
#define M 50002 
#define eps 1e-5
using namespace std;
int n,m,cnt,cnt1;
struct edge
{
    double x;
    double y;
}e[M],e1[M];
struct line
{
    edge x;
    edge y;
    double d;    
}l[M],q[M];
int dcmp(double x)
{
    if(fabs(x) < eps)return 0;
    return x < 0 ? -1:1;
}
edge operator + (edge a,edge b)
{
    return (edge){a.x + b.x,a.y + b.y};
 } 
edge operator - (edge a,edge b)
{
    return (edge){a.x - b.x,a.y - b.y};
}
edge operator * (edge a,double b)
{
    return (edge){a.x * b,a.y * b};
}
double operator * (edge a,edge b)
{
    return a.x * b.y - a.y * b.x;
}
edge maru(line a,line b)
{
    edge u = a.x - b.x;
    edge v = a.y - a.x;
    edge w = b.y - b.x;
    double t = (w * u) / (v * w); 
    return a.x + v * t;
}
int judge(line a,line b,line c)
{
    edge o = maru(b,c);
    int d = dcmp((a.y - a.x) * (o - a.x));
    return d > 0 ? 0:1; 
}
bool cmp(line a,line b)
{
    if(!dcmp(a.d - b.d))
    {
        return (a.y - a.x) * (b.y - a.x) > 0;
     }
     return a.d < b.d;
}
int main()
{
    scanf("%d",&n);
    while(n--)
    {
        double lx,ly,bx,by,x,y;
        scanf("%d",&m);
        for(int i = 1;i <= m;i++)
        {
            scanf("%lf%lf",&x,&y);
            if(i == 1)
            {
                lx = bx = x;
                ly = by = y;
                continue;
            }
            else 
            {
                l[++cnt].x = (edge){lx,ly};
                l[cnt].y = (edge){x,y};
                lx = x;
                ly = y;
            }
        }
        l[++cnt].x = (edge){lx,ly};
        l[cnt].y = (edge){bx,by};
    }
    for(int i = 1;i <= cnt;i++)
    {
        l[i].d = atan2(l[i].x.y - l[i].y.y,l[i].x.x - l[i].y.x);
    }
    sort(l + 1,l + cnt + 1,cmp);
    for(int i = 1;i < cnt;i++)
    {
        if(!dcmp(l[i].d - l[i + 1].d))continue;
        l[++cnt1] = l[i];
    }
    l[++cnt1] = l[cnt];
    cnt = cnt1;
    int b = 1,e = 0;
    q[++e] = l[1];
    q[++e] = l[2];
    for(int i = 3;i <= cnt;i++)
    {
        while(b < e && judge(l[i],q[e],q[e - 1]))e--;
        while(b < e && judge(l[i],q[b],q[b + 1]))b++;
        q[++e] = l[i];
    }
    while(b < e && judge(q[e],q[b],q[b + 1]))b++;
    while(b < e && judge(q[b],q[e],q[e - 1]))e--;
    q[e + 1] = q[b];
    cnt1 = 0;
    for(int i = b;i <= e;i++)
    {
        e1[++cnt1] = maru(q[i],q[i + 1]);
    }
    double ans = 0;
    e1[++cnt1] = e1[1];
    if(cnt1 > 2)
    {
        for(int i = 1;i <= cnt1;i++)
        {
            ans += e1[i] * e1[i + 1];
        }
    }
    printf("%0.3lf",fabs(ans) / 2.0);
    return 0;
}

 

 

是否有核:判断交点个数

posted @ 2019-07-08 14:07  ywwywwyww  阅读(302)  评论(0编辑  收藏  举报