Skip to content

Instantly share code, notes, and snippets.

@seungwonpark
Last active February 2, 2018 12:09
Show Gist options
  • Save seungwonpark/e3e5ef0b7584d9a74c9b007ee122dda1 to your computer and use it in GitHub Desktop.
Save seungwonpark/e3e5ef0b7584d9a74c9b007ee122dda1 to your computer and use it in GitHub Desktop.
Drawing toroid with TikZ using perspective projection
\documentclass{standalone}
\usepackage{tikz}
\def\camtheta{60}
\def\camphi{70}
%\def\p{((3+cos(16*\t))*cos(\t))}
%\def\q{((3+cos(16*\t))*sin(\t))}
%\def\r{sin(16*\t)}
%\def\w{\p*sin(\camtheta)*cos(\camphi) + \q*sin(\camtheta)*sin(\camphi) + \r*cos(\camtheta)}
\def\rot{32} % # of toroid revolution.
\def\dist{20} % distance of camera and origin
\def\bigr{4} % radius of big circle
\def\smallr{0.5} % radius of small circle
\begin{document}
\begin{tikzpicture}
\draw[domain=0:360, smooth, samples=200, variable=\t]
plot (
{((\bigr+\smallr*cos(\rot*\t))*cos(\t)*sin(\camphi) - (\bigr+\smallr*cos(\rot*\t))*sin(\t)*cos(\camphi)) / ((\bigr+\smallr*cos(\rot*\t))*cos(\t)*sin(\camtheta)*cos(\camphi) + (\bigr+\smallr*cos(\rot*\t))*sin(\t)*sin(\camtheta)*sin(\camphi) + sin(\rot*\t)*cos(\camtheta)-\dist)},
{((\bigr+\smallr*cos(\rot*\t))*cos(\t)*cos(\camtheta)*cos(\camphi) + (\bigr+\smallr*cos(\rot*\t))*sin(\t)*cos(\camtheta)*sin(\camphi) - sin(\rot*\t)*sin(\camtheta)) / ((\bigr+\smallr*cos(\rot*\t))*cos(\t)*sin(\camtheta)*cos(\camphi) + (\bigr+\smallr*cos(\rot*\t))*sin(\t)*sin(\camtheta)*sin(\camphi) + sin(\rot*\t)*cos(\camtheta)-\dist)}
);
\end{tikzpicture}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment