点在多边形内的判定

算法的思路是从点处引出一条向右的射线,逆时针穿过时wn++,顺时针穿过时wn--,何为顺时针穿过呢,就是向量poly[i]-p在向量poly[i]-ploy[i+1]的顺时针方向,逆时针同理。

#include<bits/stdc++.h>
using namespace std;
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y) { }
};
typedef Point Vector ;
struct Polygon
{
    double x,y;
    Polygon(double x=0,double y=0):x(x),y(y) { }
}poly[1005];
double Dot(Vector A,Vector B)
{
    return A.x*B.x+A.y*B.y;
}
bool operator == (Point A,Polygon B)
{
    if(A.x==B.x&&A.y==B.y) return 1;
    return 0;
}
Vector operator - (Polygon A,Point B)
{
    return  Vector (A.x-B.x,A.y-B.y);
}
double Length(Vector A)
{
    return sqrt(Dot(A,A));
}
bool isPointOnsegment(Point p,Polygon a,Polygon b)
{
    if(p==a||p==b) return 1;//线段的端点
    if(Dot(a-p,b-p)==-1*Length(a-p)*Length(b-p))//线段内
    return 1;
    return 0;
}
double eps=1e-10;
double dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}
int isPointInPolygon(Point p,Polygon poly)
{
    int wn=0;
    int n;
    for(int i=0;i<n;i++)
    {
        if(isPointOnsegment(p,poly[i],poly[(i+1)%n])) return -1;//边界
        int k=dcmp(Cross(poly[(i+1)%n]-poly[i],p-poly[i]));
        int d1=dcmp(poly[i].y-p.y);
        int d2=dcmp(poly[(i+1)%n].y-p.y);
        if(k>0&&d1<=0&&d2>0) wn++;
        if(k<0&&d2<=0&&d1>0) wn--;
    }
    if(wn!=0) return 1;
    return 0;
}
int main()
{
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值