\documentclass[border=10pt]{standalone}
\usepackage[inline]{asymptote}
\begin{document}
\begin{asy}[width=\the\linewidth,inline=true]
import graph3;
currentprojection=orthographic(-5,-4,2,center=true);
guide3 sphere_x_cyl(real a, real r, real R, int n=10){
// return a closed curve of the Sphere–cylinder intersection (top part)
// only for the case when a cylinder is completely inside
// except special case when the x-shift a=0
assert(a>0,"***** Positive a expected.");
real b=(R^2-r^2-a^2)/(2a);
guide3 g;
int n=30;
real phi, dphi;
phi=0;
dphi=360/n;
triple t;
for(int i=0;i
Source: TeX.SE
Author: g.kov (License)