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;
    }
}
double point_to_segment(node point ,node2 seg)//点到直线距离
{
    double ans;
    if (seg.k!=inf)
        {
            ans=Abs (seg.k*point.x-point.y+seg.b);
            ans/=sqrt(seg.k*seg.k+1);
        }
    else
    {
        ans=Abs (seg.b-point .x);
    }
    return ans;
}
int check (node a)
{

        double tmp=point_to_segment(a,bian[0]);
        if (tmp>l2)
            return 0;
        tmp=point_to_segment(a,bian[1]);
        if (tmp>l1)
            return 0;
        tmp=point_to_segment(a,bian[2]);
        if (tmp>l2)
            return 0;
        tmp=point_to_segment(a,bian[3]);
        if (tmp>l1)
            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]);
        l1=(con[0].x-con[1].x)*(con[0].x-con[1].x)+(con[0].y-con[1].y)*(con[0].y-con[1].y);
        l2=(con[2].x-con[1].x)*(con[2].x-con[1].x)+(con[2].y-con[1].y)*(con[2].y-con[1].y);
        l1=sqrt(l1);
        l2=sqrt(l2);
        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