Plotting a knot on a torus












2















Using pgfplots, I plotted a torus, with a knot that lies on its surface:



begin{tikzpicture}
begin{axis}[axis equal image]

addplot3[domain=0:360, y domain=0:360, samples=20, surf, z buffer=sort]
(
{(2 + cos(x))*cos(y)},
{(2 + cos(x))*sin(y)},
{sin(x)}
);

addplot3[domain=0:360, samples=50]
(
{(2 + cos(2*x))*cos(3*x)},
{(2 + cos(2*x))*sin(3*x)},
{sin(2*x)}
);
end{axis}
end{tikzpicture}


enter image description here



However, it turns out that the knot is not shown properly, because parts of it that should be hidden by the surface, aren't. It should look something like this:



enter image description here



One of the first things I tried is putting in a z buffer=sort key for the knot's graph, however, this just screws up the curve. I guess the issue is that the torus and the knot have to somehow know about each other, for z buffer=sort to work, but there is no good way to do so.



Are there other approaches? I am open to trying non-pgfplots solutions.










share|improve this question























  • With asymptote it should be straightforward. Are you also open to asymptote? I guess it would be slight modification of the asymptote solution here or this solution might be even closer. I also think it is possible with pgfplots, but will require more work.

    – marmot
    Jan 4 at 3:54













  • @marmot wow, this is awesome --- I have no idea what asymptote is, but I am about to learn!

    – user89
    Jan 4 at 4:13











  • Does that mean you do not want an answer because you want to try out yourself or that you are open to an asymptote answer. (Not that I have one in my pocket... ;-)

    – marmot
    Jan 4 at 4:42











  • @marmot if you'd like to provide an answer, I would be happy to accept it! otherwise, I am in the process of understanding, and will provide a solution myself --- so its entirely up to your generosity!

    – user89
    Jan 4 at 5:35











  • Either way. (It is not generous to point to posts that solved a similar problem before, I think. ;-)

    – marmot
    Jan 4 at 5:36
















2















Using pgfplots, I plotted a torus, with a knot that lies on its surface:



begin{tikzpicture}
begin{axis}[axis equal image]

addplot3[domain=0:360, y domain=0:360, samples=20, surf, z buffer=sort]
(
{(2 + cos(x))*cos(y)},
{(2 + cos(x))*sin(y)},
{sin(x)}
);

addplot3[domain=0:360, samples=50]
(
{(2 + cos(2*x))*cos(3*x)},
{(2 + cos(2*x))*sin(3*x)},
{sin(2*x)}
);
end{axis}
end{tikzpicture}


enter image description here



However, it turns out that the knot is not shown properly, because parts of it that should be hidden by the surface, aren't. It should look something like this:



enter image description here



One of the first things I tried is putting in a z buffer=sort key for the knot's graph, however, this just screws up the curve. I guess the issue is that the torus and the knot have to somehow know about each other, for z buffer=sort to work, but there is no good way to do so.



Are there other approaches? I am open to trying non-pgfplots solutions.










share|improve this question























  • With asymptote it should be straightforward. Are you also open to asymptote? I guess it would be slight modification of the asymptote solution here or this solution might be even closer. I also think it is possible with pgfplots, but will require more work.

    – marmot
    Jan 4 at 3:54













  • @marmot wow, this is awesome --- I have no idea what asymptote is, but I am about to learn!

    – user89
    Jan 4 at 4:13











  • Does that mean you do not want an answer because you want to try out yourself or that you are open to an asymptote answer. (Not that I have one in my pocket... ;-)

    – marmot
    Jan 4 at 4:42











  • @marmot if you'd like to provide an answer, I would be happy to accept it! otherwise, I am in the process of understanding, and will provide a solution myself --- so its entirely up to your generosity!

    – user89
    Jan 4 at 5:35











  • Either way. (It is not generous to point to posts that solved a similar problem before, I think. ;-)

    – marmot
    Jan 4 at 5:36














2












2








2








Using pgfplots, I plotted a torus, with a knot that lies on its surface:



begin{tikzpicture}
begin{axis}[axis equal image]

addplot3[domain=0:360, y domain=0:360, samples=20, surf, z buffer=sort]
(
{(2 + cos(x))*cos(y)},
{(2 + cos(x))*sin(y)},
{sin(x)}
);

addplot3[domain=0:360, samples=50]
(
{(2 + cos(2*x))*cos(3*x)},
{(2 + cos(2*x))*sin(3*x)},
{sin(2*x)}
);
end{axis}
end{tikzpicture}


