我想要绘制如下图所示的光纤:
如您所见,该图由三个不同半径的同心圆柱组成。这道题是要画出其中一个,比如右边那个半径较小的圆柱。
我到目前为止的代码是这样的:
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{3d}
\begin{tikzpicture}
%Opaque frame so it is easier to get the 3d visualization.
\begin{scope}[opacity=0.3]
\draw(-0.25,1,1) -- (0.25,1,1);
\draw(-0.25,1,1) -- (-0.25,1,-1);
\draw(-0.25,1,1) -- (-0.25,-1,1);
\draw(0.25,1,1) -- (0.25,1,-1);
\draw(0.25,1,1) -- (0.25,-1,1);
\draw(0.25,1,-1) -- (-0.25,1,-1);
\draw(0.25,1,-1) -- (0.25,-1,-1);
\draw(-0.25,1,-1) -- (-0.25,-1,-1);
\draw(-0.25,-1,1) -- (0.25,-1,1);
\draw(0.25,-1,-1) -- (0.25,-1,1);
\draw(0.25,-1,-1) -- (-0.25,-1,-1);
\draw(-0.25,-1,-1) -- (-0.25,-1,1);
\end{scope}
% Draw the cylinder planes
\begin{scope}[canvas is zy plane at x=0.25]
\draw (0,0) circle (1);
\end{scope}
\begin{scope}[canvas is zy plane at x=-0.25]
\draw (0,0) circle (1);
\end{scope}
% Draw the edges, this is the tricky part.
\draw (-0.25,1,0) -- (0.25,1,0);
\end{tikzpicture}
\end{document}
输出:
正如您所看到的,最后这条命令从上部可见部分从右到左绘制了一条直线,但实际上它的位置并不在它应该在的位置。有人知道如何通过参数化得出这种线的“正确 3d”坐标吗?
或者,我尝试使用shapes.geometric
其cylinder
原始版本:
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{3d}
\usetikzlibrary {shapes.geometric}
\begin{tikzpicture}
\begin{scope}[opacity=0.3]
\draw(-0.25,1,1) -- (0.25,1,1);
\draw(-0.25,1,1) -- (-0.25,1,-1);
\draw(-0.25,1,1) -- (-0.25,-1,1);
\draw(0.25,1,1) -- (0.25,1,-1);
\draw(0.25,1,1) -- (0.25,-1,1);
\draw(0.25,1,-1) -- (-0.25,1,-1);
\draw(0.25,1,-1) -- (0.25,-1,-1);
\draw(-0.25,1,-1) -- (-0.25,-1,-1);
\draw(-0.25,-1,1) -- (0.25,-1,1);
\draw(0.25,-1,-1) -- (0.25,-1,1);
\draw(0.25,-1,-1) -- (-0.25,-1,-1);
\draw(-0.25,-1,-1) -- (-0.25,-1,1);
\end{scope}
\node[cylinder,draw,minimum height=0.5cm,minimum width=1] (c) {};
\end{tikzpicture}
\end{document}
输出:
嗯,还不错,但是我对它的规格有两个疑问:
- 如何设置它的前部和后部平面?
- 如何将它的半径设置为等于2,以填满这个长方体的高度和宽度?如何将它的深度指定为等于0.5,作为棱柱的深度?
答案1
在 3D 中,有一个简单的方法可以解决这个问题:使用等距透视图。但在这种情况下,我旋转了轴,试图让绘图看起来像 OP 的。我还添加了一个\newcommand
绘制圆柱体和背景层的 ,以实现正确的可视化。
\documentclass[border=2mm]{standalone}
\usepackage {tikz}
\usetikzlibrary{3d}
% isometric axes
\pgfmathsetmacro\xx{1/sqrt(2)}
\pgfmathsetmacro\xy{1/sqrt(6)}
\pgfmathsetmacro\zy{sqrt(2/3)}
% layers
\pgfdeclarelayer{background}
\pgfsetlayers {background,main}
\newcommand{\cylinder}[5] % center (left base), radius, height, color, opacity
{%
\begin{scope}[shift={#1}]
\begin{pgfonlayer}{background}
\draw[#4] (135:#2) arc (135:315:#2);
\end{pgfonlayer}
\draw[#4,top color=#4!40,fill opacity=#5]
(-45:#2) arc (-45:135:#2) --++ (0,0,#3) arc (135:-45:#2) -- cycle;
\draw[#4,fill=#4!60,fill opacity=#5,canvas is xy plane at z=#3] (0,0) circle (#2);
\end{scope}
}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,% isometric axes:
x={(-\xx cm,-\xy cm)},y={(\xx cm,-\xy cm)},z={(0 cm,\zy cm)},%
rotate=-100] % but rotated 100 degrees clockwise
%
\cylinder{(0,0,0)} {0.75}{4} {gray}{1}
\cylinder{(0,0,0)} {1.25}{4} {red} {0.8}
\cylinder{(0,0,0)} {1.5} {4} {blue}{0.7}
\cylinder{(0,0,4)} {0.75}{1.5} {gray}{1}
\cylinder{(0,0,4)} {1.25}{1.5} {red} {0.8}
\cylinder{(0,0,5.5)}{0.75}{1.25}{gray}{1}
\end{tikzpicture}
\end{document}