Well let’s see, assuming your box is axis aligned…….
I guess I would first take the length of the line by Pythagorean. So
X_delta = (point.x – center.x)
Y_delta = (point.y – center.y)
dist_to_center = sqrt(x_delta^2 + y_delta^2)
Then I would take the aspect ratio of the box. So:
box_ratio = box_width / box_height
So now you can use your X and Y deltas to find the aspect ratio of your point to center
point_ratio = abs(x_delta / y_delta)
So this gives you a ratio that you can compare with your box ratio to see which edge you want to calculate against. If your point ratio is greater than the box ratio you calculate against the left/right ends. Otherwise you calculate against the top/bottom. If you really do this, you might need to take care divide by zero somehow and just set the result to some big number.
So now you can find a factor to multiply the original length of the line by. So guess that would be something like
If (point_ratio > box_ratio) {
factor = (box_width / 2) / abs(x_delta).
} else {
factor = (box_height / 2) / abs(y_delta)
}
Now you can find the length of the line inside the box:
dist_internal = dist_to_center * factor;
And then:
dist_external = dist_to_center - dist_internal
Anyway I think this works, there might be a better way though and hopefully I didn’t mess up anywhere.