How to Color a Torus in LaTeX?

  • LaTeX
  • Thread starter thatboi
  • Start date
  • Tags
    Latex
  • #1
thatboi
121
18
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: 42
Last edited by a moderator:
  • Like
Likes fresh_42
Physics news on Phys.org

1. What is a torus in LaTeX?

A torus in LaTeX is a geometric shape that resembles a donut or a tire. It is created using mathematical equations and can be colored in using various commands and packages in LaTeX.

2. How do I color in a torus in LaTeX?

To color in a torus in LaTeX, you can use the \color command followed by the desired color in curly braces. You can also use the \usepackage{tikz} package and its various commands to create and color a torus.

3. Can I add shading or gradients to a colored torus in LaTeX?

Yes, you can add shading and gradients to a colored torus in LaTeX using the \shadedraw and \gradientpath commands from the \usepackage{tikz} package. These commands allow you to create a gradient effect on the torus' surface.

4. Is it possible to add labels or annotations to a colored torus in LaTeX?

Yes, you can add labels or annotations to a colored torus in LaTeX using the \node command from the \usepackage{tikz} package. This command allows you to add text or symbols to specific points on the torus.

5. Can I export a colored torus from LaTeX to other formats, such as an image file?

Yes, you can export a colored torus from LaTeX to other formats, such as an image file, using the \includegraphics command from the \usepackage{graphicx} package. This command allows you to convert your LaTeX code into an image file that can be used in other programs or documents.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
0
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
275
  • Math Proof Training and Practice
4
Replies
107
Views
15K
Replies
1
Views
1K
  • Math POTW for Graduate Students
Replies
1
Views
1K
  • Differential Geometry
Replies
1
Views
2K
  • Topology and Analysis
Replies
1
Views
1K
Back
Top