示例#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 Double2Bool curPoint, ref int curedge)
        {
            if (poly1 == null || poly2 == null || poly1.GetEdgeNum() < 3 || poly2.GetEdgeNum() < 3)
            {
                return;
            }
            // 先找poly1 最小的。
            poly    = poly1;
            curedge = 0;
            Double2Bool min = poly1.GetPointPlus(0);

            for (int i = 1; i < poly1.GetEdgeNum(); i++)
            {
                Double2Bool point = poly1.GetPointPlus(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++)
            {
                Double2Bool point = poly2.GetPointPlus(i);
                if (point.y < min.y || (point.y == min.y && point.x < min.x))
                {
                    min     = point;
                    curedge = i;
                    poly    = poly2;
                }
            }
            curPoint = min;
        }
示例#2
0
        /// <summary>
        /// 求多边形的并。
        /// </summary>
        /// <param name="polygon1"></param>
        /// <param name="polygon2"></param>
        /// <returns></returns>
        private static Double2Bool[] CombinePolygon(Double2Bool[] polygon1, Double2Bool[] 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 <Double2Bool> listPoint = new List <Double2Bool>();
            Polygon2D          poly      = null;
            int         curedge          = 0;
            Double2Bool curPoint         = new Double2Bool(0, 0, true);
            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.GetPointPlus(curedge);
                    }
                    else
                    {
                        curedge--;
                        if (curedge < 0)
                        {
                            curedge = poly.GetEdgeNum() - 1;
                        }
                        curPoint = poly.GetPointPlus(curedge + 1);
                    }
                }
                else // 则需要交换了。换到另一个多边形。
                {
                    // 肯定是边上的点了, 强制设置为true,可通过
                    curPoint = new Double2Bool(nextPoint.x, nextPoint.y, true);
                    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());
        }