enter image description here



However, it turns out that the knot is not shown properly, because parts of it that should be hidden by the surface, aren't. It should look something like this:



enter image description here



One of the first things I tried is putting in a z buffer=sort key for the knot's graph, however, this just screws up the curve. I guess the issue is that the torus and the knot have to somehow know about each other, for z buffer=sort to work, but there is no good way to do so.



Are there other approaches? I am open to trying non-pgfplots solutions.










share|improve this question














Using pgfplots, I plotted a torus, with a knot that lies on its surface:



begin{tikzpicture}
begin{axis}[axis equal image]

addplot3[domain=0:360, y domain=0:360, samples=20, surf, z buffer=sort]
(
{(2 + cos(x))*cos(y)},
{(2 + cos(x))*sin(y)},
{sin(x)}
);

addplot3[domain=0:360, samples=50]
(
{(2 + cos(2*x))*cos(3*x)},
{(2 + cos(2*x))*sin(3*x)},
{sin(2*x)}
);
end{axis}
end{tikzpicture}


enter image description here



However, it turns out that the knot is not shown properly, because parts of it that should be hidden by the surface, aren't. It should look something like this:



enter image description here



One of the first things I tried is putting in a z buffer=sort key for the knot's graph, however, this just screws up the curve. I guess the issue is that the torus and the knot have to somehow know about each other, for z buffer=sort to work, but there is no good way to do so.



Are there other approaches? I am open to trying non-pgfplots solutions.







tikz-pgf pgfplots






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 4 at 3:47









user89user89

1,5412934




1,5412934













  • With asymptote it should be straightforward. Are you also open to asymptote? I guess it would be slight modification of the asymptote solution here or this solution might be even closer. I also think it is possible with pgfplots, but will require more work.

    – marmot
    Jan 4 at 3:54













  • @marmot wow, this is awesome --- I have no idea what asymptote is, but I am about to learn!

    – user89
    Jan 4 at 4:13











  • Does that mean you do not want an answer because you want to try out yourself or that you are open to an asymptote answer. (Not that I have one in my pocket... ;-)

    – marmot
    Jan 4 at 4:42











  • @marmot if you'd like to provide an answer, I would be happy to accept it! otherwise, I am in the process of understanding, and will provide a solution myself --- so its entirely up to your generosity!

    – user89
    Jan 4 at 5:35











  • Either way. (It is not generous to point to posts that solved a similar problem before, I think. ;-)

    – marmot
    Jan 4 at 5:36



















  • With asymptote it should be straightforward. Are you also open to asymptote? I guess it would be slight modification of the asymptote solution here or this solution might be even closer. I also think it is possible with pgfplots, but will require more work.

    – marmot
    Jan 4 at 3:54













  • @marmot wow, this is awesome --- I have no idea what asymptote is, but I am about to learn!

    – user89
    Jan 4 at 4:13











  • Does that mean you do not want an answer because you want to try out yourself or that you are open to an asymptote answer. (Not that I have one in my pocket... ;-)

    – marmot
    Jan 4 at 4:42











  • @marmot if you'd like to provide an answer, I would be happy to accept it! otherwise, I am in the process of understanding, and will provide a solution myself --- so its entirely up to your generosity!

    – user89
    Jan 4 at 5:35











  • Either way. (It is not generous to point to posts that solved a similar problem before, I think. ;-)

    – marmot
    Jan 4 at 5:36

















With asymptote it should be straightforward. Are you also open to asymptote? I guess it would be slight modification of the asymptote solution here or this solution might be even closer. I also think it is possible with pgfplots, but will require more work.

– marmot
Jan 4 at 3:54







With asymptote it should be straightforward. Are you also open to asymptote? I guess it would be slight modification of the asymptote solution here or this solution might be even closer. I also think it is possible with pgfplots, but will require more work.

– marmot
Jan 4 at 3:54















@marmot wow, this is awesome --- I have no idea what asymptote is, but I am about to learn!

– user89
Jan 4 at 4:13





@marmot wow, this is awesome --- I have no idea what asymptote is, but I am about to learn!

– user89
Jan 4 at 4:13













Does that mean you do not want an answer because you want to try out yourself or that you are open to an asymptote answer. (Not that I have one in my pocket... ;-)

– marmot
Jan 4 at 4:42





Does that mean you do not want an answer because you want to try out yourself or that you are open to an asymptote answer. (Not that I have one in my pocket... ;-)

– marmot
Jan 4 at 4:42













