コード例 #1
0
        /// <summary>
        /// non-overlapping Convex polygons can have continuous common edges.
        ///
        /// </summary>
        /// <param name="poly1">must be full close loop polygon</param>
        /// <param name="poly2">must be full close loop polygon</param>
        /// <returns></returns>
        static List <mPoint> mergeTwoConvexPolygons(List <mPoint> poly1, List <mPoint> poly2)
        {
            List <mPoint> ret   = new List <mPoint>();
            bool          found = false;

            for (int i = 0; i < poly1.Count - 1; i++)
            {
                LineSegment l1 = new LineSegment(poly1[i].x, poly1[i].y, poly1[i + 1].x, poly1[i + 1].y);
                for (int j = 0; j < poly2.Count - 1; j++)
                {
                    LineSegment l2 = new LineSegment(poly2[j].x, poly2[j].y, poly2[j + 1].x, poly2[j + 1].y);

                    bool sameDirection = true;
                    bool sameSegment   = false;
                    if (l1.x1 == l2.x1 && l1.y1 == l2.y1 && l1.x2 == l2.x2 && l1.y2 == l2.y2)
                    {
                        sameSegment   = true;
                        sameDirection = true;
                    }

                    if (l1.x1 == l2.x2 && l1.y1 == l2.y2 && l1.x2 == l2.x1 && l1.y2 == l2.y1)
                    {
                        sameSegment   = true;
                        sameDirection = false;
                    }


                    if (sameSegment)
                    {
                        // add half chain of poly1, excluding i
                        for (int p = 0; p < i; p++)
                        {
                            ret.Add(poly1[p]);
                        }

                        // add the entire poly2, starting from j or j+1 depending on direction
                        for (int q = 0; q < poly2.Count - 1; q++)
                        {
                            if (!sameDirection)
                            {
                                int idx = (q + j + 1) % (poly2.Count - 1);
                                ret.Add(poly2[idx]);
                            }
                            else
                            {
                                int idx = (j - q + poly2.Count - 1) % (poly2.Count - 1);
                                ret.Add(poly2[idx]);
                            }
                        }

                        // the other half poly1, excluding i+1
                        for (int p = i + 2; p < poly1.Count; p++)
                        {
                            ret.Add(poly1[p]);
                        }
                        found = true; break;
                    }
                }
                if (found)
                {
                    break;
                }
            }

            return(ret);
        }