I am drawing a custom business object diagram using .NET GDI +. Among other things, a diagram consists of several lines connecting objects.
In a specific scenario, I need to shorten the line by a certain number of pixels, say, 10 pixels, that is, find a point on the line that lies 10 pixels earlier than the end point of the line.
Imagine a circle with a radius of r = 10 pixels and a line with a starting point (x1, y1) and an ending point (x2, y2). The circle is centered at the end point of the line, as shown in the following figure.

How to calculate the point marked with a red circle, that is, the intersection between the circle and the line? This will give me a new endpoint for the line, cutting it by 10 pixels.
Decision
, . "LengthenLine", , .
, , , .
public void LengthenLine(PointF startPoint, ref PointF endPoint, float pixelCount)
{
if (startPoint.Equals(endPoint))
return;
double dx = endPoint.X - startPoint.X;
double dy = endPoint.Y - startPoint.Y;
if (dx == 0)
{
if (endPoint.Y < startPoint.Y)
endPoint.Y -= pixelCount;
else
endPoint.Y += pixelCount;
}
else if (dy == 0)
{
if (endPoint.X < startPoint.X)
endPoint.X -= pixelCount;
else
endPoint.X += pixelCount;
}
else
{
double length = Math.Sqrt(dx * dx + dy * dy);
double scale = (length + pixelCount) / length;
dx *= scale;
dy *= scale;
endPoint.X = startPoint.X + Convert.ToSingle(dx);
endPoint.Y = startPoint.Y + Convert.ToSingle(dy);
}
}