private static GraphicsPath Connect(GraphicsPath p1, GraphicsPath p2, ConnentType type) { List <PointF> pList = new List <PointF>(100); List <byte> tList = new List <byte>(100); //连接两个路径,将重复点删除 //注意:由于p1可能连续连接两次,所以p1未连接的端点要保持原样。 switch (type) { case ConnentType.FrontAndFront: pList.AddRange(p1.PathPoints); pList.RemoveAt(0); pList.InsertRange(0, p2.PathPoints); pList.Reverse(0, p2.PointCount); tList.AddRange(p1.PathTypes); tList.RemoveAt(0); tList.InsertRange(0, p2.PathTypes); break; case ConnentType.FrontAndBack: pList.AddRange(p2.PathPoints); pList.AddRange(p1.PathPoints); pList.RemoveAt(p2.PointCount); tList.AddRange(p2.PathTypes); tList.AddRange(p1.PathTypes); tList.RemoveAt(p2.PointCount); break; case ConnentType.BackAndFront: pList.AddRange(p1.PathPoints); pList.AddRange(p2.PathPoints); pList.RemoveAt(p1.PointCount); tList.AddRange(p1.PathTypes); tList.AddRange(p2.PathTypes); tList.RemoveAt(p1.PointCount); break; case ConnentType.BackAndBack: pList.AddRange(p2.PathPoints); pList.Reverse(); pList.RemoveAt(0); pList.InsertRange(0, p1.PathPoints); tList.AddRange(p2.PathTypes); tList.RemoveAt(0); tList.InsertRange(0, p1.PathTypes); break; default: break; } return(new GraphicsPath(pList.ToArray(), tList.ToArray())); }
//合并相连的路径 public static IEnumerable <GraphicsPath> ConnectPath(List <GraphicsPath> paths) { //可用路径列表 List <GraphicsPath> openedList = new List <GraphicsPath>(); List <GraphicsPath> closedList = new List <GraphicsPath>(); int count = paths.Count; //包含3个或以上路径的交点列表 List <PointF> multiCross = new List <PointF>(); ConnentType frontType = ConnentType.FrontAndFront; ConnentType backType = ConnentType.BackAndFront; for (int i = 0; i < count; i++) { if (IsClosedPath(paths[i])) { closedList.Add(paths[i]); } else { openedList.Add(paths[i]); } } for (int i = 0; i < openedList.Count; i++) { GraphicsPath p = openedList[i]; PointF begin = p.PathPoints[0]; PointF end = p.PathPoints[p.PointCount - 1]; int frontCount = 0; int backCount = 0; GraphicsPath front = null; GraphicsPath back = null; count = openedList.Count; for (int j = i + 1; j < count; j++) { PointF nBegin = openedList[j].PathPoints[0]; PointF nEnd = openedList[j].PathPoints[openedList[j].PointCount - 1]; if (EqualPointF(begin, nBegin) || EqualPointF(begin, nEnd)) { frontCount++; front = openedList[j]; frontType = EqualPointF(begin, nBegin) ? ConnentType.FrontAndFront : ConnentType.FrontAndBack; } if (EqualPointF(end, nBegin) || EqualPointF(end, nEnd)) { backCount++; back = openedList[j]; backType = EqualPointF(end, nBegin) ? ConnentType.BackAndFront : ConnentType.BackAndBack; } } //交点包含路径大于3,不操作 if (frontCount > 1) { multiCross.Add(begin); } if (backCount > 1) { multiCross.Add(end); } bool hasFront = (frontCount == 1) && (!multiCross.Any(pf => EqualPointF(pf, begin))); bool hasBack = (backCount == 1) && (!multiCross.Any(pf => EqualPointF(pf, end))); bool isSamePath = (front == back); if (hasFront || hasBack) { GraphicsPath newPath = p; if (hasFront && hasBack && isSamePath) { newPath = Connect(newPath, front, frontType); openedList.Remove(front); } else { if (hasFront) { newPath = Connect(newPath, front, frontType); openedList.Remove(front); } if (hasBack) { newPath = Connect(newPath, back, backType); openedList.Remove(back); } } openedList.Remove(p); openedList.Insert(i, newPath); //新路径生成后有新的起始结束点,需要重新计算 i--; } } openedList.AddRange(closedList); return(openedList); }