@marmot if you'd like to provide an answer, I would be happy to accept it! otherwise, I am in the process of understanding, and will provide a solution myself --- so its entirely up to your generosity!

– user89
Jan 4 at 5:35





@marmot if you'd like to provide an answer, I would be happy to accept it! otherwise, I am in the process of understanding, and will provide a solution myself --- so its entirely up to your generosity!

– user89
Jan 4 at 5:35













Either way. (It is not generous to point to posts that solved a similar problem before, I think. ;-)

– marmot
Jan 4 at 5:36





Either way. (It is not generous to point to posts that solved a similar problem before, I think. ;-)

– marmot
Jan 4 at 5:36










1 Answer
1






active

oldest

votes


















1














Here is a proposal but this is not really my proposal. It is a combination of this answer and this answer. Personally I like to use asypictureB by the author of the second answer. You can compile this e.g. with pdflatex -shell-escape.



documentclass[margin=3.14mm]{standalone}
usepackage{asypictureB}
begin{document} % based on https://tex.stackexchange.com/a/149759/121799 and
% https://tex.stackexchange.com/a/149784/121799
begin{asypicture}{name=torus}
import graph3;

size(200,0);
currentprojection=orthographic(4,0,2);

//inner radius
real R=2;
//outer radius
real a=0.75;

//surface:
triple f(pair t) {
return ((R+a*cos(t.y))*cos(t.x),(R+a*cos(t.y))*sin(t.x),a*sin(t.y));
}

//path:
real x(real t) {return cos(t*3)*(R + a*cos(t));}
real y(real t) {return sin(t*3)*(R + a*cos(t));}
real z(real t) {return a*sin(t);}

pen p=blue+opacity(0.33);
// make surface and path
surface s=surface(f,(0,0),(2pi,2pi),8,8,Spline);
path3 q=graph(x,y,z,0,6*pi,operator ..);

// draw surface and path
draw(s,surfacepen=material(diffusepen=blue+opacity(0.33), emissivepen=blue));
real linewidth = 2pt;
draw(q, p=linewidth + orange);
end{asypicture}
end{document}


enter image description here



