点是否在多边形内

/// <summary>
        /// 点是否在多边形内
        /// </summary>
        /// <param name="boundary">构成边界的点</param>
        /// <param name="point">检查的点</param>
        /// <returns>是或否</returns>
        private bool InBoudary(List<PointD> boundary, PointD point,double xMax)
        {
            //作point点的一条水平线段
            PointD pointMax = new PointD(xMax, point.Y);

            bool isInBoundary = true;//是否在边界内
            int insectNum = 0;//相交的个数
            for (int i = 0; i < boundary.Count-1; i++)
            {
                //判断线段向上还是向下
                double yMax = 0, yMin = 0;
                if (boundary[i].Y>boundary[i+1].Y)
                {
                    yMax = boundary[i].Y;
                    yMin = boundary[i + 1].Y;
                }
                else
                {
                    yMin = boundary[i].Y;
                    yMax = boundary[i + 1].Y;
                }

                //判断线段是否相交
                //快速检验
                if (boundary[i].Y==boundary[i+1].Y||yMax<point.Y||yMin>point.Y)
                {
                    continue;
                }
                else
                {
                    double firstCross = (point.X - boundary[i].X) * (boundary[i + 1].Y - boundary[i].Y) - (point.Y - boundary[i].Y) * (boundary[i + 1].X - boundary[i].X);
                    double secondCross = (pointMax.X - boundary[i].X) * (boundary[i + 1].Y - boundary[i].Y) - (pointMax.Y - boundary[i].Y) * (boundary[i + 1].X - boundary[i].X);
                    double cross = firstCross * secondCross;
                    //>0 不相交
                    if (cross>0)
                    {
                        continue;
                    }
                    else
                    {
                        //向下
                        if (boundary[i].Y>boundary[i+1].Y)
                        {
                            if (point.Y==boundary[i].Y)
                            {
                                continue;
                            }
                            else
                            {
                                insectNum++;
                            }
                        }
                        else
                        {
                            if (point.Y == boundary[i+1].Y)
                            {
                                continue;
                            }
                            else
                            {
                                insectNum++;
                            }
                        }
                       
                    }
                }
            }

            //如果相交个数为奇数则在多边形内部
            if (insectNum%2==0)
            {
                isInBoundary = false;
            }
            return isInBoundary;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值