# stable equilibrium in the system

I'm trying to understand the solution of the following problem.

A solid cube of uniform density and sides of b is in equilibrium on top of a cylinder of radius R. The planes of four sides of the cube are parallel to the axis of the cylinder. The contact between cube and sphere is perfectly rough. Under what conditions is the equilibrium stable or not stable?

the solution of the question can be found here:

I just cannot understand how obtain the height $h(\theta) = (R + \frac{b}{2})\cos(\theta) + R\theta\sin(\theta)$. If someone could explain to me i would be grateful.