View Code of Problem 2595

#include <stdio.h>
#include <math.h>
#define inf 999999999
#define eps 1e-9
using namespace std;
typedef struct node
{
    double x, y;
}node;
typedef struct node2
{
    double k,b;
}node2;
node dig[105];
node  con[4];
node2 bian[4];
double l1,l2;
double Abs (double a )
{
    return a>eps?a:-a;
}
node2 build_segment (node x,node y)//建立直线或线段
{
    node2 k;
    if (x.x==y.x)
    {
        k.k=inf ;
        k.b=x.x;
        return k;
    }
    else
    {
        k.k=(x.y-y.y)/(x.x-y.x);
        k.b=-k.k*x.x+x.y;
        return k;
    }
}
int check (node a)
{
    double tmp1,tmp2;
    if (bian[0].k!=inf)
    {
        tmp1=a.y-bian[0].k*a.x-bian[0].b;
        tmp2=a.y-bian[2].k*a.x-bian[2].b;
    }
    else
    {
        tmp1=a.x-bian[0].b;
        tmp2=a.x-bian[2].b;
    }
    if (tmp1>0&&tmp2>0)
        return 0;
    else if (tmp1<0&&tmp2<0)
        return 0;
    if (bian[1].k!=inf)
    {
        tmp1=a.y-bian[1].k*a.x-bian[1].b;
        tmp2=a.y-bian[3].k*a.x-bian[3].b;
    }
    else
    {
        tmp1=a.x-bian[1].b;
        tmp2=a.x-bian[3].b;
    }
    if (tmp1>0&&tmp2>0)
        return 0;
    else if (tmp1<0&&tmp2<0)
        return 0;


    return 1;

}
int main()
{
    int  T;
    scanf ("%d",&T);
    while (T--)
    {
        int n,num=0;
        scanf ("%d",&n);
        for (int i=0;i<n;i++)
            scanf ("%lf%lf",&dig[i].x,&dig[i].y);
        for (int i=0;i<4;i++)
            scanf ("%lf%lf",&con[i].x,&con[i].y);
        for (int i=0;i<3;i++)
            bian[i]=build_segment(con[i],con[i+1]);
        bian[3]=build_segment(con[3],con[0]);
        for (int i=0;i<n;i++)
            if (check(dig[i])==1)
                num++;
        printf ("%d\n",num);
    }
    return 0;
}

Double click to view unformatted code.


Back to problem 2595