Of course, one can also color different stretches of the path differently. Please let me know if there are problems, or if I should remove this answer because it does not represent any real progress compared to what is already on the market.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "85"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468502%2fplotting-a-knot-on-a-torus%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Here is a proposal but this is not really my proposal. It is a combination of this answer and this answer. Personally I like to use asypictureB by the author of the second answer. You can compile this e.g. with pdflatex -shell-escape.



    documentclass[margin=3.14mm]{standalone}
    usepackage{asypictureB}
    begin{document} % based on https://tex.stackexchange.com/a/149759/121799 and
    % https://tex.stackexchange.com/a/149784/121799
    begin{asypicture}{name=torus}
    import graph3;

    size(200,0);
    currentprojection=orthographic(4,0,2);

    //inner radius
    real R=2;
    //outer radius
    real a=0.75;

    //surface:
    triple f(pair t) {
    return ((R+a*cos(t.y))*cos(t.x),(R+a*cos(t.y))*sin(t.x),a*sin(t.y));
    }

    //path:
    real x(real t) {return cos(t*3)*(R + a*cos(t));}
    real y(real t) {return sin(t*3)*(R + a*cos(t));}
    real z(real t) {return a*sin(t);}

    pen p=blue+opacity(0.33);
    // make surface and path
    surface s=surface(f,(0,0),(2pi,2pi),8,8,Spline);
    path3 q=graph(x,y,z,0,6*pi,operator ..);

    // draw surface and path
    draw(s,surfacepen=material(diffusepen=blue+opacity(0.33), emissivepen=blue));
    real linewidth = 2pt;
    draw(q, p=linewidth + orange);
    end{asypicture}
    end{document}


    enter image description here



    Of course, one can also color different stretches of the path differently. Please let me know if there are problems, or if I should remove this answer because it does not represent any real progress compared to what is already on the market.






    share|improve this answer




























      1














      Here is a proposal but this is not really my proposal. It is a combination of this answer and this answer. Personally I like to use asypictureB by the author of the second answer. You can compile this e.g. with pdflatex -shell-escape.



      documentclass[margin=3.14mm]{standalone}
      usepackage{asypictureB}
      begin{document} % based on https://tex.stackexchange.com/a/149759/121799 and
      % https://tex.stackexchange.com/a/149784/121799
      begin{asypicture}{name=torus}
      import graph3;

      size(200,0);
      currentprojection=orthographic(4,0,2);

      //inner radius
      real R=2;
      //outer radius
      real a=0.75;

      //surface:
      triple f(pair t) {
      return ((R+a*cos(t.y))*cos(t.x),(R+a*cos(t.y))*sin(t.x),a*sin(t.y));
      }

      //path:
      real x(real t) {return cos(t*3)*(R + a*cos(t));}
      real y(real t) {return sin(t*3)*(R + a*cos(t));}
      real z(real t) {return a*sin(t);}

      pen p=blue+opacity(0.33);
      // make surface and path
      surface s=surface(f,(0,0),(2pi,2pi),8,8,Spline);
      path3 q=graph(x,y,z,0,6*pi,operator ..);

      // draw surface and path
      draw(s,surfacepen=material(diffusepen=blue+opacity(0.33), emissivepen=blue));
      real linewidth = 2pt;
      draw(q, p=linewidth + orange);
      end{asypicture}
      end{document}


      enter image description here



      Of course, one can also color different stretches of the path differently. Please let me know if there are problems, or if I should remove this answer because it does not represent any real progress compared to what is already on the market.






      share|improve this answer


























        1












        1








        1







        Here is a proposal but this is not really my proposal. It is a combination of this answer and this answer. Personally I like to use asypictureB by the author of the second answer. You can compile this e.g. with pdflatex -shell-escape.



        documentclass[margin=3.14mm]{standalone}
        usepackage{asypictureB}
        begin{document} % based on https://tex.stackexchange.com/a/149759/121799 and
        % https://tex.stackexchange.com/a/149784/121799
        begin{asypicture}{name=torus}
        import graph3;

        size(200,0);
        currentprojection=orthographic(4,0,2);

        //inner radius
        real R=2;
        //outer radius
        real a=0.75;

        //surface:
        triple f(pair t) {
        return ((R+a*cos(t.y))*cos(t.x),(R+a*cos(t.y))*sin(t.x),a*sin(t.y));
        }

        //path:
        real x(real t) {return cos(t*3)*(R + a*cos(t));}
        real y(real t) {return sin(t*3)*(R + a*cos(t));}
        real z(real t) {return a*sin(t);}

        pen p=blue+opacity(0.33);
        // make surface and path
        surface s=surface(f,(0,0),(2pi,2pi),8,8,Spline);
        path3 q=graph(x,y,z,0,6*pi,operator ..);

        // draw surface and path
        draw(s,surfacepen=material(diffusepen=blue+opacity(0.33), emissivepen=blue));
        real linewidth = 2pt;
        draw(q, p=linewidth + orange);
        end{asypicture}
        end{document}


        enter image description here



        Of course, one can also color different stretches of the path differently. Please let me know if there are problems, or if I should remove this answer because it does not represent any real progress compared to what is already on the market.






        share|improve this answer













        Here is a proposal but this is not really my proposal. It is a combination of this answer and this answer. Personally I like to use asypictureB by the author of the second answer. You can compile this e.g. with pdflatex -shell-escape.



        documentclass[margin=3.14mm]{standalone}
        usepackage{asypictureB}
        begin{document} % based on https://tex.stackexchange.com/a/149759/121799 and
        % https://tex.stackexchange.com/a/149784/121799
        begin{asypicture}{name=torus}
        import graph3;

        size(200,0);
        currentprojection=orthographic(4,0,2);

        //inner radius
        real R=2;
        //outer radius
        real a=0.75;

        //surface:
        triple f(pair t) {
        return ((R+a*cos(t.y))*cos(t.x),(R+a*cos(t.y))*sin(t.x),a*sin(t.y));
        }

        //path:
        real x(real t) {return cos(t*3)*(R + a*cos(t));}
        real y(real t) {return sin(t*3)*(R + a*cos(t));}
        real z(real t) {return a*sin(t);}

        pen p=blue+opacity(0.33);
        // make surface and path
        surface s=surface(f,(0,0),(2pi,2pi),8,8,Spline);
        path3 q=graph(x,y,z,0,6*pi,operator ..);

        // draw surface and path
        draw(s,surfacepen=material(diffusepen=blue+opacity(0.33), emissivepen=blue));
        real linewidth = 2pt;
        draw(q, p=linewidth + orange);
        end{asypicture}
        end{document}


        enter image description here



        Of course, one can also color different stretches of the path differently. Please let me know if there are problems, or if I should remove this answer because it does not represent any real progress compared to what is already on the market.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 6:14









        marmotmarmot

        90.7k4104195




        90.7k4104195






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468502%2fplotting-a-knot-on-a-torus%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?