Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone | Function to find the radius of the sphere ; height and radius cannot be negative ; radius of the sphere ; Driver code
function sphereSide ( h , r ) { if ( h < 0 && r < 0 ) return - 1 ; var R = ( ( h * r * Math . sqrt ( 2 ) ) / ( h + Math . sqrt ( 2 ) * r ) ) / 2 ; return R ; } var h = 5 , r = 6 ; document . write ( sphereSide ( h , r ) . toFixed ( 5 ) ) ;