• HDU 1756 Cupid's Arrow( 判断点在多边形的内外 )



    链接:传送门

    思路:判断每支箭是否在多边形内,计算几何点定位中水题,不清楚下面的代码能不能适用于给定点的顺序不确定( 既不是顺时针又不是逆时针 )


    /*************************************************************************
        > File Name: hdu1756.cpp
        > Author:    WArobot 
        > Blog:      http://www.cnblogs.com/WArobot/ 
        > Created Time: 2017年04月30日 星期日 22时58分41秒
     ************************************************************************/
    
    #include<bits/stdc++.h>
    using namespace std;
    
    #define dou double
    #define eps 1.0e-5
    
    struct point{
    	dou x;
    	dou y;
    }po[110];
    int n,m;
    
    // 判断是否在线上
    bool online(point p1,point p2,point p3){
    	if( p2.x>=min(p1.x,p3.x) && p2.x<=max(p1.x,p3.x) && p2.y>=min(p1.y,p3.y) && p2.y<=max(p1.y,p3.y)){
    		if(fabs((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))<=eps)
    			return true;
    	}
    	return false;
    }
    // 判断是否在多边形之内
    bool inside(point p){
    	int cnt = 0;
    	dou xinters;
    	point p1,p2;
    	p1 = po[0];
    	for(int i=1;i<=n;i++){
    		p2 = po[i%n];
    		if( online(p1,p,p2) )	return true;
    		if( p.y>min(p1.y,p2.y) && p.y<=max(p1.y,p2.y) && p.x<=max(p1.x,p2.x) && p1.y!=p2.y ){
    			xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y) + p1.x;
    			if( p1.x==p2.x || p.x<=xinters )	cnt++;
    		}
    		p1 = p2;
    	}
    	if(cnt%2==0)	return false;
    	return	true;
    }
    
    int main(){
    	point tmp;
    	while(~scanf("%d",&n)){
    		for(int i=0;i<n;i++)	scanf("%lf%lf",&po[i].x,&po[i].y);
    		scanf("%d",&m);
    		for(int i=0;i<m;i++){
    			scanf("%lf%lf",&tmp.x,&tmp.y);
    			if( inside(tmp) )	printf("Yes
    ");
    			else				printf("No
    ");
    		}
    	}
    	return 0;
    }
  • 相关阅读:
    最全Redis面试题
    mabatisplus-update
    windows下安装redis并部署服务
    登录方案
    redis
    IntelliJ IDEA Debug模式启动项目
    物流跟踪 调用快递鸟API
    springboot中文官方文档
    国内物流地址
    什么是电磁兼容?什么是EMC设计?
  • 原文地址:https://www.cnblogs.com/WArobot/p/6790761.html
Copyright © 2020-2023  润新知