Distance of two Rectangle Center's Connecting Line Outside of the Rectangles Well excuse me for the long title, i dont really know how to call it.

I would like you to explain me how to calculate the image's red line's length, knowing the rectangles position and dimensions.
Context: I need this for a 2d game collision response since when the length is negative it means not only they are intersecting but also that length is the distance they have to repel each other to stop being collided. If there's an easier way to do this, even better.
 A: 
We have two rectangles one centred at A the the other on B.
If the position of A is $(x_a,y_a)$ and the position of B is $(x_b,y_b)$ We can use Pythagoras to determine the length AB.
$$ AB = \sqrt{(x_b-x_a)^2 + (y_b-y_a)^2}$$
Now we are only interested in the distance DE.  We know AC as its half the width of the rectangle centred on A call it $\frac{W_a}{2}$ and we know EF as its half the width of the rectangle centred on B call it $\frac{W_b}{2}$. We also AG its $x_b-x_a$ 
We can use ratios because for example $\frac{AD}{AB} = \frac{AC}{AG}$
The distance DE is thus:
$$
DE = \sqrt{(x_b-x_a)^2 + (y_b-y_a)^2} \cdot \frac{x_b-x_a - \frac{W_a}{2} - \frac{W_b}{2}}{x_b-x_a}
$$
Given that A and B could be anywhere then you may also need to consider what happens when B is much higher than A so the line between them does not go through the sides of both rectangles but goes out of the top or bottom of one or both of the rectangles instead.  But all these combinations can be solved in a similar way. 
A: Lets Assume X1, Y1, H1, W1 for rec 1's dimensions.
Same for Rec 2.
For the x dimension, if distance between X1 and X2 is less than half of W1 and W2, then they could be collision. 
Same check can be done for Y. If both X and Y dimensions have dimensional-collision, then there is object collision.
So:
If ( abs(X1-X2) < (W1+W2)/2 && abs(Y1-Y2) < (H1+H2)/2 ) then Collision !
I haven't double checked it for all edge cases...
