Distance from center of a 2d capsule based on angle First of all, I am not a native speaker nor a mathematician, so please excuse if I don't properly phrase things.
I am wondering what the simplest solution to this problem is:
Given a capsule (usually 3D, lets project it on a plane though, cut through the center) with a "rectangle" width of a and a radius of the semi circles of b. How can I calculate the distance z to a point on the edge of the capsule from the center given an angle α, where an angle of zero degrees would be straight up (and thus return the radius, as that is half the height of the rectangle)?
Here is my best paint drawing skills to illustrate what I am talking about for you:

It's obviously easy if it was only a rectangle, especially the semi circles give me troubles and I have no clue how to tackle that. 
Thanks for you help!
 A: Consider the below diagram:

Now, let $\alpha$ = CA
Also notice thata triangle ACD, DFG are simiar, both have 90 degree angles and $\angle FGD = \angle CGA$.
Hence $\frac{CA}{FD} = \frac{CG}{GD}$
The equation of the circle is:
$$x^2+y^2=r^2 $$
where r is the radius of the circle
Now if $\angle ACG = \alpha$, the equation of the line CG is,
$$y = tan(\alpha)(x+b)$$
Here b is the length of CA.
If we substitute this into the equation of our circle, and solve the quadratic equation, we get:
$$x = \frac{ \pm \sqrt{-b^2 tan^2(a) + r^2 tan^2(a) + r^2} - b .tan^2(a)}{tan^2(a) + 1} $$
Consider only the positive case,
Hence we now know what the value of $FD$ is, also $CG = \frac{b}{\cos{(\alpha)}}$
Due to simlarity,
$$\frac{CG}{CA}=\frac{GD}{FD}$$
So,
$$GD = \frac{CG.FD}{CA} = \frac{\frac{b}{\cos{(\alpha)}}.\frac{ \sqrt{-b^2 tan^2(a) + r^2 tan^2(a) + r^2} - b .tan^2(a)}{tan^2(a) + 1}}{b} $$
Now,$$ CD = CG+GD = \frac{b}{\cos{(\alpha)}}.(1+\frac{\frac{ \sqrt{-b^2 tan^2(a) + r^2 tan^2(a) + r^2} - b .tan^2(a)}{tan^2(a) + 1}}{b})$$
To test this, let's try the diagram,
$$\alpha = 20.1669886607522$$
$$b = 3$$
$$r = 2$$
Substituting this into the equation,

Which is accurate.
For your question, all you would need to do is take CA = half the width of the rectangle, $\alpha$ as 90-the alpha in your pic, r as the radius.
The equation can be simplified to:
$$ CD = \cos{\alpha} . (b+\sqrt{r^2+(r-b)(r+b) \tan^2({\alpha})})$$
This works for the opposite side too:


Now, for the general equation for any angle,
$$\theta = \tan^{-1}(\frac{a}{2r})$$
$a$ is the width of the rectangle of the capsule
$r$ is the radius of the semi-circles
$b = a/2$
$\beta = 90 - \alpha $
$$
  z = \left\{
\begin{array}{ll}
      |\cos{\beta} . (b+\sqrt{r^2+(r-b)(r+b) \tan^2({\beta})})| & \theta \leq \alpha \leq 180- \theta, 180+ \theta \leq \alpha \leq 360 - \theta \\
      |\frac{r}{cos(\alpha)}| & \text{for all other } \alpha \\
\end{array} 
\right. 
$$
A: Steps (using basic coordinate geometry) : 


*

*Assume origin at center of rectangle and horizontal (usual) x axis. This gives center of the semicircle on right side as (a/2, 0).

*Get equation of circle by that center and radius b.

*Get equation of line by angle π/2 - $\alpha$ and origin as a point on line.

*Get intersection(s). Choose the one which lies to the right of circle's center.

*Get distance between intersection point and center.

