Tikz:重复使用情节的部分并使用参数?

Tikz:重复使用情节的部分并使用参数?

我已经使用 Tikz 制作了一个立方体,现在我想在不同位置制作它的多个实例。因此,我设想使用参数化pic或“正常编程函数”之类的东西来重复使用我的立方体。图片的问题在于,我已经使用图片绘制了立方体的各个部分。所以我需要图片中的图片。

作为参数,我希望获得立方体的位置和理想的方向,两个框架固定在立方体中,但随立方体旋转。因此,理想情况下,存在某种分组,并且所有内容都随设置旋转。我该如何实现这一点?

谢谢!

在此处输入图片描述

\documentclass[border=1cm]{standalone}
\usepackage{tikz}

\usetikzlibrary{shapes.geometric, arrows.meta, 3d, calc, decorations.markings,shapes,positioning, backgrounds, scopes}
\usepackage{tikz-3dplot}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground} 

\begin{document}
\begin{tikzpicture}[>=latex,scale=1.5,line cap=round, line join =round]

\tikzset{pics/coordsys/.style n args={4}{ code = { 
        \draw [->, #1] (0,0,0) -- +(1,0,0)[red] node [pos=1.3]{#2}; 
        \draw [->, #1] (0,0,0) -- +(0,1,0)[green!80!black] node [pos=1.2]{#3}; 
        \draw [->, #1] (0,0,0) -- +(0,0,1)[blue] node [pos=1.2]{#4}; } }} 

\pgfmathsetmacro{\cubex}{5}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{5}
\newcommand{\boxfillcolor}{yellow!20!}
\newcommand{\boxframecolor}{gray!20!}
\begin{scope}
    \draw[\boxframecolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \draw[\boxframecolor] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \draw[\boxframecolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{scope}
\begin{scope}[on background layer]
    \fill[\boxfillcolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \fill[\boxfillcolor] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \fill[\boxfillcolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{scope}

\pgfmathsetmacro{\dx}{0.6};
\pgfmathsetmacro{\dy}{2};
\pgfmathsetmacro{\dz}{0.3};
\coordinate (origin) at (-5,-3,0);
\coordinate (M) at (-\dx,-\cubey/\dy,-\dz);
\coordinate (B) at (-\cubex+\dx,-\cubey/\dy,-\cubez+\dz);

\draw (M) pic {coordsys={thick}{$x_M$}{$y_M$}{$z_M$}};
\node [below right] at (M.south) {\textit{M}};
\draw (B) pic {coordsys={dashed}{$x_M'$}{$y_M'$}{$z_M'$}};
\node [above left] at (B.west) {\textit{B}};

\tdplotsetmaincoords{0}{0};
\tdplotsetrotatedcoords{20}{-60}{10};
\tdplotsetrotatedcoordsorigin{(B)};
\draw [tdplot_rotated_coords] (B) pic {coordsys={thick}{$x_{B}$}{$y_{B}$}{$z_{B}$}}; 

\draw [->, thick] (M) -- (B) node [midway,fill=\boxfillcolor] {$q_{MB}, r_{MB}$};

\end{tikzpicture}
\end{document}

答案1

这不是一个考虑到所有参数的完整答案,而是告诉您如何从原则上使其发挥作用。

  1. 据我所知,你可以嵌套pics。至少在下面的例子中它是有效的。所以你pic也可以把更大的东西做成 a 。
  2. 将您想要调整的所有内容存储在 pgf 键中。我对立方体的尺寸、颜色和轴标签都这样做了。如果您想添加旋转角度等,您只需添加更多 pgf 键。它们可以针对每张图片进行更改。与具有固定数量参数的样式相比,这是最大的优势之一:您始终可以添加一些参数而不会破坏向下兼容性,即您的旧代码仍将有效。

我还清理了你的序言。

\documentclass[tikz,border=1cm]{standalone}
\usepackage{tikz-3dplot} % loads 3d and calc libraries
\usetikzlibrary{backgrounds} % cleaned up

\begin{document}
\begin{tikzpicture}[>=latex,line cap=round, line join =round]

\tikzset{pics/coordsys/.style={
    code = {\tikzset{coordsys/.cd,#1}
        \draw [->,pic actions] (0,0,0) -- +(1,0,0)[red] node[pos=1.1]
        {$\pgfkeysvalueof{/tikz/coordsys/x}$};
        \begin{scope}[on background layer]
        \draw [->,pic actions] (0,0,0) -- +(0,1,0)[green!60!black] node[pos=1.1]
        {$\pgfkeysvalueof{/tikz/coordsys/y}$};
        \end{scope}
        \draw [->,pic actions] (0,0,0) -- +(0,0,1)[blue] node[pos=1.1]
        {$\pgfkeysvalueof{/tikz/coordsys/z}$};
    }
},coordsys/.cd,x/.initial=x,y/.initial=y,z/.initial=z} 

\tikzset{pics/complicated/.style={code={\tikzset{complicated/.cd,#1}
\begin{scope}
    \draw[\boxframecolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \draw[\boxframecolor] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \draw[\boxframecolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{scope}
\begin{scope}[on background layer]
    \fill[\boxfillcolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \fill[\boxfillcolor] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \fill[\boxfillcolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{scope}

%\draw (-\cubex+0.2,-\cubey/2,-0.6) coordinate (origin) pic {coordsys={very thick}{x}{y}{z}}; 
\pgfmathsetmacro{\dx}{0.6};
\pgfmathsetmacro{\dy}{2};
\pgfmathsetmacro{\dz}{0.3};
\coordinate (origin) at (-5,-3,0);
\coordinate (M) at (-\dx,-\cubey/\dy,-\dz);
\coordinate (B) at (-\cubex+\dx,-\cubey/\dy,-\cubez+\dz);

\draw (M) pic[thick] {coordsys={x=x_M,y=y_M,z=z_M}};
\node [below right] at (M.south) {\textit{M}};
\draw (B) pic[dashed] {coordsys={x=x_M',y=y_M',z=z_M'}};
\node [above left] at (B.west) {\textit{B}};

\tdplotsetmaincoords{0}{0};
\tdplotsetrotatedcoords{20}{-60}{10};
\tdplotsetrotatedcoordsorigin{(B)};
%\begin{scope}[on background layer]
\draw [tdplot_rotated_coords] (B) pic[thick]{coordsys={x=x_{B},y=y_{B},z=z_{B}}}; 

\draw [->, thick] (M) -- (B) node [midway,fill=\boxfillcolor] {$q_{MB}, r_{MB}$};
}},complicated/.cd,cube x/.store in=\cubex,cube x=5,
cube y/.store in=\cubey,cube y=1,
cube z/.store in=\cubez,cube z=5,
box fill color/.store in=\boxfillcolor,box fill color=yellow!20!,
box frame color/.store in=\boxframecolor,box frame color=gray!20!,}
\path pic{complicated} (5,0,0) pic{complicated={box fill color=red!20}}
(10,0,0) pic{complicated={box fill color=blue!20,box frame color=blue}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容