判断点是否在两个多边形之间

inline bool in_range(float t1, float t2,float x)
{
    return x>t1 && x<t2 || x<t1 && x>t2;
}

//判断点是否在圈内
//采用水平填充算法
/*    
1. count ← 0;
2. 以P为端点,作从右向左的射线L;  
3, for 多边形的每条边s
4.   do if P在边s上  
5.          then return true;
6.      if s不是水平的
7.          then if s的一个端点在L上且该端点是s两端点中纵坐标较大的端点
9.                  then count ← count+1
10.              else if s和L相交
11.                 then count ← count+1;
12. if count mod 2 = 1 
13.   then return true
14.   else return false;
*/
bool Fish::IsInside(Point *polygon, int N, Point p)
{
    bool inside = true;
    int count=0;
    for (int i=0; i<N; i++)
    {

        if (polygon[i].y != polygon[i+1].y)//s不是水平的
        {
            //if (polygon[i].y == p.y)continue;
            if ((polygon[i].y == p.y && polygon[i].x<p.x)
            && (polygon[i].y>polygon[i-1].y || polygon[i].vy<0))
            {
                count++;
            }
            else 
            if (in_range(polygon[i].y, polygon[i].y+polygon[i].vy,p.y))
            {
                if(in_range(polygon[i].x, polygon[i].x+polygon[i].vx,p.x))
                {
                    count++;
                    float k=polygon[i].vy/polygon[i].vx;
                    float x=polygon[i].x-(polygon[i].y-p.y)/k;
                    if (x>p.x)
                    {
                        count--;
                    }
                    else if(x==p.x)
                    {
                        return true;
                    }
                }
                else if(p.x >=polygon[i].x && p.x >=polygon[i].x+polygon[i].vx )
                {
                    count++;
                }
            }
        }
    }
    if (1 == count%2)
    {
        inside = true;
    }
    else
    {
        inside = false;
    }
    return inside;
}


//判断点 是否满足条件
bool Fish::IsOkPoint(Point &point)
{
    Point *p_point_outside,*p_point_inside;
    int n_inside,n_outside;
    map.GetNum(n_inside,n_outside);
    p_point_inside=map.GetInPoint();
    p_point_outside=map.GetOutPoint();

    //点在内边界的外面,在外边界的里面
    if (!IsInside(p_point_inside,n_inside,point)
        && IsInside(p_point_outside,n_outside,point))
    {
        return true;
    }
    else
    {
        return false;
    }
}

作者:涵曦www.hanxi.cc
出处:hanxi.cnblogs.com
GitHub:github.com/hanxi
Email:im.hanxi@gmail.com
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

《 Skynet 游戏服务器开发实战》

原文地址:https://www.cnblogs.com/hanxi/p/2478184.html