GetEdgeEndpointOnRectangle
(
Point oVertexALocation,
Rect oVertexARectangle,
Point oVertexBLocation,
out Point oEdgeEndpoint
)
{
AssertValid();
if (oVertexALocation == oVertexBLocation)
{
oEdgeEndpoint = oVertexALocation;
return;
}
Double dVertexAX = oVertexALocation.X;
Double dVertexAY = oVertexALocation.Y;
Double dVertexBX = oVertexBLocation.X;
Double dVertexBY = oVertexBLocation.Y;
Double dHalfVertexARectangleWidth = oVertexARectangle.Width / 2.0;
Double dHalfVertexARectangleHeight = oVertexARectangle.Height / 2.0;
// Get the angle between vertex A and vertex B.
Double dEdgeAngle = WpfGraphicsUtil.GetAngleBetweenPointsRadians(
oVertexALocation, oVertexBLocation);
// Get the angle that defines the aspect ratio of vertex A's rectangle.
Double dAspectAngle = Math.Atan2(
dHalfVertexARectangleHeight, dHalfVertexARectangleWidth);
if (dEdgeAngle >= -dAspectAngle && dEdgeAngle < dAspectAngle)
{
// For a square, this is -45 degrees to 45 degrees.
Debug.Assert(dVertexBX != dVertexAX);
oEdgeEndpoint = new Point(
dVertexAX + dHalfVertexARectangleWidth,
dVertexAY + dHalfVertexARectangleWidth *
( (dVertexBY - dVertexAY) / (dVertexBX - dVertexAX) )
);
return;
}
if (dEdgeAngle >= dAspectAngle && dEdgeAngle < Math.PI - dAspectAngle)
{
// For a square, this is 45 degrees to 135 degrees.
Debug.Assert(dVertexBY != dVertexAY);
oEdgeEndpoint = new Point(
dVertexAX + dHalfVertexARectangleHeight *
( (dVertexBX - dVertexAX) / (dVertexAY - dVertexBY) ),
dVertexAY - dHalfVertexARectangleHeight
);
return;
}
if (dEdgeAngle < -dAspectAngle && dEdgeAngle >= -Math.PI + dAspectAngle)
{
// For a square, this is -45 degrees to -135 degrees.
Debug.Assert(dVertexBY != dVertexAY);
oEdgeEndpoint = new Point(
dVertexAX + dHalfVertexARectangleHeight *
( (dVertexBX - dVertexAX) / (dVertexBY - dVertexAY) ),
dVertexAY + dHalfVertexARectangleHeight
);
return;
}
// For a square, this is 135 degrees to 180 degrees and -135 degrees to
// -180 degrees.
Debug.Assert(dVertexAX != dVertexBX);
oEdgeEndpoint = new Point(
dVertexAX - dHalfVertexARectangleWidth,
dVertexAY + dHalfVertexARectangleWidth *
( (dVertexBY - dVertexAY) / (dVertexAX - dVertexBX) )
);
}