LaTeX How Can I Color Different Sections of a Torus in LaTeX Using TikZ?

  • Thread starter Thread starter thatboi
  • Start date Start date
  • Tags Tags
    Latex
AI Thread Summary
The discussion focuses on creating a torus in LaTeX using TikZ and PGFPlots. A user shares a code snippet that generates a torus with lines winding along different axes. The code includes functions for calculating the torus's coordinates and critical values. The user seeks assistance in filling the surfaces of two sections of the torus, represented by dashed circles, with different colors. They express a desire for guidance, indicating their relative inexperience with 3D graphics in TikZ. The conversation references a related question on TeX Stack Exchange, suggesting that the topic has been discussed previously.
thatboi
Messages
130
Reaction score
20
Hey all,
I have been looking into drawing a torus in LaTeX and came across the post Torus with lines winding along different axis, where the response by user194703 involved the following block of code (which I have very slightly edited)
Code:
\documentclass[tikz,border=3.14mm]{standalone}
% based on 
% [URL]https://tex.stackexchange.com/a/199715[/URL]
% [URL]https://tex.stackexchange.com/a/485833[/URL]
% [URL]https://tex.stackexchange.com/a/485494[/URL]
\usepackage{pgfplots}
\pgfplotsset{compat=1.16,width=16cm,
}
\tikzset{declare function={torusx(\u,\v,\R,\r)=cos(\u)*(\R + \r*cos(\v)); 
torusy(\u,\v,\R,\r)=(\R + \r*cos(\v))*sin(\u);
torusz(\u,\v,\R,\r)=\r*sin(\v);
vcrit1(\u,\th)=atan(tan(\th)*sin(\u));% first critical v value
vcrit2(\u,\th)=180+atan(tan(\th)*sin(\u));% second critical v value
vtest(\u,\v,\az,\el)=sin(-vcrit1(\u-\az,\el)+\v);
disc(\th,\R,\r)=((pow(\r,2)-pow(\R,2))*pow(cot(\th),2)+% 
pow(\r,2)*(2+pow(tan(\th),2)))/pow(\R,2);% discriminant
umax(\th,\R,\r)=ifthenelse(disc(\th,\R,\r)>0,asin(sqrt(abs(disc(\th,\R,\r)))),0);
}}
\pgfplotsset{visible stretch/.style={restrict expr to domain={vtest(atan2(rawy,rawx),%
ifthenelse(veclen(rawx,rawy)>\R,asin(rawz/\r),180-asin(rawz/\r)),\pgfkeysvalueof{/pgfplots/view/az},\pgfkeysvalueof{/pgfplots/view/el})}{-0.05:1.1}},
hidden stretch/.style={restrict expr to 
domain={vtest(atan2(rawy,rawx),%
ifthenelse(veclen(rawx,rawy)>\R,asin(rawz/\r),180-asin(rawz/\r)),\pgfkeysvalueof{/pgfplots/view/az},\pgfkeysvalueof{/pgfplots/view/el})}{-1.1:0.05}}}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\R}{4}
\pgfmathsetmacro{\r}{1.2}
\pgfplotsset{view={35}{60},axis lines=none,}
\matrix{
\begin{axis}[]
    \pgfplotsinvokeforeach{0,10,...,350}  
    {\addplot3[samples y=0,domain=0:361,smooth,samples=71,ultra thin,gray!50]  
        ({torusx(x,#1+x/12,\R,\r)}, 
        {torusy(x,#1+x/12,\R,\r)}, 
        {torusz(x,#1+x/12,\R,\r)});
     \addplot3[samples y=0,domain=0:360,smooth,samples=71,ultra thin,gray!50]  
        ({torusx(#1,x,\R,\r)}, 
        {torusy(#1,x,\R,\r)}, 
        {torusz(#1,x,\R,\r)});
    }
    \pgfplotsinvokeforeach{0,10,...,350}  
    {\addplot3[samples y=0,domain=0:361,smooth,samples=71,visible stretch,very
    thin,gray]  
        ({torusx(x,#1+x/12,\R,\r)}, 
        {torusy(x,#1+x/12,\R,\r)}, 
        {torusz(x,#1+x/12,\R,\r)});
    \addplot3[samples y=0,domain=0:360,smooth,samples=71,visible stretch,very
    thin,gray]  
        ({torusx(#1,x,\R,\r)}, 
        {torusy(#1,x,\R,\r)}, 
        {torusz(#1,x,\R,\r)});}
    \addplot3[samples y=0,domain=0:360,smooth,samples=71,opacity=0.6,
        purple,very thick,dashed]  
        ({torusx(-60,x,\R,\r)}, 
        {torusy(-60,x,\R,\r)}, 
        {torusz(-60,x,\R,\r)}); 
    \addplot3[samples y=0,domain=0:360,smooth,samples=71,visible
    stretch,purple,very thick,dashed]  
        ({torusx(-10,x,\R,\r)}, 
        {torusy(-10,x,\R,\r)}, 
        {torusz(-10,x,\R,\r)}); 
\end{axis} \\ };
\end{tikzpicture}
\end{document}
The image produced I have also attached. The 2 dashed circles effectively create 2 sections along the torus and I wish to fill the surface of these sections with 2 different colors. If anyone could teach me how to do so I would greatly appreciate it as I am relatively new to drawing 3d graphics via tikz.
 

Attachments

  • Example_torus (1)-1.png
    Example_torus (1)-1.png
    42 KB · Views: 103
Last edited by a moderator:
Physics news on Phys.org

Similar threads

Back
Top