tikz:可重新定位、可旋转、可着色的物体,附示例(但没有填充)

tikz:可重新定位、可旋转、可着色的物体,附示例(但没有填充)

我承认我发布这篇文章的部分原因是 tikz 太酷了。使用 .pic 功能的实验展示了如何定义一个简单的对象,并将其放置在图中的任何位置,缩放、旋转等。我唯一不明白的是为什么填充似乎没有被拾取。这可能是我的错误或 pic 的限制。

\documentclass[border=3mm,12pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\def\centerarc[#1](#2)(#3:#4:#5)% [draw options] (center) (initial angle:final angle:radius)
{ \draw[#1] ($(#2)+({#5*cos(#3)},{#5*sin(#3)})$) arc (#3:#4:#5); }

\tikzset{
  speaker/.pic={
    \draw (-1,-0.5) -- (0,-0.5) -- (1,-1.5) -- (1,1.5) -- (0,0.5) -- (-1,0.5) -- (-1,-0.5);
    % sound waves
    \centerarc[red,thin](1,0)(-60:60:0.5);
    \centerarc[thin](1,0)(-60:60:1);
    \centerarc[red,thin](1,0)(-60:60:1.5);
  }
}

\noindent%
\fbox{
  \begin{tikzpicture}

    \draw[step=1, lightgray, very thin] (-1,-1) grid (11,11);
    \node[draw] at (0,0) {\tiny 0,0};
    \node[draw] at (10,10) {\tiny 10,10};

    \pic at (5,5) {speaker};

    \pic[rotate=90] at (2,2) {speaker};

    \pic[rotate=135,scale=0.5] at (2,7) {speaker};

    \pic[rotate=135,scale=0.5,blue, fill=yellow] at (7,2) {speaker};

  \end{tikzpicture}
}

\end{document}

扬声器示意图

答案1

您可以使用pic actions来告诉pic定义中的部分或所有操作使用随\pic命令传递的选项。例如:

\documentclass[border=10pt,tikz,multi]{standalone}
\usetikzlibrary{calc}
\begin{document}
\def\centerarc[#1](#2)(#3:#4:#5)% [draw options] (center) (initial angle:final angle:radius)
{\draw[#1] ($(#2)+({#5*cos(#3)},{#5*sin(#3)})$) arc (#3:#4:#5);}
\tikzset{%
  speaker/.pic={%
    \draw [pic actions] (-1,-0.5) -- (0,-0.5) -- (1,-1.5) -- (1,1.5) -- (0,0.5) -- (-1,0.5) -- (-1,-0.5);
    % sound waves
    \centerarc[red, thin, pic actions](1,0)(-60:60:0.5);
    \centerarc[thin, pic actions](1,0)(-60:60:1);
    \centerarc[red, thin, pic actions](1,0)(-60:60:1.5);
  }
}
\begin{tikzpicture}

  \draw[step=1, lightgray, very thin] (-1,-1) grid (11,11);
  \node[draw] at (0,0) {\tiny 0,0};
  \node[draw] at (10,10) {\tiny 10,10};

  \pic at (5,5) {speaker};

  \pic[rotate=90] at (2,2) {speaker};

  \pic[rotate=135,scale=0.5] at (2,7) {speaker};

  \pic[rotate=135,scale=0.5,blue, fill=yellow] at (7,2) {speaker};

\end{tikzpicture}
\end{document}

生产

图片动作

或者

\documentclass[border=10pt,tikz,multi]{standalone}
\usetikzlibrary{calc}
\begin{document}
\def\centerarc[#1](#2)(#3:#4:#5)% [draw options] (center) (initial angle:final angle:radius)
{\draw[#1] ($(#2)+({#5*cos(#3)},{#5*sin(#3)})$) arc (#3:#4:#5);}
\tikzset{%
  speaker/.pic={%
    \draw [pic actions] (-1,-0.5) -- (0,-0.5) -- (1,-1.5) -- (1,1.5) -- (0,0.5) -- (-1,0.5) -- (-1,-0.5);
    % sound waves
    \centerarc[red, thin](1,0)(-60:60:0.5);
    \centerarc[thin](1,0)(-60:60:1);
    \centerarc[red, thin](1,0)(-60:60:1.5);
  }
}
\begin{tikzpicture}

  \draw[step=1, lightgray, very thin] (-1,-1) grid (11,11);
  \node[draw] at (0,0) {\tiny 0,0};
  \node[draw] at (10,10) {\tiny 10,10};

  \pic at (5,5) {speaker};

  \pic[rotate=90] at (2,2) {speaker};

  \pic[rotate=135,scale=0.5] at (2,7) {speaker};

  \pic[rotate=135,scale=0.5,blue, fill=yellow] at (7,2) {speaker};

\end{tikzpicture}
\end{document}

生产

有限的图片操作

相关内容