hdu 1756 Cupid's Arrow 计算几何

判断一个点是否在多边形之内,我开始是使用叉乘求面积,wa了一段时间后然后发现这个算法是错误的,于是上网搜了题解,我的写法参考了这篇文章http://blog.csdn.net/lfz95/article/details/8037685

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1756

#include <stdio.h>
#include <math.h>

const double pi = acos(-1.0);
const double eps = 1e-8;

struct Node {
    double x,y;
};

Node a[105],temp;

int n,m;

double cross(Node a1,Node a2,Node a3){
    return (a2.x-a1.x)*(a3.y-a2.y) - (a3.x-a2.x)*(a2.y-a1.y);
}

double dist2(Node a1,Node a2){
    return (a1.x-a2.x)*(a1.x-a2.x)+(a1.y-a2.y)*(a1.y-a2.y);
}

double get_each_digit(Node a1,Node a2,Node a3){
    double t1 = dist2(a1,a2) + dist2(a1,a3) - dist2(a2,a3);
    double t2 = 2 * sqrt(dist2(a1,a2)) * sqrt(dist2(a1,a3));
    return acos(t1/t2);
}

double get_Digit(){
    double digit = 0,tempDigit;
    for(int i = 0;i<n;i++){
        if(fabs(cross(temp,a[i],a[i+1]))<eps)continue;
        tempDigit = get_each_digit(temp,a[i],a[i+1]);
        if(cross(temp,a[i],a[i+1])<0)tempDigit = -tempDigit;
        digit += tempDigit;
    }
    return digit;
}

void input(){
    while(~scanf("%d",&n)){
        for(int i = 0;i<n;i++){
            scanf("%lf %lf",&a[i].x,&a[i].y);
        }
        a[n] = a[0];
        scanf("%d",&m);
        for(int i = 0;i<m;i++){
            scanf("%lf %lf",&temp.x,&temp.y);
            double digit = get_Digit();
            if(fabs(digit)<eps)puts("No");
            else puts("Yes");
        }
    }
}

void File(){
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdin);
}

int main(void){
    //File();
    input();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值