tikz-3dplot用颜色填充内圈

tikz-3dplot用颜色填充内圈

我将如何为这个超球面的内圆着色在此处输入图片描述

该图的代码如下:

\documentclass[tikz,border=5mm]{standalone}     % <<< added
\usepackage{tikz-3dplot}                        % <<< added
\tdplotsetmaincoords{60}{115}                   % <<< added
\begin{document}                                % <<< added
\begin{tikzpicture}[tdplot_main_coords, scale = 2.5]
\coordinate (P) at ({-1},{0},{0});
\coordinate (P1) at ({sqrt(2)/2},{sqrt(2)/2},0);
    
\shade[ball color = lightgray, opacity = 0.5] (0,0,0) circle (1cm);       
    
\tdplotsetrotatedcoords{0}{0}{0};
\draw[dashed, tdplot_rotated_coords, gray] (0,0,0) circle (1);
    
\tdplotsetrotatedcoords{90}{90}{90};
\draw[dashed, tdplot_rotated_coords, gray] (1,0,0) arc (0:180:1);
    
\tdplotsetrotatedcoords{0}{90}{90};
\draw[dashed, tdplot_rotated_coords, gray] (1,0,0) arc (0:180:1);
    
\draw[dashed, gray] (0,0,0) -- (-1,0,0);
\draw[dashed, gray] (0,0,0) -- (0,-1,0);
    
\draw[-stealth] (0,0,0) -- (1.80,0,0) node[below left] {$x$};
\draw[-stealth] (0,0,0) -- (0,1.30,0) node[below right] {$y$};
\draw[-stealth] (0,0,0) -- (0,0,1.30) node[above] {$z$};
\draw[thick, -stealth] (0,0,0) -- (P) node[right] {$P_i$};
\draw[thick, -stealth] (0,0,0) -- (P1) node[right] {$P_f$};
    
\draw[fill = lightgray!50] (P) circle (.5pt);
\draw[fill = lightgray!50] (P1) circle (.5pt);
\end{tikzpicture}
\end{document}             % <<< added

答案1

看起来,你的一些虚线不正确。

