镜像 tikz 形状

镜像 tikz 形状

我正在尝试绘制一个切割圆柱体,并且为此获得了一些代码,但是当我尝试镜像它时,参数不起作用\begin{scope},我不明白背后的原因。这是 MWE:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}

\begin{tikzpicture}[
pics/fcylinder/.style={code={
 \tikzset{fcylinder/.cd,#1}
 \def\pv##1{\pgfkeysvalueof{/tikz/fcylinder/##1}}
 % light color part of the cylinder
 \draw[fill=gray!30] (0,\pv{r}) 
    arc[start angle=90,end angle=270,x radius=\pv{r}*\pv{aspect},y radius=\pv{r}]
    -- (\pv{h},-\pv{r}) to[out=180,in=-120] 
    (\pv{h},0) to[out=60,in=0] (\pv{h},\pv{r}) -- cycle;
 % dark color part of the cylinder
 \draw[fill=gray!60]  (\pv{h},-\pv{r}) to[out=180,in=-120]  (\pv{h},0)
 to[out=-60,in=0] cycle;
 \draw[fcylinder/finish] (0,\pv{r}) 
  arc[start angle=90,end angle=-90,x radius=\pv{r}*\pv{aspect},y radius=\pv{r}];
}},fcylinder/.cd,r/.initial=5mm,h/.initial=1cm,aspect/.initial=1/3,
    finish/.style={solid},
    %
]


\begin{scope}[xshift=4.5cm, yshift=1.5cm, local bounding box=inset]
  \path (1,0) pic{fcylinder} node[below right,yshift=-20pt] {};
  \draw[decoration={aspect=0.3, segment length=1.5mm, amplitude=2.5mm, pre length=0.7mm, post length=0.mm, coil}, thick, decorate] (0,0) -- node[above, yshift=7pt] {}++ (1,0);
\end{scope}


\begin{scope}[yscale=1,xscale=-1, xshift=-2.5cm, yshift=1.5cm, local bounding box=inset]

  \path (-1,0) pic{fcylinder} node[below right,yshift=-20pt] {};

\end{scope}

\end{tikzpicture}
\end{document}

结果是

在此处输入图片描述

正如您所看到的,我希望将左侧切割杆倒置,以便曲面远离弹簧。

答案1

我认为您需要一个不同的。这是将选项应用于 时pic发生的情况。xscale=-1pic

在此处输入图片描述

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}

\begin{tikzpicture}[
pics/fcylinder/.style={code={
 \tikzset{fcylinder/.cd,#1}
 \def\pv##1{\pgfkeysvalueof{/tikz/fcylinder/##1}}
 % light color part of the cylinder
 \draw[fill=gray!30] (0,\pv{r}) 
    arc[start angle=90,end angle=270,x radius=\pv{r}*\pv{aspect},y radius=\pv{r}]
    -- (\pv{h},-\pv{r}) to[out=180,in=-120] 
    (\pv{h},0) to[out=60,in=0] (\pv{h},\pv{r}) -- cycle;
 % dark color part of the cylinder
 \draw[fill=gray!60]  (\pv{h},-\pv{r}) to[out=180,in=-120]  (\pv{h},0)
 to[out=-60,in=0] cycle;
 \draw[fcylinder/finish] (0,\pv{r}) 
  arc[start angle=90,end angle=-90,x radius=\pv{r}*\pv{aspect},y radius=\pv{r}];
}},fcylinder/.cd,r/.initial=5mm,h/.initial=1cm,aspect/.initial=1/3,
    finish/.style={solid},
    %
]


\begin{scope}[xshift=4.5cm, yshift=1.5cm, local bounding box=inset]
  \path (1,0) pic{fcylinder} node[below right,yshift=-20pt] {};
  \path (0,0) pic[xscale=-1]{fcylinder} node[below right,yshift=-20pt] {};
  \draw[decoration={aspect=0.3, segment length=1.5mm, amplitude=2.5mm, pre length=0.7mm, post length=0.mm, coil}, thick, decorate] (0,0) -- node[above, yshift=7pt] {}++ (1,0);
\end{scope}

\end{tikzpicture}
\end{document}

更新

我认为用left圆柱体看起来会更好看:

在此处输入图片描述

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}

\begin{tikzpicture}[
pics/fcylinder/.style={%
    code={%
        \tikzset{fcylinder/.cd,#1}
        \def\pv##1{\pgfkeysvalueof{/tikz/fcylinder/##1}}
        % light color part of the cylinder
        \draw[fill=gray!30] (0,\pv{r}) 
                arc[start angle=90,end angle=270,x radius=\pv{r}*\pv{aspect},y radius=\pv{r}]
                -- (\pv{h},-\pv{r}) to[out=180,in=-120] 
                (\pv{h},0) to[out=60,in=0] (\pv{h},\pv{r}) -- cycle;
        % dark color part of the cylinder
        \draw[fill=gray!60]  (\pv{h},-\pv{r}) to[out=180,in=-120]  (\pv{h},0)
                to[out=-60,in=0] cycle;
        \draw[fcylinder/finish] (0,\pv{r}) 
            arc[start angle=90,end angle=-90,x radius=\pv{r}*\pv{aspect},y radius=\pv{r}];
    }
},
pics/leftfcylinder/.style={%
    code={%
        \tikzset{fcylinder/.cd,#1}
        \def\pv##1{\pgfkeysvalueof{/tikz/fcylinder/##1}}
        \begin{scope}[xscale=-1]
        % light color part of the cylinder
        \draw[fill=gray!30] (0,\pv{r}) 
                arc[start angle=90,end angle=270,x radius=\pv{r}*\pv{aspect},y radius=\pv{r}]
                -- (\pv{h},-\pv{r}) to[out=180,in=-120] 
                (\pv{h},0) to[out=60,in=0] (\pv{h},\pv{r}) -- cycle;
        % dark color part of the cylinder
        \draw[fill=gray!60]  (\pv{h},-\pv{r}) to[out=180,in=-120]  (\pv{h},0)
                to[out=-60,in=0] cycle;
        \end{scope}
    }
},
    fcylinder/.cd,
    r/.initial=5mm,
    h/.initial=1cm,
    aspect/.initial=1/3,
    finish/.style={solid},
    %
]
\begin{scope}[xshift=4.5cm, yshift=1.5cm, local bounding box=inset]
  \path (1,0) pic{fcylinder} node[below right,yshift=-20pt] {};
  \draw[decoration={aspect=0.3, segment length=1.5mm, amplitude=2.5mm, pre length=0.7mm, post length=0.mm, coil}, thick, decorate] (0,0) -- node[above, yshift=7pt] {}++ (1,0);
  \path (0,0) pic{leftfcylinder} node[below right,yshift=-20pt] {};
\end{scope}

\end{tikzpicture}
\end{document}

相关内容