# Finding radius of capsule defined by total volume and height of inner cylinder

I have a capsule shape that is comprised of a cylinder and two half-sphere end caps, and I want to dynamically resize it. As I stretch the capsule by increasing/decreasing the height of the cylinder, the total volume should remain constant by adjusting the radius. This means, I need to define the radius in terms of height and volume.

My starting equation looks like this:

Total volume = Sphere volume + Cylinder volume

$$W_v = \frac{4}{3}\pi r^3 + h \pi r^2$$

Given this equation, I don't know how to solve for radius (r).

Am I approaching this from the right direction? It seems that, when it comes to weird circle problems, or pi in general, it often comes down to integrating/derivating(??) a solution. I know basic Calc, but I can't see how to use it here. Also, since this is for a computer game and needs to run in realtime, I'm really hoping the solution isn't too ... problematic. ;) I know I can fudge an okay representation, but if the real solution is elegant, I'd rather have that. Plus after circling poor attempts to factor and simplify for a while, I'm genuinely curious to know what the answer is now.

## Non-Complex Solution

The math involved for solving for $r$ goes a bit beyond basic calculus, I can spare you the long explanations and tell you that $V = \frac{4}{3}\pi r^3+h\pi r^2$ is solved for $r$ as:

$$\frac{1}{4} \left(-\frac{\sqrt[3]{4 \sqrt{3} \sqrt{12 V^2-\pi h^3 V}+\pi h^3-24 V}}{\sqrt[3]{\pi }}-\frac{\sqrt[3]{\pi } h^2}{\sqrt[3]{4 \sqrt{3} \sqrt{12 V^2-\pi h^3 V}+\pi h^3-24 V}}-h\right)$$

That's the only solution not involving complex numbers.

## Complex Soltution

The solution(s) involving complex numbers are:

$$r = \frac{\left(1 \mp i \sqrt{3}\right) \sqrt[3]{4 \sqrt{3} \sqrt{12 V^2-\pi h^3 V}+\pi h^3-24 V}}{8 \sqrt[3]{\pi }}+\frac{\sqrt[3]{\pi } \left(1 \pm i \sqrt{3}\right) h^2}{8 \sqrt[3]{4 \sqrt{3} \sqrt{12 V^2-\pi h^3 V}+\pi h^3-24 V}}-\frac{h}{4}$$

Note that this is two separate solutions due to the flipping of the signs. Also notice the formatting of the "minus-plus sign" and the "plus-minus sign". This means that when $\mp$ is negative, $\pm$ is positive. When $\mp$ is positive, $\pm$ is negative.

## Graphical Representation of the Functions

I thought it'd be best to represent the data graphically.

The three functions are labeled in these representations as:

• $f(h,V) = \frac{1}{4} \left(-\frac{\sqrt[3]{4 \sqrt{3} \sqrt{12 V^2-\pi h^3 V}+\pi h^3-24 V}}{\sqrt[3]{\pi }}-\frac{\sqrt[3]{\pi } h^2}{\sqrt[3]{4 \sqrt{3} \sqrt{12 V^2-\pi h^3 V}+\pi h^3-24 V}}-h\right)$
• $f\text{complex1}(h,V) = \frac{\left(1-i \sqrt{3}\right) \sqrt[3]{4 \sqrt{3} \sqrt{12 V^2-\pi h^3 V}+\pi h^3-24 V}}{8 \sqrt[3]{\pi }}+\frac{\left(1+i \sqrt{3}\right) \sqrt[3]{\pi } h^2}{8 \sqrt[3]{4 \sqrt{3} \sqrt{12 V^2-\pi h^3 V}+\pi h^3-24 V}}-\frac{h}{4}$
• $f\text{complex2}(h,V) = \frac{\left(1+i \sqrt{3}\right) \sqrt[3]{4 \sqrt{3} \sqrt{12 V^2-\pi h^3 V}+\pi h^3-24 V}}{8 \sqrt[3]{\pi }}+\frac{\left(1-i \sqrt{3}\right) \sqrt[3]{\pi } h^2}{8 \sqrt[3]{4 \sqrt{3} \sqrt{12 V^2-\pi h^3 V}+\pi h^3-24 V}}-\frac{h}{4}$

