internal Point2DCollection Clip(Point2DCollection pts) { Point2DCollection newPoints ; newPoints = pts; if (pts.GetBounds().IntersectsWith(this.boundary)) { foreach (Edge edge in this.Edges) { newPoints = ClipRing(newPoints, edge); } } return newPoints; }
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; }
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 (((rect.Left >= box.Left) && (rect.Bottom >= box.Bottom)) && ((rect.Right <= box.Right) && (rect.Top <= box.Top))) { list.Add(points); return list; }//全部在内 if (points.Count < 2) { return null; } Point2DCollection item = null; bool flag = IsWithin(points[0], box); if (flag) { item = new Point2DCollection(); list.Add(item); item.Add(points[0]); } for (int i = 0; i < (points.Count - 1); i++) { Point2D point = points[i]; Point2D p = points[i + 1]; bool flag2 = IsWithin(p, box); if (flag && flag2) { item.Add(p); } else { if (flag != flag2) { if (flag) { item.Add(this.ClipLineSegment(point, p, box)); } else { item = new Point2DCollection(); list.Add(item); item.Add(this.ClipLineSegment(p, point, box)); item.Add(p); } } else { Rectangle2D box3 = new Rectangle2D(Math.Min(point.X, p.X), Math.Min(point.Y, p.Y), Math.Max(point.X, p.X), Math.Max(point.Y, p.Y)); if (box3.IntersectsWith(box)) { item = new Point2DCollection(); list.Add(item); Point2D point3 = EdgeIntersection(point, p, box.Left, false); Point2D point4 = EdgeIntersection(point, p, box.Right, false); Point2D point5 = EdgeIntersection(point, p, box.Bottom, true); Point2D point6 = EdgeIntersection(point, p, 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); } } } flag = flag2; } } for (int j = list.Count - 1; j >= 0; j--) { Point2DCollection points3 = list[j]; if (points3.Count < 2) { list.RemoveAt(j); } } if (list.Count == 0) { return null; } return list; }