如何绘制以下内部带有圆柱体的球体?

如何绘制以下内部带有圆柱体的球体?

我想用 tikz 绘制下面的图片?

在此处输入图片描述

为此我使用了代码

https://tex.stackexchange.com/a/597008/241755

发布但我希望 x 轴从原点到右。这不仅仅是绘制轴的问题,也是这些坐标中的计算问题。(我不需要链接帖子的顶部圆柱体

附言我知道我的帖子有点“请帮我做点什么”的意思。对此我很抱歉。

在此处输入图片描述

答案1

正如我在评论中尝试解释的那样,您链接到的答案经过调整,使得在屏幕坐标中,圆的最高点与球的最高点重合。更具体地说,答案是调整圆柱和球体半径的比率,使得这些点在 中重合isometric view。以下调整视角,使得这些点在给定圆柱半径r和球体半径的情况下重合R。这允许您将 x 轴指向您想要的任何方向。

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\begin{tikzpicture}[declare function={R=3;% radius of sphere
        r=1.5;% radius of cylinder
    }]
    \pgfmathsetmacro{\myalpha}{asin(r/R)}
    \pgfmathsetmacro{\myh}{R*cos(\myalpha)}
    \tdplotsetmaincoords{90-\myalpha}{0}
    \begin{scope}[tdplot_main_coords]
      \begin{scope}  
        \clip (0,0,\myh) circle[radius=r];
        \path[left color=gray!50!black,right color=gray,middle color=gray!20] (r/2,0,\myh) circle[radius=1.5*r];;  
      \end{scope}
      \draw[ball color=gray,even odd rule]  (0,0,\myh) circle[radius=r]
        [tdplot_screen_coords] (0,0) circle[radius=R];  
    \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

但是,很明显这不是最普遍的情况,在这种情况下,您可以独立地选择半径和视角。这就是以下代码所做的。

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\begin{tikzpicture}[declare function={R=3;% radius of sphere
        r=1.5;% radius of cylinder
        theta=75;% view angle
    }]
    \pgfmathsetmacro{\myalpha}{asin(r/R)}
    \pgfmathsetmacro{\myh}{R*cos(\myalpha)}
    \pgfmathtruncatemacro{\iflag}{ifthenelse(theta>90-\myalpha,1,0)}
    \ifnum\iflag=0\relax
        \path[ball color=gray] (0,0) circle[radius=R];          
    \else
        \pgfmathsetmacro{\myhprime}{\myh*sin(theta)+r*cos(theta)}
        \pgfmathsetmacro{\mytcrit}{-asin((r*cos(theta)*\myhprime)/(r*r-\myhprime*\myhprime))}
        \pgfmathsetmacro{\mybeta}{90-atan2(\myhprime+r*cos(theta)*sin(\mytcrit),r*cos(\mytcrit))}
        \pgfmathsetmacro{\mytcrit}{\mytcrit/cos(theta)}
        \path[ball color=gray]  (90-\mybeta:R) arc[start angle=90-\mybeta,end angle=-270+\mybeta,radius=R];
    \fi
    \tdplotsetmaincoords{theta}{0}
    \begin{scope}[tdplot_main_coords]
        \draw[dashed] (0,0,-\myh) circle[radius=r] (-r,0,\myh) edge (-r,0,-\myh) (r,0,\myh) edge (r,0,-\myh);
        \clip (0,0,\myh) circle[radius=r];
        \path[left color=gray!50!black,right color=gray,middle color=gray!20] (r/2,0,\myh) circle[radius=1.5*r];
        \fill (0,0,-\myh) circle[radius=r];  
    \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

您可以设置视角theta来更改视图。由于绕 z 轴旋转对称,另一个视角是多余的。此版本还在底部截断了球体。

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\newcommand{\DrawSphereInCylinder}[1]{\begin{tikzpicture}[declare function={R=3;% radius of sphere
        r=1.5;% radius of cylinder
        theta=#1;% view angle
    }]
    \pgfmathsetmacro{\myalpha}{asin(r/R)}
    \pgfmathsetmacro{\myh}{R*cos(\myalpha)}
    \pgfmathtruncatemacro{\iflag}{ifthenelse(theta>90-\myalpha,1,0)}
    \tdplotsetmaincoords{theta}{0}
    \ifnum\iflag=0\relax
        \path[ball color=gray] (0,0) circle[radius=R];          
    \else
        \pgfmathsetmacro{\myhprime}{\myh*sin(theta)+r*cos(theta)}
        \pgfmathsetmacro{\mytcrit}{-asin((r*cos(theta)*\myhprime)/(r*r-\myhprime*\myhprime))}
        \pgfmathsetmacro{\mybeta}{90-atan2(\myhprime+r*cos(theta)*sin(\mytcrit),r*cos(\mytcrit))}
        \pgfmathsetmacro{\mytcrit}{asin((cos(theta)*\myhprime)/(r*sin(theta)*sin(theta)))}
        \path[ball color=gray]  (90-\mybeta:R) arc[start angle=90-\mybeta,end angle=-90+\mybeta,radius=R]
        [tdplot_main_coords] 
        -- ({r*cos(-\mytcrit)},{-r*sin(\mytcrit)},-\myh)
        arc[start angle=-\mytcrit,end angle=-180+\mytcrit,radius=r]
        [tdplot_screen_coords]
        -- (-90-\mybeta:R) arc[start angle=-90-\mybeta,end angle=-270+\mybeta,radius=R];
    \fi
    \begin{scope}[tdplot_main_coords]
        \ifnum\iflag=0\relax
            \draw[dashed] (0,0,-\myh) circle[radius=r] (-r,0,\myh) edge (-r,0,-\myh) (r,0,\myh) edge (r,0,-\myh);
        \else 
            \draw[dashed] ({r*cos(-\mytcrit)},{-r*sin(\mytcrit)},-\myh)
            arc[start angle=-\mytcrit,end angle=180+\mytcrit,radius=r]
            (-r,0,\myh) edge (-r,0,-\myh) (r,0,\myh) edge (r,0,-\myh);
        \fi    
        \clip (0,0,\myh) circle[radius=r];
        \path[left color=gray!50!black,right color=gray,middle color=gray!20] (r/2,0,\myh) circle[radius=1.5*r];
        \fill[white] (0,0,-\myh) circle[radius=r];  
    \end{scope}
    \path (current bounding box.north) node[above]{$\theta={#1}^\circ$};
\end{tikzpicture}}
\begin{document}
\begin{tabular}{cc}
    \DrawSphereInCylinder{85} & \DrawSphereInCylinder{70}\\    
    \DrawSphereInCylinder{55} & \DrawSphereInCylinder{40}\\    
    \DrawSphereInCylinder{25} & \DrawSphereInCylinder{10}\\    
\end{tabular}
\end{document}

在此处输入图片描述

相关内容