I am trying to find the angle between two rectangles when there is a known gap between them.
See this diagram:

I have simplified the problem into three triangles, two of which are the same.
Here is my attempted solution:
$$ \theta = 2\alpha + \beta $$ Finding $\alpha$, this is the angle of a right angle so I can just use $tan$ $$ \begin{align} \tan\alpha &= \frac{a}{2r}\\ \therefore \alpha &= \arctan{\frac{a}{2r}} \end{align} $$
That's easy, when I test $\alpha$ seems reasonable.
Finding $\beta$, this isn't a right angle triangle so I use the cosine rule as I know all three sides:
$$ \begin{align} b^2 &= 2r^2 + 2r^2\cos\beta\\ b^2 - 2r^2 &= 2r^2\cos\beta\\ \frac{b^2 - 2r^2}{2r^2} &= \cos\beta\\ \frac{b^2}{2r^2} - 1 &= cos\beta\\ \therefore\beta &= \arccos\left(\frac{b^2}{2r^2} - 1\right) \end{align} $$
When I now check my $\beta$ value with some reasonable numbers I get an unreasonable angle?
For example: $$ \begin{aligned} \text{let } b &= 4\\ r &= 128\\ \\ \therefore\beta &= \arccos\left(\frac{4^2}{2\times128^2} - 1\right)\\ &\approx 0.99\pi \end{aligned} $$
Why am I getting such a big angle, I expect it to be more like $0.01\pi$!
Update
The comments highlighted a mistake in my diagram, the lengths around $\beta$ aren't of length $r$.
So my new formula for $\beta$ is:
$$ \begin{align} r_2^2 &= \left(\frac{a}{2}\right)^2 + r^2\\ \therefore r_2 &= \sqrt{\left(\frac{a}{2}\right)^2 + r^2}\\ \\ \beta &= \arccos\left(\frac{b^2}{2\left(\frac{a}{2}^2 + r^2\right)} - 1\right)\\ &= \arccos\left(\frac{2b^2}{w^2+4d^2} - 1\right) \end{align} $$
But I still get a ridiculous result!
Assuming that the line originating at the point $X$ bisects the line of lenght $a$ of the rectangle in a right angle and that we know $r$, $a$ and $b$. We can calculate $r_{2}$ (like you did) using Pythagoras' Theorem. Now consider the triangle with the sides $b$, $r_{2}$ and $r_{2}$ in your sketch. Then the line which meets $b$ in a right angle and goes through $X$ bisects $b$ (it is just the height line because our triangle is equal-sided). So \begin{equation*} \sin{\frac{\beta}{2}} = \frac{\frac{b}{2}}{r_{2}}\end{equation*} and hence \begin{equation*} \beta = 2 \cdot \arcsin{\frac{b}{2 \cdot r_{2}}} = 2 \cdot \arcsin{\frac{b}{2 \cdot \sqrt{r^{2} + \left(\frac{a}{2}\right)^{2}}}}\end{equation*}