All three functions, transposed onto a single $3D$ graph looks like so:

To further represent the data, we can visualize the functions transforming when we put them into the form $f\left(x \cdot h, \; y \cdot V\right)$:

Note that in this gif, $h$ and $V$ are both running from $-10$ to $10$.

• That's a spectacular equation! I knew in the back of my mind it wasn't going to be elegant, but I was still hoping. Probably not going to make the cut when it comes time for benchmarking performance, though this will give me a great guideline for faking it. :D (I promise I hit the up arrows on both answers, but still new..) – Plystire May 10 '18 at 0:18
• After implementing the equation and testing it, I found that there's an issue when pi*h^3 > 12V ... there becomes a sqrt negative and the math operation fails. – Plystire May 21 '18 at 5:03
• @Plystire like I said tho, there are other solutions to the problem that involve complex numbers – Joseph Eck May 21 '18 at 5:07
• After further investigation, it seems that if this equation is correct, it only applies to small range. With V = 1, h = 1.6 there's that problem. The only proof I could provide to this equation being correct was when h = 0, I'd get out the radius used to calculate the volume put in. Even if the rest of the answer involves complex numbers, can you please post it for completion sake? – Plystire May 21 '18 at 5:25
• @Plystire check it out now – Joseph Eck May 21 '18 at 18:20

Based on what I understood, you are asking what adjustments must be made to the the radii of the half spheres and the cylinder in order to maintain the volume while changing the height of the cylinder. In effect, you want: $$\pi (\alpha +h) (r-\epsilon )^2+\frac{4}{3} \pi (r-\epsilon )^3=V_T\tag{1}$$ Where $r$ is the radius of the cylinder and the sphere, $\epsilon$ is the change in the radius while changing height $h$ by adding $\alpha$. So we want to solve for $\epsilon$ here, which gives us one real answer as: $$\epsilon \to \frac{-9 \pi ^{4/3} \sqrt[3]{-\pi \alpha ^3-\pi h^3-3 \pi \alpha h^2-3 \pi \alpha ^2 h+4 \sqrt{3} \sqrt{V_T \left(-\left(\pi (\alpha +h)^3-12 V_T\right)\right)}+24 V_T}-\frac{9 \pi ^2 (\alpha +h)^2}{\sqrt[3]{-\pi \alpha ^3-\pi h^3-3 \pi \alpha h^2-3 \pi \alpha ^2 h+4 \sqrt{3} \sqrt{V_T \left(-\left(\pi (\alpha +h)^3-12 V_T\right)\right)}+24 V_T}}+9 \pi ^{5/3} (\alpha +h+4 r)}{36 \pi ^{5/3}}\tag{2}$$

To check, suppose originally, we have $$h\to 1\,\,r\to 3\,\,\epsilon \to 0\,\,\alpha \to 0$$ Which gives a total volume from $(1)$ of $$V_T=45\pi$$ Now we want to change the height by $2$, thus $\alpha=2$, now we need to solve $(2)$ using: $$\{\alpha \to 2,h\to 1,r\to 3,V\to 45 \pi\}$$ And we get: $$\epsilon \to \frac{-9 \pi ^{4/3} \sqrt[3]{108 \pi \sqrt{95}+1053 \pi }+135 \pi ^{5/3}-\frac{81 \pi ^2}{\sqrt[3]{108 \pi \sqrt{95}+1053 \pi }}}{36 \pi ^{5/3}}$$ And now substitute everything into $(1)$, we get back $V_T=45\pi$

• Ahh, when you say it like that, I can see this direction making a lot of sense. Though, looking at the answer, I don't think it's the most efficient. If the equation Joseph posted is right (I am not (yet) smart enough to proof it), I should be able to take the current radius and subtract new radius to find ϵ, right? Since that's still a shorter equation, and his is more direct in finding the new radius, I've selected his for the answer. Though I really appreciate your help and insight! Thank you :) – Plystire May 10 '18 at 0:16