2

I have used the sample code from latexdraw that draws a torus. I am trying to draw a filled circle and a dashed circle where they should be wrap around the torus instead of being drawn uniformly at z=0. I am looking for a TikZ solution. The closest I have seen is this solution but it is too difficult for me to decipher. Any help would be appreciated!

current output

\documentclass[margin=4pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{tikz-3dplot}
\usepgfplotslibrary{colormaps}

\pgfplotsset{
  compat=newest,
  colormap={manifoldmap}{color=(lightgray) color=(white) color=(lightgray)}
}

\begin{document}
\begin{tikzpicture}
  \def\R{2.75}     % Major radius (distance from torus centre to tube centre)
  \def\offset{0.5} % Radial displacement from the torus centre circle
  \def\thetaB{2.5} % Angular position (in radians) for highlighted point

  \begin{axis}[
    width=12cm,      
    axis equal image,
    hide axis,       
    z buffer=sort,   
    view={125}{35},  
    clip=false       
  ]
    % Parametric torus equations:
      % x = (R + cos(x)) cos(y)
      % y = (R + cos(x)) sin(y)
      % z = sin(x)
    \addplot3[
      surf,
      shader = faceted interp,
      samples = 30,
      samples y = 40,
      domain = 0:2*pi,
      domain y = 0:2*pi,
      colormap name = manifoldmap,
      thin,
      opacity=0.75
    ]
    (
      {(\R + cos(deg(x))) * cos(deg(y))},
      {(\R + cos(deg(x))) * sin(deg(y))},
      {sin(deg(x))}
    );
    \begin{scope}[canvas is xy plane at z=0, transform shape]
      \coordinate (p) at
        ({(\R + \offset) * cos(deg(\thetaB))},
         {(\R + \offset) * sin(deg(\thetaB))});
      \draw[fill=red, draw=none] (p) circle (4pt);
      \draw[blue, dashed, thick] (p) circle (16pt);
    \end{scope}
  \end{axis}
\end{tikzpicture}
\end{document}

1 Answer 1

0

Maybe this? For 3D, I'd personally use a specialized 3D package, not plain TikZ. Just had this old concept lying around.

\documentclass[tikz, border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\pgfmathdeclarefunction{sphereX}{2}{%
    % #1  - longitude
    % #2  - latitude
    \pgfmathparse{cos((#2) r)*cos((#1) r)}%
}
\pgfmathdeclarefunction{sphereY}{2}{%
    % #1  - longitude
    % #2  - latitude
    \pgfmathparse{cos((#2) r)*sin((#1) r)}%
}
\pgfmathdeclarefunction{sphereZ}{2}{%
    % #1  - longitude
    % #2  - latitude
    \pgfmathparse{sin((#2) r)}%
}
\begin{document}
    \tdplotsetmaincoords{60}{80}
    \begin{tikzpicture}[tdplot_main_coords]
        \foreach \ANGLE in {0,...,36} {
            \pgfmathsetmacro{\ANGLE}{\ANGLE*pi/18}
            \draw[domain=0:2*pi] plot (   
                {2*cos(\x r)+0.5*sphereX(\x,\ANGLE)},
                {2*sin(\x r)+0.5*sphereY(\x,\ANGLE)},
                {0.5*sphereZ(\x,\ANGLE)}
            );
            \draw[domain=0:2*pi] plot (   
                {2*cos(\ANGLE r)+0.5*sphereX(\ANGLE,\x)},
                {2*sin(\ANGLE r)+0.5*sphereY(\ANGLE,\x)},
                {0.5*sphereZ(\ANGLE,\x)}
            );
        }
        \pgfmathsetmacro{\ANGLE}{3*pi/2}
        \draw[domain=0:2*pi,red,ultra thick] plot (   
            {2*cos(\ANGLE r)+0.5*sphereX(\ANGLE,\x)},
            {2*sin(\ANGLE r)+0.5*sphereY(\ANGLE,\x)},
            {0.5*sphereZ(\ANGLE,\x)}
        );
    \end{tikzpicture}
\end{document}
2
  • 1
    maybe add line cap=round could tweak the ellipse better🤔 Commented 2 hours ago
  • @Explorer I completely agree, and would normally do that for a polished illustration - I wasn't making this for aesthetic purposes, I just wanted to share the trigonometric-linear algebraic approach which I am so fond of. Commented 2 hours ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.