After investigating, I was able to find the formula for calculating the radial focus point:
2D rendering tools such as GDI+ and D2D have built-in mechanisms for drawing gradients, and they often present the concept of "focus point" - the 2D point where the gradient radiates away from. Our algorithm for calculating the radial gradient focus point is relative to the anchor rectangle and the focus rectangle. Our PathGradientInfo takes in an anchor rectangle and a focus rectangle for determining the size and position of our inner path. On the other hand, GDI+ takes in a center point and a scaling factor pair, so that the focus path can be determined by scaling the filled path about this center point. We have to convert our rectangles into this representation, and these equations show how: Let P be the required center point and S be the required scaling factors in two dimensions. Let X and X' be the top-left corner of our outer and inner rectangles, respectively, and D and D' be the size of our outer and inner rectangles in two dimensions respectively. Then we must have:
S = D' / D
( X - P ) * S + P = X'
Solving these equations for P,
P = ( X' - S * X ) / ( 1 - S )
= ( D * X' - D' * X ) / ( D - D' )
= X' + D' * ( X' - X ) / ( D - D' )
Therefore, we use this to perform our conversions:
S = D' / D
P = X' + D' * ( X' - X ) / ( D - D' )
Here is a more literal translation of our currently shipping logic, in C++-like pseudo-code. Here, "rcInner" is the focus rect, and "rcOuter" is the circumscribed rectangle around the shape bounds.
Point2D ITech::CalculateGradientOrigin(const Rect& rcInner, const Rect& rcOuter)
{
Point2D gradientOrigin = Point2D(rcInner.left, rcInner.top);
Vector2D innerRectSize = rcInner.GetSize();
Vector2D outerRectSize = rcOuter.GetSize();
if( innerRectSize.dx > 0.0 )
{
double widthDiff = outerRectSize.dx - innerRectSize.dx;
if(NotEqualToZero(widthDiff)) // This helper function performs comparison using tolerance of 2 * DBL_EPSILON
gradientOrigin.x += innerRectSize.dx * (rcInner.left - rcOuter.left) / widthDiff;
}
if( innerRectSize.dy > 0.0 )
{
double heightDiff = outerRectSize.dy - innerRectSize.dy;
if(NotEqualToZero(heightDiff))
gradientOrigin.y += innerRectSize.dy * (rcInner.top - rcOuter.top) / heightDiff;
}
return gradientOrigin;
}