SuperMap.WindowsPhone.Core.PolylineElementClip.ClipPointCollection C# (CSharp) Method

ClipPointCollection() private method

private ClipPointCollection ( Point2DCollection points, Rectangle2D box ) : IList
points Point2DCollection
box Rectangle2D
return IList
        private IList<Point2DCollection> ClipPointCollection(Point2DCollection points, Rectangle2D box)
        {
            List<Point2DCollection> list = new List<Point2DCollection>();
            Rectangle2D rect = points.GetBounds();
            if (((rect.Right < box.Left) || (rect.Top < box.Bottom)) || ((rect.Left > box.Right) || (rect.Bottom > box.Top)))
            {
                return null;
            }//全部在外,不相交
            if (box.Contains(rect))
            {
                list.Add(points);
                return list;
            }//全部在内
            if (points.Count < 2)
            {
                return null;
            }
            Point2DCollection item = null;
            bool flag = box.Contains(points[0]);
            if (flag)
            {
                item = new Point2DCollection();
                list.Add(item);
                item.Add(points[0]);
            }//第一个点在内
            for (int i = 0; i < (points.Count - 1); i++)
            {
                Point2D head = points[i];
                Point2D end = points[i + 1];
                bool flag2 = box.Contains(end);
                if (flag && flag2)
                {
                    item.Add(end);
                }//同时在内。那么else则有三种情况,head内end外,head外edn内,hean外end外
                else
                {
                    if (flag != flag2)
                    {
                        if (flag)
                        {
                            item.Add(this.ClipLineSegment(head, end, box));
                        }//head内end外,添加交点
                        else
                        {
                            item = new Point2DCollection();
                            list.Add(item);
                            item.Add(this.ClipLineSegment(head, end, box));
                            item.Add(end);
                        }//head外edn内,添加交点和end点
                    }
                    else
                    {
                        Rectangle2D segmentBox = new Rectangle2D(Math.Min(head.X, end.X), Math.Min(head.Y, end.Y), Math.Max(head.X, end.X), Math.Max(head.Y, end.Y));
                        if (segmentBox.IntersectsWith(box))
                        {
                            item = new Point2DCollection();
                            list.Add(item);
                            //与四条边框求交点,在中间就添加上去
                            Point2D point3 = EdgeIntersection(head, end, box.Left, false);
                            Point2D point4 = EdgeIntersection(head, end, box.Right, false);
                            Point2D point5 = EdgeIntersection(head, end, box.Bottom, true);
                            Point2D point6 = EdgeIntersection(head, end, box.Top, true);
                            if ((point6.X >= box.Left) && (point6.X <= box.Right))
                            {
                                item.Add(point6);
                            }
                            if ((point5.X >= box.Left) && (point5.X <= box.Right))
                            {
                                item.Add(point5);
                            }
                            if ((point4.Y >= box.Bottom) && (point4.Y <= box.Top))
                            {
                                item.Add(point4);
                            }
                            if ((point3.Y >= box.Bottom) && (point3.Y <= box.Top))
                            {
                                item.Add(point3);
                            }
                            if (item.Count < 2)
                            {
                                list.Remove(item);
                            }

                        }//hean外end外,添加两个交点
                    }
                    flag = flag2;
                }
            }
            for (int j = list.Count - 1; j >= 0; j--)
            {
                Point2DCollection pts = list[j];
                if (pts.Count < 2)
                {
                    list.RemoveAt(j);
                }
            }
            if (list.Count == 0)
            {
                return null;
            }
            return list;
        }