如何用 tikz 绘制多位置圆柱体

如何用 tikz 绘制多位置圆柱体

我想绘制具有不同方向的圆柱体(tikz 形状)。因此我希望圆柱体(2)与 x 轴平行。

我测试了不同的参数值,我无法感谢您的帮助

在此处输入图片描述

\documentclass{article}
\usepackage{tikz,esvect}
\usetikzlibrary{3d,calc,shapes}
\begin{document}    
\begin{tikzpicture}[x={(-0.2cm,-0.4cm)}, y={(1cm,0cm)}, z={(0cm,1cm)}]
\draw[-latex] (0,0,0) coordinate(O) -- (5,0,0) node[above]{$\vv{x}$};
\draw[-latex] (0,0,0) coordinate(O) -- (0,5,0) node[above]{$\vv{y}$};
\draw[-latex] (0,0,0) coordinate(O) -- (0,0,5) node[right]{$\vv{z}$};

\tikzset{zxplane/.style={canvas is zx plane at y=#1,very thin}}
\tikzset{yxplane/.style={canvas is yx plane at z=#1,very thin}}

   \begin{scope}[yxplane=3]
   \draw[dashed] (0,0) circle[radius=5cm] ;
   \coordinate(C) at (0,5);
   \draw[dashed] (0,0) -- (C);
   \draw[thick,blue] ($(C)+(-0.25,-0.5)$) --++(-0.5,0.5)--++(+0.5,0.5);

   \node [cylinder,draw=black,thick,aspect=1.5,minimum height=2cm,minimum width=1cm,shape border rotate=90,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C)-(0,0,-1)$){1};
      \draw[thick,blue] ($(C)+(0.25,-0.5)$) --++(0.5,0.5)--++(-0.5,0.5);
   \coordinate(C2) at (5,0);
      \node [cylinder,draw=black,thick,aspect=1,minimum height=2cm,minimum width=1cm,shape border      
   rotate=-90,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C2)-(0,0,-1)$){2};

   \coordinate(C3) at (2.5,0);
      \node [cylinder,draw=black,thick,aspect=0.5,minimum height=2cm,minimum width=1cm,shape border      
   rotate=0,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C3)-(0,0,-1)$){3};


 \end{scope}
    \begin{scope}[yxplane=0]

   \coordinate(C4) at (2.5,0);
      \node [cylinder,draw=black,thick,aspect=1.5,minimum height=2cm,minimum width=1cm,shape border      
   rotate=90,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C4)-(0,0,-1)$){4};

    \end{scope}

\draw[dashed]  (C2) -- (C3) --(C4);
\end{tikzpicture}

\end{document}

答案1

请记住,节点是使用屏幕坐标而不是画布坐标绘制的,因此您必须直接进行所有 3D 校正。

要构建正交轴,最简单的方法是选择两个角度并使用极坐标变换。您也可以使用 tikz-3d,但它有自己的缺点。

给定轴,您可以确定给定平面的 y 半径与 x 半径的比率。对于 1cm 宽的垂直圆柱体,x 半径为 0.5cm,y 半径为 0.2cm 乘以纵横比参数。这意味着应该将所需的纵横比乘以 2.5

由于 y 轴不再是水平的,圆柱体 2 和 3 稍微旋转了一点。

\documentclass{article}
\usepackage{tikz,esvect}
\usetikzlibrary{3d,calc,shapes}
\begin{document}    

\def\mytheta{10}% POV angles
\def\myphi{20}

\pgfmathparse{sin(\mytheta)}
\let\xx=\pgfmathresult
\pgfmathparse{cos(\mytheta)}
\let\yx=\pgfmathresult
\pgfmathparse{sin(\myphi)}
\let\zxy=\pgfmathresult
\pgfmathparse{\yx * \zxy}
\let\xy=\pgfmathresult
\pgfmathparse{\xx * \zxy}
\let\yy=\pgfmathresult
\pgfmathparse{cos(\myphi)}
\let\zz=\pgfmathresult
\pgfmathparse{atan(\yy/\yx)}
\let\rotate=\pgfmathresult

y radius / x radius for xy plane = \zxy

x radius / y radius for xz plane = \xx

y radius / x radius for yz plane = \zz

\begin{tikzpicture}[x={(-\xx cm,-\xy cm)}, y={(\yx cm,-\yy cm)},
   z={(0cm,\zz cm)}]
\draw[-latex] (0,0,0) coordinate(O) -- (5,0,0) node[above]{$\vv{x}$};
\draw[-latex] (0,0,0) coordinate(O) -- (0,5,0) node[above]{$\vv{y}$};
\draw[-latex] (0,0,0) coordinate(O) -- (0,0,5) node[right]{$\vv{z}$};

\tikzset{zxplane/.style={canvas is zx plane at y=#1,very thin}}
\tikzset{yxplane/.style={canvas is yx plane at z=#1,very thin}}

   \begin{scope}[yxplane=3]
   \draw[dashed] (0,0) circle[radius=5cm] ;
   \coordinate(C) at (0,5);
   \draw[dashed] (0,0) -- (C);
   \draw[thick,blue] ($(C)+(-0.25,-0.5)$) --++(-0.5,0.5)--++(+0.5,0.5);

   \pgfmathparse{2.5 * \zxy}
   \let\aspect=\pgfmathresult
   \node [cylinder,draw=black,thick,aspect={\aspect},minimum height=2cm,minimum width=1cm,shape border rotate=90,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C)-(0,0,-1)$){1};
      \draw[thick,blue] ($(C)+(0.25,-0.5)$) --++(0.5,0.5)--++(-0.5,0.5);
   \coordinate(C2) at (5,0);
      \pgfmathparse{2.5 * \zz}
      \let\aspect=\pgfmathresult
      \node [cylinder,draw=black,thick,aspect={\aspect},minimum height=2cm,minimum width=1cm,shape border rotate=-90,
      rotate={-\rotate},cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C2)-(0,0,-1)$){2};

   \coordinate(C3) at (2.5,0);
      \pgfmathparse{2.5 *\xx}
      \let\aspect=\pgfmathresult
      \node [cylinder,draw=black,thick,aspect=0.5,minimum height=2cm,minimum width=1cm,rotate={-\rotate},
   cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C3)-(0,0,-1)$){3};


 \end{scope}
    \begin{scope}[yxplane=0]
   \pgfmathparse{2.5 * \zxy}
   \let\aspect=\pgfmathresult
   \coordinate(C4) at (2.5,0);
      \node [cylinder,draw=black,thick,aspect={\aspect},minimum height=2cm,minimum width=1cm,shape border      
   rotate=90,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C4)-(0,0,-1)$){4};

    \end{scope}

\draw[dashed]  (C2) -- (C3) --(C4);
\end{tikzpicture}

\end{document}

气缸

相关内容