Ejemplo n.º 1
0
        /// <summary>
        /// 一个点是否在复杂多边形内部(点选是否选中了复杂多边形)
        /// </summary>
        /// <param name="point"></param>
        /// <param name="multipolygon"></param>
        /// <returns></returns>
        static public bool IsPointInMultiPolygon(PointD point, MultiPolygon multipolygon)
        {
            if (!IsPointInRect(point, multipolygon.MBR))
            {
                return(false);
            }
            int spolygonCount = multipolygon.Count;
            int sInterLineNum = 0;

            //循环对每个polygon求交点并计算交点总数
            for (int i = 0; i < spolygonCount; i++)
            {
                Polygon mpolygon = multipolygon.GetPolygon(i);
                int     sCount   = mpolygon.Count;
                PointD  sP1      = new PointD();
                PointD  sP2      = new PointD();
                for (int j = 0; j < sCount - 1; j++)
                {
                    sP1 = mpolygon.GetPoint(j);
                    sP2 = mpolygon.GetPoint(j + 1);
                    if (IsRayInterLine(point, sP1, sP2))
                    {
                        sInterLineNum++;
                    }
                }
                if (IsRayInterLine(point, mpolygon.GetPoint(0), mpolygon.GetPoint(sCount - 1)))
                {
                    sInterLineNum++;
                }
            }

            return(sInterLineNum % 2 == 1);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 判断复杂多边形是否位于矩形盒内(框选是否选中复杂多边形)
        /// </summary>
        /// <param name="multipolygon"></param>
        /// <param name="rect"></param>
        /// <returns></returns>
        static public bool IsRectInterMultipolygon(MultiPolygon multipolygon, RectangleD rect)
        {
            //判断外包矩形与矩形框是否相交
            if (IsMBRInterRect(multipolygon.MBR, rect) == false)
            {
                return(false);
            }
            //先判断是否有点在矩形框内
            int spolygonCount = multipolygon.Count;

            for (int i = 0; i < spolygonCount; i++)
            {
                Polygon spolygon = multipolygon.GetPolygon(i);
                int     sCount   = spolygon.Count;
                for (int j = 0; j < sCount; j++)
                {
                    if (IsPointInRect(spolygon.GetPoint(j), rect))
                    {
                        return(true);
                    }
                }
            }
            //判断矩形框点是否在复杂多边形内
            if (IsPointInMultiPolygon(new PointD(rect.MaxX, rect.MaxY), multipolygon))
            {
                return(true);
            }
            if (IsPointInMultiPolygon(new PointD(rect.MaxX, rect.MinY), multipolygon))
            {
                return(true);
            }
            if (IsPointInMultiPolygon(new PointD(rect.MinX, rect.MaxY), multipolygon))
            {
                return(true);
            }
            if (IsPointInMultiPolygon(new PointD(rect.MinX, rect.MinY), multipolygon))
            {
                return(true);
            }
            //接着判断复杂多边形的边是否与矩形框相交
            //循环对每个polygon的每条边做求交判断
            for (int i = 0; i < spolygonCount; i++)
            {
                Polygon mpolygon = multipolygon.GetPolygon(i);
                int     sCount   = mpolygon.Count;
                for (int j = 0; j < sCount - 1; j++)
                {
                    if (IsLineInterLine(mpolygon.GetPoint(j), mpolygon.GetPoint(j + 1), new PointD(rect.MaxX, rect.MaxY), new PointD(rect.MaxX, rect.MinY)))
                    {
                        return(true);
                    }
                    if (IsLineInterLine(mpolygon.GetPoint(j), mpolygon.GetPoint(j + 1), new PointD(rect.MaxX, rect.MaxY), new PointD(rect.MinX, rect.MaxY)))
                    {
                        return(true);
                    }
                    if (IsLineInterLine(mpolygon.GetPoint(j), mpolygon.GetPoint(j + 1), new PointD(rect.MinX, rect.MaxY), new PointD(rect.MinX, rect.MinY)))
                    {
                        return(true);
                    }
                    if (IsLineInterLine(mpolygon.GetPoint(j), mpolygon.GetPoint(j + 1), new PointD(rect.MinX, rect.MinY), new PointD(rect.MaxX, rect.MinY)))
                    {
                        return(true);
                    }
                }
                //判断最后一个顶点与起始点组成的边是否与矩形盒相交
                if (IsLineInterLine(mpolygon.GetPoint(0), mpolygon.GetPoint(sCount - 1), new PointD(rect.MaxX, rect.MaxY), new PointD(rect.MaxX, rect.MinY)))
                {
                    return(true);
                }
                if (IsLineInterLine(mpolygon.GetPoint(0), mpolygon.GetPoint(sCount - 1), new PointD(rect.MaxX, rect.MaxY), new PointD(rect.MinX, rect.MaxY)))
                {
                    return(true);
                }
                if (IsLineInterLine(mpolygon.GetPoint(0), mpolygon.GetPoint(sCount - 1), new PointD(rect.MinX, rect.MaxY), new PointD(rect.MinX, rect.MinY)))
                {
                    return(true);
                }
                if (IsLineInterLine(mpolygon.GetPoint(0), mpolygon.GetPoint(sCount - 1), new PointD(rect.MinX, rect.MinY), new PointD(rect.MaxX, rect.MinY)))
                {
                    return(true);
                }
            }
            //其他
            return(false);
        }