zoj1081

本文介绍了一种判断一个点是否位于给定多边形内的算法。通过构造射线并计算其与多边形各边的交点来确定点的位置关系。适用于计算机图形学、地理信息系统等领域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

判断点是否在多边形内部

#include <iostream>
#include <queue>   
#include <stack>   
#include <math.h>   
#include <stdio.h>   
#include <stdlib.h>      
#include <limits.h>   
#include <string.h>   
#include <algorithm> 
using namespace std;  
const int MAX = 110;  
const double eps = 1e-6;  
struct point{  
    double x,y;  
};  
struct beeline{  
    point a,b;  
};  
point p[MAX];  
int n;  
bool dy(double x,double y)  // x > y    
{  
    return x > y + eps;  
}  
bool xy(double x,double y) // x < y    
{  
    return x < y - eps;  
}  
bool dyd(double x,double y) // x >= y    
{  
    return x > y - eps;  
}  
bool xyd(double x,double y) // x <= y    
{  
    return x < y + eps;  
}  
bool dd(double x,double y)  // x == y    
{  
    return fabs( x - y ) < eps;  
}  
double crossProduct(point a,point b,point c)    
{  
    return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);  
}  
bool onSegment(point a, point b, point c)  
{  
    double maxx = max(a.x,b.x);  
    double maxy = max(a.y,b.y);  
    double minx = min(a.x,b.x);  
    double miny = min(a.y,b.y);  
    if( dd(crossProduct(a,b,c),0.0) && dyd(c.x,minx) && xyd(c.x,maxx) && dyd(c.y,miny) && xyd(c.y,maxy) )  
        return true;  
    return false;  
}  
bool segIntersect(point p1,point p2, point p3, point p4)  
{  
    double d1 = crossProduct(p3,p4,p1);  
    double d2 = crossProduct(p3,p4,p2);  
    double d3 = crossProduct(p1,p2,p3);  
    double d4 = crossProduct(p1,p2,p4);  
    if( xy(d1 * d2,0.0) && xy( d3*d4,0.0 ) )  
       return true;  
    if( dd(d1,0.0) && onSegment(p3,p4,p1) )  
        return true;  
    if( dd(d2,0.0) && onSegment(p3,p4,p2) )  
        return true;  
    if( dd(d3,0.0) && onSegment(p1,p2,p3) )  
        return true;  
    if( dd(d4,0.0) && onSegment(p1,p2,p4) )  
        return true;  
    return false;  
}  
bool inPolygon(point pot)  
{  
    int count = 0;  
    beeline l;  
    l.a = pot;  
    l.b.x = 1e10;  
    l.b.y = pot.y;  
    p[n] = p[0];  
    for(int i=0; i<n; i++)  
    {  
        if( onSegment(p[i],p[i+1],pot) )  
            return true;  
        if( !dd(p[i].y,p[i+1].y) )  
        {  
            int tmp = -1;  
            if( onSegment(l.a,l.b,p[i]) )  
                tmp = i;  
            else  
                if( onSegment(l.a,l.b,p[i+1]) )  
                    tmp = i+1;  
            if( tmp != -1 && dd(p[tmp].y,max(p[i].y,p[i+1].y)) )  
                count++;  
            else  
                if( tmp == -1 && segIntersect(p[i],p[i+1],l.a,l.b) )  
                    count++;  
        }  
   }  
    if( count % 2 == 1 )  
        return true;  
    return false;  
}  
int main()  
{  
    int m;  
    int ind = 1;  
    point pot;  
    while( scanf("%d",&n) && n )  
    {  
        if( ind != 1 )  
            printf("\n");  
        scanf("%d",&m);  
        for(int i=0; i<n; i++)  
            scanf("%lf %lf",&p[i].x,&p[i].y);  
        printf("Problem %d:\n",ind++);  
       while( m-- )  
        {  
           scanf("%lf %lf",&pot.x,&pot.y);  
           if( inPolygon(pot) )  
                printf("Within\n");  
            else  
               printf("Outside\n");  
        }  
    }  
return 0;  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值