18

When I use the parametric equations (according to Wikipedia) to plot a trefoil knot:

x(t)=sint+2sin2t

y(t)=cost−2cos2t

z(t)=−sin3t

I get a plot that looks very different from the nice plot on Wolfram Alpha.

trefoil from WA

This is my code:

\begin{tikzpicture}
\begin{axis}
\addplot3[variable=t,mesh,domain=-2*pi:2*pi] ({sin(deg(t))+2*sin(deg(2*t))},{cos(deg(t))-2*cos(deg(2*t))}, {-1*sin(deg(3*t))});
\end{axis}
\end{tikzpicture}

How can I create a nice plot of a trefoil knot? Thank you!

4

2 Answers 2

26

I think that the key here is to put more samples. This is your code, with 100 samples. I changed the domain too, from 0 to 2pi, because yours draws everything twice. I also added a view more alike to the plot in WA that you linked.

\documentclass[border=2mm]{standalone}
\usepackage {pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[view={30}{55}]
  \addplot3[red,variable=t,samples=100,domain=0:2*pi]
    ({sin(deg(t))+2*sin(deg(2*t))},{cos(deg(t))-2*cos(deg(2*t))}, {-sin(deg(3*t))});
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

4
  • Thank you so much!!!
    – Cheryl
    Commented Mar 13, 2021 at 17:55
  • 3
    Giving your helper a thank you is fine, voting and accepting his post is a better way to encourage him ;)
    – SebGlav
    Commented Mar 13, 2021 at 18:10
  • 1
    @Cheryl The above comment is meant for you :). Commented Mar 14, 2021 at 3:34
  • @SebGlav @ Dr.ManuelKuehner he is not registered, therefore he will not respond. :). Commented Oct 8, 2021 at 11:17
25

This is basic Asymptote code:

//
// trefoil.asy
// 
// to get trefoil.png, run
// asy -f png -render=4 trefoil.asy
// 
// to view it in internal Asymptote 3d-viewer, run
// asy -V trefoil.asy
//
// or, to get an interactive 3D vector WebGL graphics,
//    embedded within HTML file trefoil.html, run
// asy -f html trefoil.asy

//
import graph3;
import tube;

size(200,0);
currentlight.background=paleyellow+opacity(0.0);
currentprojection=orthographic(camera=(-40,9,70), up=Z);

real x(real t){return sin(t)+2sin(2t);}
real y(real t){return cos(t)-2cos(2t);}
real z(real t){return -sin(3t);}

guide3 g=graph(x,y,z, 0, 2pi,operator..);

draw(tube(g,circle((0,0),0.618)),white);

enter image description here

Slightly more involved version:

import graph3;
import tube;

size(200,0);
currentlight.background=paleyellow+opacity(0.0);
currentprojection=
  orthographic(camera=(0.4,20,-74),up=(0.88,0.46,0.13)); //,zoom=0.8);

real x(real t){return sin(t)+2sin(2t);}
real y(real t){return cos(t)-2cos(2t);}
real z(real t){return -sin(3t);}
guide3 g=graph(x,y,z, 0, 2pi,operator..);

pair[][] p={
  {(-40, 20),( -56.8421, 23.4210),( -78.9473, 28.6842),(-90, 20)},
  {(-90, 20),(-101.0526, 11.3157),(-101.0526,-11.3157),(-90,-20)},
  {(-90,-20),( -78.9473,-28.6842),( -56.8421,-23.4210),(-40,-20)},
  {(-40,-20),( -23.1578,-16.5789),( -11.5789,-15     ),(  0,-15)},
  {(  0,-15),(  11.5789,-15     ),(  23.1578,-16.5789),( 40,-20)},
  {( 40,-20),(  56.8421,-23.4210),(  78.9473,-28.6842),( 90,-20)},
  {( 90,-20),( 101.0526,-11.3157),( 101.0526, 11.3157),( 90, 20)},
  {( 90, 20),(  78.9473, 28.6842),(  56.8421, 23.4210),( 40, 20)},
  {( 40, 20),(  23.1578, 16.5789),(  11.5789, 15     ),(  0, 15)},
  {(  0, 15),( -11.5789, 15     ),( -23.1578, 16.5789),(-40, 20)}
};

pen paint(real t){
  return hsv(t*360*0.2,1,1);
}

guide gx=p[0][0];
for(int i=0;i<p.length;++i){
  gx=gx.. controls p[i][1] and p[i][2] .. p[i][3];
}
gx=scale(0.007)*(gx&cycle);

draw(tube(g,coloredpath(gx, paint),
           new transform(real t) {return rotate(80*t/2/pi);}));

enter image description here

If this is not a nice plot of a trefoil knot, then I don't know what is.


Edit:

And this is a one-sided version (kind of):

import graph3;
import tube;

size(200,0);
currentlight.background=paleyellow+opacity(0.0);
currentprojection=orthographic(camera=(-10,49,-58),up=(0.92,0.36,0.14));

real x(real t){return sin(2pi*t)+2sin(2*2pi*t);}
real y(real t){return cos(2pi*t)-2cos(2*2pi*t);}
real z(real t){return -sin(3*2pi*t);}
guide3 g=graph(x,y,z, 0, 1,operator..);

pair[][] p={
  {(-40, 20),( -56.8421, 23.4210),( -78.9473, 28.6842),(-90, 20)},
  {(-90, 20),(-101.0526, 11.3157),(-101.0526,-11.3157),(-90,-20)},
  {(-90,-20),( -78.9473,-28.6842),( -56.8421,-23.4210),(-40,-20)},
  {(-40,-20),( -23.1578,-16.5789),( -11.5789,-15     ),(  0,-15)},
  {(  0,-15),(  11.5789,-15     ),(  23.1578,-16.5789),( 40,-20)},
  {( 40,-20),(  56.8421,-23.4210),(  78.9473,-28.6842),( 90,-20)},
  {( 90,-20),( 101.0526,-11.3157),( 101.0526, 11.3157),( 90, 20)},
  {( 90, 20),(  78.9473, 28.6842),(  56.8421, 23.4210),( 40, 20)},
  {( 40, 20),(  23.1578, 16.5789),(  11.5789, 15     ),(  0, 15)},
  {(  0, 15),( -11.5789, 15     ),( -23.1578, 16.5789),(-40, 20)}
};

pen paint(real t){return hsv(t*360*0.1,0.997,0.997);}

guide gx=p[0][0];
for(int i=0;i<p.length;++i){
  gx=gx.. controls p[i][1] and p[i][2] .. p[i][3];
}
gx=scale(0.006,0.004)*(gx&cycle);
draw(tube(g,coloredpath(gx, paint),new transform(real t) {return rotate((-41.25)*t);}));

enter image description here

4
  • Nice pictures. Can you provide code for the "slightly more involved" version? Commented Mar 17, 2021 at 7:47
  • @Andrew Swann: Thanks for asking, it's there now. Also (at least from version 2.54), an interactive 3D vector WebGL graphics, embedded within HTML file, is available via asy -f html trefoil.asy.
    – g.kov
    Commented Mar 17, 2021 at 12:10
  • Amazing!!!! How did you compute the pair points?
    – Sigur
    Commented Mar 17, 2021 at 12:19
  • 3
    @Sigur: That's from some old project, supposed to be a closed C2 smooth path.
    – g.kov
    Commented Mar 17, 2021 at 12:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .