/// <summary> /// Finds the nearest node to a starting node, in the desired direction</summary> /// <param name="startNode">Node to measure distance from</param> /// <param name="arrow">Direction (Keys.Up, Keys.Right, Keys.Down, or Keys.Left)</param> /// <param name="nearestRect">Nearest node's rectangle, or an empty rectangle if no /// nearest node is found</param> /// <returns>The nearest node or null if there is none in that direction</returns> protected virtual TNode FindNearestElement(TNode startNode, Keys arrow, out Rectangle nearestRect) { TNode nearest = null; int bestDist = int.MaxValue; var pickingAdapter = AdaptedControl.Cast <IPickingAdapter2>(); nearestRect = new Rectangle(); Rectangle startRect = pickingAdapter.GetBounds(new[] { startNode }); foreach (TNode node in m_graph.Nodes) { if (node != startNode) { Rectangle targetRect = pickingAdapter.GetBounds(new[] { node }); int dist = WinFormsUtil.CalculateDistance(startRect, arrow, targetRect); if (dist < bestDist) { bestDist = dist; nearest = node; nearestRect = targetRect; } } } return(nearest); }