/// <summary> /// Find the best <see cref="PortSide"/> according to the position of the port. /// </summary> /// <remarks> /// The orientation is not rotated, i.e. <paramref name="bottomLeft"/> is always the anchor /// of the oriented rectangle. /// </remarks> /// <param name="point">The port position.</param> /// <param name="bottomLeft">The bottom left corner of the oriented rectangle.</param> /// <param name="bottomRight">The bottom right corner.</param> /// <param name="topLeft">The top left corner.</param> /// <param name="topRight">The top right corner.</param> /// <returns>The side to which the given port is closest.</returns> private static PortSide FindBestSide(PointD point, PointD bottomLeft, PointD bottomRight, PointD topLeft, PointD topRight) { // determine the distances to the sides of the oriented rectangle // with a small penalty to the left and right side. var distToBottom = point.DistanceToSegment(bottomLeft, bottomRight); var distToTop = point.DistanceToSegment(topLeft, topRight); var distToLeft = point.DistanceToSegment(topLeft, bottomLeft) * 1.05; var distToRight = point.DistanceToSegment(topRight, bottomRight) * 1.05; PortSide side; if (distToTop <= distToBottom) { if (distToTop <= distToLeft) { side = distToTop <= distToRight ? PortSide.North : PortSide.East; } else { side = distToLeft < distToRight ? PortSide.West : PortSide.East; } } else if (distToBottom <= distToLeft) { side = distToBottom <= distToRight ? PortSide.South : PortSide.East; } else { side = distToLeft < distToRight ? PortSide.West : PortSide.East; } return(side); }
/// <summary> /// Determines the index of the segment in which the bend was created. /// </summary> private int DetermineBendSegmentIndex(List <PointD> points, PointD location) { int closestIndex = -1; double minDist = Double.MaxValue; for (int i = 0; i < points.Count - 1; i++) { double dist = location.DistanceToSegment(points[i], points[i + 1]); if (dist < minDist) { closestIndex = i; minDist = dist; } } return(closestIndex); }