3

I am currently stuck trying to connect the two red outer tubes of this tikz plot;hypersurfaces Here is the code which I am working with;

\documentclass[tikz,border=3mm]{standalone}
\usepackage{physics}
\begin{document}

\begin{tikzpicture}[line join = round, line cap = round]
\pgfmathsetmacro{\factor}{.1/sqrt(2)};
\coordinate (O1) at (-2,0,0);
\coordinate (O2) at (2,0,0);
\coordinate (01a) at (-1,-.5,.4);
\coordinate (01b) at (-1,-.4,-.6);
\coordinate (01c) at (-1,.4,-.4);
\coordinate (01d) at (-1,.3,.4);
\coordinate (02a) at (1,-.5-\factor,.4);
\coordinate (02b) at (1,-.4-\factor,-.6);
\coordinate (02c) at (1,.4+\factor,-.4);
\coordinate (02d) at (1,.3+\factor,.4);
\coordinate (A) at (-1,-1,-1);
\coordinate (B) at (-1,-1,1);
\coordinate (C) at (-1,1,1);
\coordinate (D) at (-1,1,-1);
\coordinate (E) at (1,-1,-1);
\coordinate (F) at (1,-1,1);
\coordinate (G) at (1,1,1);
\coordinate (H) at (1,1,-1);


\filldraw[fill=red!10,dashed] plot [smooth cycle, tension=0.6] coordinates { (-1,-.6,.5) (-1,-.5,-.7) (-1,.5,-.5) (-1,.4,.5) };
\filldraw[fill=red!10,dashed] plot [smooth cycle, tension=0.6] coordinates { (1,-.6-2*\factor,.5) (1,-.5-2*\factor,-.7) (1,.5+2*\factor,-.5) (1,.4+2*\factor,.5) };
\filldraw[fill=blue!20] plot [smooth cycle, tension=0.6] coordinates { (01a) (01b) (01c) (01d) };
\filldraw[fill=blue!20] plot [smooth cycle, tension=0.6] coordinates { (02a) (02b) (02c) (02d) };

\draw[-] (A) -- (B) -- (C) -- (D) -- cycle;
\draw[-] (E) -- (F) -- (G) -- (H) -- cycle;

\end{tikzpicture}
\end{document}

I am wondering if, given the above coordinates, it is possible to generate a surface which follows the outer contour.

1 Answer 1

3

The answer is yes, if you compute a fair amount of intersections.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{physics}
\usetikzlibrary{intersections,calc,shadings}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}


\begin{document}

\begin{tikzpicture}[line join = round, line cap = round]
\pgfmathsetmacro{\factor}{.1/sqrt(2)};
\coordinate (O1) at (-2,0,0);
\coordinate (O2) at (2,0,0);
\coordinate (01a) at (-1,-.5,.4);
\coordinate (01b) at (-1,-.4,-.6);
\coordinate (01c) at (-1,.4,-.4);
\coordinate (01d) at (-1,.3,.4);
\coordinate (02a) at (1,-.5-\factor,.4);
\coordinate (02b) at (1,-.4-\factor,-.6);
\coordinate (02c) at (1,.4+\factor,-.4);
\coordinate (02d) at (1,.3+\factor,.4);
\coordinate (A) at (-1,-1,-1);
\coordinate (B) at (-1,-1,1);
\coordinate (C) at (-1,1,1);
\coordinate (D) at (-1,1,-1);
\coordinate (E) at (1,-1,-1);
\coordinate (F) at (1,-1,1);
\coordinate (G) at (1,1,1);
\coordinate (H) at (1,1,-1);


\filldraw[fill=red!10,dashed,name path=leftcontour] plot [smooth cycle, tension=0.6] coordinates { (-1,-.6,.5) (-1,-.5,-.7) (-1,.5,-.5) (-1,.4,.5) };
\filldraw[fill=red!10,dashed,name path=rightcontour] plot [smooth cycle, tension=0.6] coordinates { (1,-.6-2*\factor,.5) (1,-.5-2*\factor,-.7) (1,.5+2*\factor,-.5) (1,.4+2*\factor,.5) };
\filldraw[fill=blue!20] plot [smooth cycle, tension=0.6] coordinates { (01a) (01b) (01c) (01d) };
\filldraw[fill=blue!20] plot [smooth cycle, tension=0.6] coordinates { (02a) (02b) (02c) (02d) };

\draw[-] (A) -- (B) -- (C) -- (D) -- cycle;

\path[name path=leftaux] ($(A)!0.9!(B)$) -- ($(C)!0.9!(D)$);

\path[name path=rightaux] ($(E)!0.7!(F)$) -- ($(G)!0.7!(H)$);

\path[name intersections={of=leftaux and leftcontour, name=leftext}];
\path[name intersections={of=rightaux and rightcontour, name=rightext}];

\draw[opacity=0.2,name path=topline] ([xshift=-0.1pt,yshift=-0.1pt]leftext-1) -- ([xshift=0.1pt,yshift=-0.1pt]rightext-1); 
\draw[opacity=0.2,name path=bottomline] (leftext-2) -- (rightext-2); 

\path [%draw,line width=3,blue,
    name path=bottom-right,
    intersection segments={
        of=bottomline and rightcontour,
        sequence={A0 -- B0[reverse]}
    }];

\path [%draw,line width=3,blue,
    name path=bottom-left,
    intersection segments={
        of=bottomline and leftcontour,
        sequence={B0 -- A1[reverse]}
    }];

\path [%draw,line width=3,blue,
    name path=bottom,
    intersection segments={
        of=bottom-left and bottom-right,
        sequence={A0 -- B1}
    }];

\shade[top color=red!20,bottom color=red!70,opacity=0.3,
    intersection segments={
       of=topline and bottom,
        sequence={A0 -- B1[reverse]},
    }
  ];
\draw[-] (E) -- (F) -- (G) -- (H) -- cycle;

\end{tikzpicture}
\end{document}

enter image description here

Notice that if you were to draw the contours by other means, i.e. not by a smooth contour but by, say, plotting a function, there would be a much simpler solution....

1
  • This is a neat solution; Thank you, Marmot!
    – Dhuality
    Apr 5, 2018 at 22:01

You must log in to answer this question.

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