\documentclass[tikz,border=1cm]{standalone}
\usetikzlibrary{calc,3dtools}% https://github.com/marmotghost/tikz-3dtools
\begin{document}
    \begin{tikzpicture}[line join=round, 
        line cap=round,3d/install view={phi=120,theta=70},c/.style={circle,fill,inner sep=1pt},
        declare function={R=4;h=2;r=sqrt(R*R-h*h;}]
        \path
        (0,0,0) coordinate (O)
        pic{3d/circle on sphere={R=R,P={(O)},n={(0,0,1)}}};
        \begin{scope}[canvas is xz plane at y=0]
        \draw[3d/hidden] (R,0) arc[start angle=0,end angle=180,radius=R];
    \end{scope} 
\begin{scope}[canvas is yz plane at x=0]
    \draw[3d/hidden] (R,0) arc[start angle=0,end angle=180,radius=R];
\end{scope} 
     \shade[ball color=white,3d/screen coords,opacity=0.7] (O) circle[radius=R];
     \draw[3d/hidden]  (0,0,0) -- (0,0,R) (O)--(R,0,0) (O)--(0,R,0); 
     \draw[3d/visible, -latex] (R,0,0) -- (R + 4,0,0) node[right]{$x$};
     \draw[3d/visible, -latex] (0,R,0) -- (0,R + 1,0) node[right]{$y$};
     \draw[3d/visible, -latex] (0,0,R) -- (0,0,R + 1) node[above]{$z$};
        \path foreach \p/\g in {O/-90}
        {(\p)node[c]{}+(\g:2.5mm) node{$\p$}};
    \end{tikzpicture}   

    \begin{tikzpicture}[line join=round, 
    line cap=round,3d/install view={phi=120,theta=70},c/.style={circle,fill,inner sep=1pt},
    declare function={R=4;h=2;r=sqrt(R*R-h*h;}]
    \path
    pic{3d/circle on sphere={R=R,P={(O)},n={(0,0,1)}}}
    pic{3d/circle on sphere={R=R,P={(O)},n={(1,0,0)}}}
    pic{3d/circle on sphere={R=R,P={(O)},n={(0,1,0)}}}
    ;

    \shade[ball color=white,3d/screen coords,opacity=0.7] (O) circle[radius=R];
    \draw[3d/hidden]  (0,0,0) -- (0,0,R) (O)--(R,0,0) (O)--(0,R,0); 
    \draw[3d/visible, -latex] (R,0,0) -- (R + 4,0,0) node[right]{$x$};
    \draw[3d/visible, -latex] (0,R,0) -- (0,R + 1,0) node[right]{$y$};
    \draw[3d/visible, -latex] (0,0,R) -- (0,0,R + 1) node[above]{$z$};
    \path foreach \p/\g in {O/-90}
    {(\p)node[c]{}+(\g:2.5mm) node{$\p$}};
\end{tikzpicture}   
\end{document}

在此处输入图片描述

在此处输入图片描述

我懂了这个答案在球体上绘制圆弧。

   \documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{3dtools}% https://github.com/marmotghost/tikz-3dtools
\makeatletter
\tikzset{3d/point on sphere/.code={\tikzset{3d/.cd,#1,
            /tikz/insert path={
                ({\pgfkeysvalueof{/tikz/3d/R}*cos(\pgfkeysvalueof{/tikz/3d/longitude})*cos(\pgfkeysvalueof{/tikz/3d/latitude})},
                {\pgfkeysvalueof{/tikz/3d/R}*sin(\pgfkeysvalueof{/tikz/3d/longitude})*cos(\pgfkeysvalueof{/tikz/3d/latitude})},
                {\pgfkeysvalueof{/tikz/3d/R}*sin(\pgfkeysvalueof{/tikz/3d/latitude})})}}},
    3d/latitude/.initial=0,3d/longitude/.initial=0,
    3d/arcs/O/.initial={(0,0,0)},
    3d/arcs/n/.initial={(0,0,1)},
    3d/arcs/A/.initial={(1,0,0)},
    3d/arcs/B/.initial={(0,1,0)},
    pics/3d/great circle arc/.style={code={%
            \tikzset{3d/arcs/.cd,#1}%
            \def\pv##1{\pgfkeysvalueof{/tikz/3d/arcs/##1}}%
            \pgfmathsetmacro{\pgfutil@tmpa}{tddistance("\pgfkeysvalueof{/tikz/3d/arcs/A}","\pgfkeysvalueof{/tikz/3d/arcs/O}")}%
            \pgfmathsetmacro{\pgfutil@tmpb}{tddistance("\pgfkeysvalueof{/tikz/3d/arcs/B}","\pgfkeysvalueof{/tikz/3d/arcs/O}")}%
            \pgfmathtruncatemacro{\pgfutil@tmpi}{abs(\pgfutil@tmpa-\pgfutil@tmpb)<0.01?1:0}%
            \ifnum\pgfutil@tmpi=0\relax
            \PackageWarning{3dtools}{The points \pv{A} and \pv{B} do not sit on the
                same sphere around \pv{O}.}%
            \else
            \pgfmathsetmacro{\pgfutil@tmpA}{TD("\pgfkeysvalueof{/tikz/3d/arcs/A}")}%
            \pgfmathsetmacro{\pgfutil@tmpB}{TD("\pgfkeysvalueof{/tikz/3d/arcs/B}")}%
            \pgfmathsetmacro{\pgfutil@tmpO}{TD("\pgfkeysvalueof{/tikz/3d/arcs/O}")}%
            \pgfmathtruncatemacro{\pgfutil@tmpV}{sign(screendepth(\pgfutil@tmpA)-screendepth(\pgfutil@tmpO))}%
            \pgfmathtruncatemacro{\pgfutil@tmpW}{sign(screendepth(\pgfutil@tmpB)-screendepth(\pgfutil@tmpO))}%
            \pgfmathsetmacro{\pgfutil@tmpr}{sqrt(\pgfutil@tmpa*\pgfutil@tmpb)}%
            \pgfmathsetmacro{\pgfutil@tmpc}{Mod(360+acos(TD("\pv{A}-\pv{O}o\pv{B}-\pv{O}")/\pgfutil@tmpa/\pgfutil@tmpb),360)}%
            \pgfmathsetmacro{\pgfutil@tmpi}{(tddistance("\pgfkeysvalueof{/tikz/3d/arcs/B}","\pgfkeysvalueof{/tikz/3d/arcs/A}")
                -\pgfutil@tmpa-\pgfutil@tmpb<0.1?1:0)}%
            \begingroup 
            \ifnum\pgfutil@tmpi=0\relax
            \pgfmathsetmacro{\pgfutil@tmpC}{TD("\pgfkeysvalueof{/tikz/3d/arcs/A}x\pgfkeysvalueof{/tikz/3d/arcs/n}")}%
            \tikzset{3d/define orthonormal dreibein={A={\pv{O}},B={\pv{A}},C={(\pgfutil@tmpC)}}}%
            \typeout{May not properly work yet.}%
            \else   
            \tikzset{3d/define orthonormal dreibein={A={\pv{O}},B={\pv{A}},C={\pv{B}}}}
            \fi
            \endgroup
            \edef\pgfutil@tmpp{0}%
            \tikzset{3d/arc/draw arc}
            \fi     
    }},
    pics/3d/circle arc/.style={code={%
            \tikzset{3d/arcs/.cd,#1}%
            \def\pv##1{\pgfkeysvalueof{/tikz/3d/arcs/##1}}%
            \pgfmathsetmacro{\pgfutil@tmpa}{tddistance("\pv{A}","\pv{O}")}%
            \pgfmathsetmacro{\pgfutil@tmpb}{tddistance("\pv{B}","\pv{O}")}%
            \pgfmathtruncatemacro{\pgfutil@tmpi}{abs(\pgfutil@tmpa-\pgfutil@tmpb)<0.01?1:0}%
            \ifnum\pgfutil@tmpi=0\relax
            \PackageWarning{3dtools}{The points \pv{A} and \pv{B} do not sit on the
                same sphere around \pv{O}.}%
            \else
            \pgfmathsetmacro{\pgfutil@tmpA}{TD("\pv{A}")}%
            \pgfmathsetmacro{\pgfutil@tmpB}{TD("\pv{B}")}%
            \pgfmathsetmacro{\pgfutil@tmpO}{TD("\pv{O}")}%
            \pgfmathsetmacro{\pgfutil@tmpn}{TDunit("\pv{n}")}%
            \pgfmathsetmacro{\pgfutil@tmpp}{TD("(\pgfutil@tmpn)o\pv{A}")}%
            \pgfmathsetmacro{\pgfutil@tmpq}{TD("(\pgfutil@tmpn)o\pv{B}")}%
            \pgfmathtruncatemacro{\pgfutil@tmpi}{abs(\pgfutil@tmpp-\pgfutil@tmpq)<0.01?1:0}%
            \ifnum\pgfutil@tmpi=0\relax
            \PackageWarning{3dtools}{The points \pv{A} and \pv{B} are not located on
                the sphere around \pv{O} in such a way that the normal \pv{n} can be used
                to define an arc.}%
            \else
            \pgfmathsetmacro{\pgfutil@tmpX}{TD("\pv{A}-\pv{O}x\pv{B}-\pv{O}")}%
            \pgfmathsetmacro{\pgfutil@tmpx}{TD("(\pgfutil@tmpX)o(\pgfutil@tmpX)")}%
            \pgfmathtruncatemacro{\pgfutil@tmpi}{(\pgfutil@tmpx<0.02?0:1)}%
            \ifnum\pgfutil@tmpi=0\relax
            \pgfmathsetmacro{\pgfutil@tmpC}{TD("\pv{A}-\pv{O}x(\pgfutil@tmpn)")}%
            \else
            \let\pgfutil@tmpC\pgfutil@tmpB
            \fi
            \pgfmathsetmacro{\pgfutil@tmpD}{TD("\pv{O}+\pgfutil@tmpp*(\pgfutil@tmpn)")}%
            %\typeout{\pgfutil@tmpp,\pgfutil@tmpq,(D)=(\pgfutil@tmpD)}%
            \pgfmathtruncatemacro{\pgfutil@tmpV}{sign(screendepth(\pgfutil@tmpA)-screendepth(\pgfutil@tmpO))}%
            \pgfmathtruncatemacro{\pgfutil@tmpW}{sign(screendepth(\pgfutil@tmpB)-screendepth(\pgfutil@tmpO))}%
            \pgfmathsetmacro{\pgfutil@tmpr}{sqrt(TD("\pv{A}-(\pgfutil@tmpD)o\pv{A}-(\pgfutil@tmpD)"))}%
            \pgfmathsetmacro{\pgfutil@tmpc}{Mod(360+acos(TD("\pv{A}-(\pgfutil@tmpD)o\pv{B}-(\pgfutil@tmpD)")/\pgfutil@tmpr/\pgfutil@tmpr),360)}%
            %\typeout{A=(\pgfutil@tmpA),B=(\pgfutil@tmpB),D=(\pgfutil@tmpD)}%   
            \begingroup
            \tikzset{3d/define orthonormal dreibein={B={\pv{A}},
                    A={(\pgfutil@tmpD)},C/.expanded={(\pgfutil@tmpC)}}}%
            \endgroup
            \pgfmathsetmacro{\pgfutil@tmpp}{TD("\pgfkeysvalueof{/tikz/3d/aux keys/ez}o(\pgfutil@tmpA)")}
            \tikzset{3d/arc/draw arc}
            \fi     
            \fi     
    }},
    /tikz/3d/arc/draw arc/.code={\begin{scope}[shift={(\pgfutil@tmpO)},
            x/.expanded={\pgfkeysvalueof{/tikz/3d/aux keys/ex}},
            y/.expanded={\pgfkeysvalueof{/tikz/3d/aux keys/ey}},
            z/.expanded={\pgfkeysvalueof{/tikz/3d/aux keys/ez}}]     
            \pgfmathsetmacro{\pgfutil@tmpt}{min(Mod(360+atan2(-1*nscreenx,nscreeny),360),Mod(540+atan2(-1*nscreenx,nscreeny),360))}%    
            \pgfmathsetmacro{\pgfutil@tmpu}{max(Mod(360+atan2(-1*nscreenx,nscreeny),360),Mod(540+atan2(-1*nscreenx,nscreeny),360))}%
            \pgfmathtruncatemacro{\pgfutil@tmpi}{(\pgfutil@tmpt>0&&\pgfutil@tmpt<\pgfutil@tmpc)+(\pgfutil@tmpu>0&&\pgfutil@tmpu<\pgfutil@tmpc)}%
            %\typeout{i=\pgfutil@tmpi, t=\pgfutil@tmpt, u=\pgfutil@tmpu, 
                %   c=\pgfutil@tmpc, p=\pgfutil@tmpp, V=\pgfutil@tmpV,W=\pgfutil@tmpW}
            \begin{scope}[canvas is xy plane at z=\pgfutil@tmpp]
                \ifcase\pgfutil@tmpi
                \ifnum\pgfutil@tmpV<0
                \path[3d/hidden] (0:\pgfutil@tmpr) arc[start angle=0,end angle=\pgfutil@tmpc,radius=\pgfutil@tmpr];
                \else
                \path[3d/visible] (0:\pgfutil@tmpr) arc[start angle=0,end angle=\pgfutil@tmpc,radius=\pgfutil@tmpr];
                \fi
                \or
                \ifnum\pgfutil@tmpV<0
                \path[3d/hidden] (0:\pgfutil@tmpr) 
                arc[start angle=0,end angle=\pgfutil@tmpt,radius=\pgfutil@tmpr];
                \path[3d/visible] (\pgfutil@tmpt:\pgfutil@tmpr) 
                arc[start angle=\pgfutil@tmpt,end angle=\pgfutil@tmpc,radius=\pgfutil@tmpr];
                \else
                \path[3d/visible] (0:\pgfutil@tmpr) 
                arc[start angle=0,end angle=\pgfutil@tmpt,radius=\pgfutil@tmpr];
                \path[3d/hidden] (\pgfutil@tmpt:\pgfutil@tmpr) 
                arc[start angle=\pgfutil@tmpt,end angle=\pgfutil@tmpc,radius=\pgfutil@tmpr];
                \fi
                \or
                \ifnum\pgfutil@tmpV<0
                \path[3d/hidden] (0:\pgfutil@tmpr) 
                arc[start angle=0,end angle=\pgfutil@tmpt,radius=\pgfutil@tmpr];
                \path[3d/visible] (\pgfutil@tmpt:\pgfutil@tmpr) 
                arc[start angle=\pgfutil@tmpt,end angle=\pgfutil@tmpu,radius=\pgfutil@tmpr];
                \path[3d/hidden] (\pgfutil@tmpu:\pgfutil@tmpr) 
                arc[start angle=\pgfutil@tmpu,end angle=\pgfutil@tmpc,radius=\pgfutil@tmpr];
                \else
                \path[3d/visible] (0:\pgfutil@tmpr) 
                arc[start angle=0,end angle=\pgfutil@tmpt,radius=\pgfutil@tmpr];
                \path[3d/hidden] (\pgfutil@tmpt:\pgfutil@tmpr) 
                arc[start angle=\pgfutil@tmpt,end angle=\pgfutil@tmpu,radius=\pgfutil@tmpr];
                \path[3d/visible] (\pgfutil@tmpu:\pgfutil@tmpr) 
                arc[start angle=\pgfutil@tmpu,end angle=\pgfutil@tmpc,radius=\pgfutil@tmpr];
                \fi
                \fi
            \end{scope} 
\end{scope}}}
\makeatother    
\begin{document}
    \begin{tikzpicture}[3d/install view={phi=140,psi=0,theta=70},
        line join = round, line cap = round,
        declare function={R=3;}] 
    \path (0,0,0) coordinate (O);
     \shade[ball color=white,3d/screen coords,opacity=0.7] (O) circle[radius=R];
        \path
     (0,0,0) coordinate (O)
     pic[green]{3d/circle on sphere={R=R,P={(O)},n={(0,0,1)}}};
        \path (R,0,0) coordinate (M)
        (-R,0,0) coordinate  (N)
        (O) pic[blue]{3d/circle arc={A={(M)},B={(N)},O={(O)},n={(0,1,0)}}};
        \path (0,R,0) coordinate (E)
        (0,-R, 0) coordinate  (F)
        (O) pic[red]{3d/circle arc={A={(E)},B={(F)},O={(O)},n={(-1,0,0)}}};
        
        %\path foreach \X in {M,N,E,F} {(\X)node[circle,fill,inner sep=1pt]{}};
         \draw[3d/hidden]  (0,0,0) -- (0,0,R) (-R,0,0)--(R,0,0) (0,-R,0)--(0,R,0); 
        \draw[3d/visible, -latex] (R,0,0) -- (R + 2,0,0) node[above]{$x$};
        \draw[3d/visible, -latex] (0,R,0) -- (0,R + 2,0) node[right]{$y$};
        \draw[3d/visible, -latex] (0,0,R) -- (0,0,R + 1) node[above]{$z$};
    \end{tikzpicture}   
    
\end{document}  

在此处输入图片描述

相关内容