我目前正试图连接这个 tikz 图的两个红色外管; 这是我正在使用的代码;
\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}
我想知道,给定上述坐标,是否有可能生成一个遵循外轮廓的表面。
答案1
如果您计算了相当数量的交点,答案是肯定的。
\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}
请注意,如果您通过其他方式绘制轮廓,即不是通过平滑的轮廓,而是通过绘制函数,那么会有一个更简单的解决方案......