public static ConnectionRoute DrawRouteBetweenShapes( this Canvas InCanvas, Shape Shape1, Shape Shape2) { ConnectionRoute selectedRoute = null; // for each direction from the from shape. var possibleRoutes = new List <ConnectionRoute>(); foreach (var dir in WhichDirectionExt.Directions()) { var side = new ShapeSide(Shape1, dir.ToSide()); var route = new ConnectionRoute(side, Shape2); { var leg = route.DrawInitialLegToShape(); if (leg == null) { continue; } route.AddLeg(leg); } while (true) { if (Shape2.Intersects(route.LastLeg.End)) { break; } var leg = route.DrawLegToShape(); route.AddLeg(leg); } // add the route to the possible routes list. possibleRoutes.Add(route); } // select the route with the fewest number of legs. foreach (var route in possibleRoutes) { if (selectedRoute == null) { selectedRoute = route; } else if (route.LegCount < selectedRoute.LegCount) { selectedRoute = route; } } if (selectedRoute != null) { InCanvas.DrawLinesOfRoute(selectedRoute); Shape1.StoreConnection(selectedRoute, Shape2); Shape2.StoreConnection(selectedRoute, Shape1); } return(selectedRoute); }
/// <summary> /// map out a route from shape1 to shape2, starting from a specific side of shape1. /// </summary> /// <param name="InCanvas"></param> /// <param name="Shape1"></param> /// <param name="Shape2"></param> /// <param name="DepartureSide"></param> /// <param name="Route"></param> public static void DrawRouteBetweenShapes( this Canvas InCanvas, Shape Shape1, Shape Shape2, WhichSide DepartureSide, ConnectionRoute Route) { // vertical and horizontal intersection of the two shapes. var vi = ShapeExt.VerticalIntersect(Shape1, Shape2); var hi = ShapeExt.HorizontalIntersect(Shape1, Shape2); if (DepartureSide == WhichSide.Left) { LineCoordinates coor = null; if (Shape1.IsEntirelyToTheRightOf(Shape2).GetValueOrDefault() && (vi.Length > 0)) { coor = InCanvas.DrawLineBetweenShapes( Shape1, WhichSide.Left, Shape2, WhichSide.Right); } else { // draw a short line from the shape to the next available orbit location // around the from shape. var leg = ConnectionLeg.DrawLegToOrbit(Shape1, DepartureSide); Route.AddLeg(leg); } } }