Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="poly1"></param>
        /// <param name="poly2"></param>
        /// <param name="poly"></param>
        /// <param name="curedge"></param>
        private static void SetInitData(Polygon2D poly1, Polygon2D poly2, ref Polygon2D poly, ref Double2 curPoint, ref int curedge)
        {
            if (poly1 == null || poly2 == null || poly1.GetEdgeNum() < 3 || poly2.GetEdgeNum() < 3)
            {
                return;
            }
            // 先找poly1 最小的。
            poly    = poly1;
            curedge = 0;
            Double2 min = poly1.GetPoint(0);

            for (int i = 1; i < poly1.GetEdgeNum(); i++)
            {
                Double2 point = poly1.GetPoint(i);
                if (point.y < min.y || (point.y == min.y && point.x < min.x))
                {
                    min     = point;
                    curedge = i;
                }
            }
            //
            for (int i = 0; i < poly2.GetEdgeNum(); i++)
            {
                Double2 point = poly2.GetPoint(i);
                if (point.y < min.y || (point.y == min.y && point.x < min.x))
                {
                    min     = point;
                    curedge = i;
                    poly    = poly2;
                }
            }
            curPoint = min;
        }
Esempio n. 2
0
        /// <summary>
        /// 主多边形上找到一个顶点,不在diff多边形内的。
        /// </summary>
        /// <param name="mainPoly"></param>
        /// <param name="diffpoly"></param>
        /// <param name="poly"></param>
        /// <param name="curedge"></param>
        public static bool FindOutDiffPointPoint(Polygon2D mainPoly, Polygon2D diffpoly, ref Double2 curPoint, ref int curedge)
        {
            if (mainPoly == null || diffpoly == null || mainPoly.GetEdgeNum() < 3 || diffpoly.GetEdgeNum() < 3)
            {
                return(false);
            }

            for (int i = 0; i < mainPoly.GetEdgeNum(); i++)
            {
                Double2 point = mainPoly.GetPoint(i);
                if (diffpoly.CheckIn(point) == false)
                {
                    curPoint = point;
                    curedge  = i;
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 3
0
        /// <summary>
        /// 获取多边形所有边的相交点
        /// </summary>
        /// <param name="mainPoly"></param>
        /// <param name="diffpoly"></param>
        /// <param name="mainPolyIntersectArray"></param>
        /// <param name="Poly2IntersectArray"></param>
        public static void GetAllEdgeInterSectPoint(Polygon2D mainPoly, Polygon2D diffpoly, ref List <Double3>[] mainPolyIntersectArray, ref List <Double3>[] Poly2IntersectArray)
        {
            if (mainPoly == null || diffpoly == null)
            {
                return;
            }

            for (int i = 0; i < mainPoly.GetEdgeNum(); i++)
            {
                diffpoly.GetAllIntersectPoint(mainPoly.GetEdge(i), ref mainPolyIntersectArray[i]);
            }
            //
            for (int i = 0; i < diffpoly.GetEdgeNum(); i++)
            {
                mainPoly.GetAllIntersectPoint(diffpoly.GetEdge(i), ref Poly2IntersectArray[i]);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 获取多边形所有边的相交点
        /// </summary>
        /// <param name="poly1"></param>
        /// <param name="poly2"></param>
        /// <param name="Poly1IntersectArray"></param>
        /// <param name="Poly2IntersectArray"></param>
        private static void GetAllEdgeInterSectPoint(Polygon2D poly1, Polygon2D poly2, ref List <Double3>[] Poly1IntersectArray, ref List <Double3>[] Poly2IntersectArray)
        {
            if (poly1 == null || poly2 == null)
            {
                return;
            }

            for (int i = 0; i < poly1.GetEdgeNum(); i++)
            {
                poly2.GetAllIntersectPoint(poly1.GetEdge(i), ref Poly1IntersectArray[i]);
            }
            //
            for (int i = 0; i < poly2.GetEdgeNum(); i++)
            {
                GetAllIntersectPoint(poly2.GetEdge(i).startPoint, i, Poly1IntersectArray, ref Poly2IntersectArray[i]);
            }
        }
Esempio n. 5
0
 /// <summary>
 /// 与多边形的关系
 /// </summary>
 /// <param name="dbd1"></param>
 /// <returns>true 相交: false 不相交</returns>
 public override bool CheckIntersect(Polygon2D ab)
 {
     if (ab == null)
     {
         return(false);
     }
     for (int i = 0; i < ab.GetEdgeNum(); i++)
     {
         if (this.CheckIn(ab.GetPoint(i)) == true)
         {
             return(true);
         }
         if (this.CheckLineRelation(ab.GetEdge(i)) == LineRelation.Intersect)
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 6
0
        /// <summary>
        /// 与多边形的关系
        /// </summary>
        /// <param name="ab"></param>
        /// <returns>true 相交: false 不相交</returns>
        public override bool CheckIntersect(Polygon2D ab)
        {
            if (ab == null)
            {
                return(false);
            }
            if (ab.CheckIn(this.circleCenter) == true)
            {
                return(true);
            }

            for (int i = 0; i < ab.GetEdgeNum(); i++)
            {
                LineSegment2D ls = ab.GetEdge(i);
                if (this.CheckLineRelation(ls) != LineRelation.Detach)
                {
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 7
0
        /// <summary>
        /// 求多边形的和。
        /// </summary>
        /// <param name="mainPoly">逆时针序列</param>
        /// <param name="addPoly">逆时针序列</param>
        /// <returns></returns>
        public static Double2[] CalcPoly(Double2[] mainPoly, Double2[] addPoly)
        {
            if (mainPoly == null || mainPoly.Length < 3)
            {
                return(null);
            }
            if (addPoly == null || addPoly.Length < 3)
            {
                return(mainPoly);
            }
            //
            Polygon2D mainPoly_ = new Polygon2D(mainPoly);
            Polygon2D diffPoly_ = new Polygon2D(addPoly);

            // 获取边上的交点
            List <Double3>[] mainPolyIntersectArray = new List <Double3> [mainPoly_.GetEdgeNum()];
            List <Double3>[] addPolyIntersectArray  = new List <Double3> [diffPoly_.GetEdgeNum()];
            PolygonBool.GetAllEdgeInterSectPoint(mainPoly_, diffPoly_, ref mainPolyIntersectArray, ref addPolyIntersectArray);

            // 没有交点直接返回呗
            bool CheckIntersect = false;

            foreach (List <Double3> list in mainPolyIntersectArray)
            {
                if (list != null && list.Count > 0)
                {
                    CheckIntersect = true;
                    break;
                }
            }
            if (CheckIntersect == false)
            {
                PolygonBool.ClearPolyIntersectArray(ref mainPolyIntersectArray, ref addPolyIntersectArray);
                return(mainPoly);
            }
            // 有交点的处理
            List <Double2> listPoint = new List <Double2>();
            int            curedge   = 0;
            Double2        curPoint  = Double2.zero;

            // 查找在diffpoly外的一个顶点。
            if (PolygonBool.FindOutDiffPointPoint(mainPoly_, diffPoly_, ref curPoint, ref curedge) == false)
            {
                return(mainPoly);
            }
            // 初始化数据。
            Polygon2D poly = mainPoly_;

            List <Double3>[] curPolyIntersectArray = mainPolyIntersectArray;

            while (poly != null && curedge >= 0 && curedge < poly.GetEdgeNum())
            {
                Point2D ls2d = poly.GetSimpleEdge(curedge);

                if (PolygonBool.AddPoint(listPoint, curPoint) == false)
                {
                    break;
                }
                Double3 nextPoint = Double3.zero;
                bool    ret       = PolygonBool.GetNearPointInEdge(poly, mainPoly_, diffPoly_, ls2d, curPoint, curPolyIntersectArray[curedge], ref nextPoint);
                if (ret == false)
                {
                    curedge++;
                    if (curedge >= poly.GetEdgeNum())
                    {
                        curedge = 0;
                    }
                    curPoint = poly.GetEdge(curedge).startPoint;
                }
                else // 则需要交换了。
                {
                    Point2D otherEdge = PolygonBool.GetOtherEdge(poly, mainPoly_, diffPoly_, (int)nextPoint.z);

                    if (Double2.Cross(ls2d.endPoint - ls2d.startPoint, otherEdge.endPoint - otherEdge.startPoint) <= 0)  // 进一步判断是否需要更换
                    {
                        curPoint = new Double2(nextPoint.x, nextPoint.y);
                        curedge  = (int)nextPoint.z;
                        PolygonBool.ExChangePoly(ref poly, mainPoly_, diffPoly_, ref curPolyIntersectArray, mainPolyIntersectArray, addPolyIntersectArray);
                    }
                    else // 不需要变换了。
                    {
                        curedge++;
                        if (curedge >= poly.GetEdgeNum())
                        {
                            curedge = 0;
                        }
                        curPoint = poly.GetEdge(curedge).startPoint;
                    }
                }
            }
            PolygonBool.ClearPolyIntersectArray(ref mainPolyIntersectArray, ref addPolyIntersectArray);
            return(listPoint.ToArray());
        }
Esempio n. 8
0
        /// <summary>
        /// 求多边形的并。
        /// </summary>
        /// <param name="polygon1"></param>
        /// <param name="polygon2"></param>
        /// <returns></returns>
        private static Double2[] CombinePolygon(Double2[] polygon1, Double2[] polygon2, ref bool isCombine)
        {
            isCombine = true;
            if (polygon1 == null || polygon1.Length < 3)
            {
                return(null);
            }
            if (polygon2 == null || polygon2.Length < 3)
            {
                return(polygon1);
            }
            //
            Polygon2D poly1 = new Polygon2D(polygon1);
            Polygon2D poly2 = new Polygon2D(polygon2);

            // 获取poly1每条线段上的交点。
            List <Double3>[] Poly1IntersectArray = new List <Double3> [poly1.GetEdgeNum()];
            List <Double3>[] Poly2IntersectArray = new List <Double3> [poly2.GetEdgeNum()];
            GetAllEdgeInterSectPoint(poly1, poly2, ref Poly1IntersectArray, ref Poly2IntersectArray);

            bool CheckIntersect = false;

            foreach (List <Double3> list in Poly1IntersectArray)
            {
                if (list != null && list.Count > 0)
                {
                    CheckIntersect = true;
                    break;
                }
            }
            if (CheckIntersect == false)
            {
                isCombine = false;
                ClearPolyIntersectArray(ref Poly1IntersectArray, ref Poly2IntersectArray);
                return(polygon1);
            }
            //
            List <Double2> listPoint = new List <Double2>();
            Polygon2D      poly      = null;
            int            curedge   = 0;
            Double2        curPoint  = Double2.zero;
            bool           SearchDir = true;

            List <Double3>[] curPolyIntersectArray = null;
            SetInitData(poly1, poly2, ref poly, ref curPoint, ref curedge);
            if (poly == poly1)
            {
                curPolyIntersectArray = Poly1IntersectArray;
            }
            else if (poly == poly2)
            {
                curPolyIntersectArray = Poly2IntersectArray;
            }

            while (poly != null && curedge >= 0 && curedge < poly.GetEdgeNum())
            {
                Point2D ls2d      = poly.GetSimpleEdge(curedge);
                Double2 normalDir = poly.GetNormal(curedge);

                if (AddPoint(ref listPoint, curPoint) == false)
                {
                    break;
                }
                Double3 nextPoint = Double3.zero;
                bool    ret       = GetNearPointInEdge(ls2d, SearchDir, curPoint, curPolyIntersectArray[curedge], ref nextPoint);
                if (ret == false)
                {
                    if (SearchDir == true)
                    {
                        curedge++;
                        if (curedge >= poly.GetEdgeNum())
                        {
                            curedge = 0;
                        }
                        curPoint = poly.GetEdge(curedge).startPoint;
                    }
                    else
                    {
                        curedge--;
                        if (curedge < 0)
                        {
                            curedge = poly.GetEdgeNum() - 1;
                        }
                        curPoint = poly.GetEdge(curedge).endPoint;
                    }
                }
                else // 则需要交换了。
                {
                    curPoint = new Double2(nextPoint.x, nextPoint.y);
                    curedge  = (int)nextPoint.z;
                    ExChangePoly(ref poly, poly1, poly2, ref curPolyIntersectArray, Poly1IntersectArray, Poly2IntersectArray);
                    Point2D ls = poly.GetSimpleEdge(curedge);

                    if (Double2.Dot(ls.endPoint - ls.startPoint, normalDir) > 0)
                    {
                        SearchDir = true;
                    }
                    else
                    {
                        SearchDir = false;
                    }
                }
            }
            ClearPolyIntersectArray(ref Poly1IntersectArray, ref Poly2IntersectArray);
            return(listPoint.ToArray());;
        }