我正在尝试绘制一个机器人的骨架模型,该模型由 4 个旋转关节组成,如下所示:
以下是我尝试绘制的:
\documentclass[tikz,border=2px]{standalone}
\usetikzlibrary{shapes.geometric, arrows}
\begin{document}
\tikzset{
line/.style = {-, draw=black!30, line width=1pt},
circl/.style = {draw, circle, minimum size=#1, fill=black!20},
deci/.style = {draw, diamond, minimum width=3mm, aspect=#1, fill=black!20},
box/.style = {draw=black!30, rectangle, minimum height=#1, minimum width=#1,line width=1pt}
}
\begin{tikzpicture}[node distance=2cm]
\node (crca) [circl=6mm]{};
\node (crcb) [circl=4mm, below of=crca, yshift=+2cm, label={[label distance=4mm]135:1}]{};
\node (deca) [deci=2, below of=crcb, label={[label distance=4mm]135:2}]{};
\node (decb) [deci=.5, below of=deca, label={[label distance=4mm]135:3}]{};
\node (decc) [deci=.5, right of=crca, label={[label distance=4mm]135:4}]{};
\node (boxa) [box=6mm, below of=decb]{};
\node (boxb) [box=8mm, right of=decc]{};
\draw [line] (crca) -- (decc);
\draw [line] (decc) -- (boxb);
\draw [line] (crca) -- (deca);
\draw [line] (deca) -- (decb);
\draw [line] (decb) -- (boxa);
\end{tikzpicture}
\end{document}
正如您所见,代码不断增长,我想它可以用更好的方式完成。以下是生成的 PDF 的屏幕截图:
我正在寻找一种更好的方法来生成所示的图表一开始这个问题
答案1
我会用pic
s 来表示大多数东西,并将它们放置在路径沿途。
pic
s 是一些小图片,您可以定义它们然后使用,同时可以灵活地旋转它们或以其他方式变换它们。它们的优点是它们比节点更容易定制。- 沿着路径放置物体具有一点优势,即您可以在指定物体的相对位置时进行绘制(而不是将物体放置在相对位置然后在单独的步骤中将它们连接起来)。
chamfered rectangle
可以使用或将路径末端的节点带入屏幕截图的形式path picture
。
因此,第一步是绘制路径,其中|-
语法有助于绘制 90 度角。然后可以通过语法添加图片pos=...
。它们可以旋转等等。将它们放入local bounding box
es 中允许我们访问它们的边界框锚点,这在添加蓝色箭头的循环中用到。
\documentclass[tikz,border=2px]{standalone}
\usetikzlibrary{arrows.meta,bending,shapes.misc}
\begin{document}
\tikzset{
line/.style = {-, draw=black!30, line width=1pt},
box/.style = {draw=black!30, rectangle, minimum height=#1,
minimum width=#1,line width=1pt},
pics/.cd,
dimmy/.style={code={
\draw[black,line width=1pt,fill=gray!50] (0,0.5) -- (0.4,0) -- (0,-0.5) -- (-0.4,0) -- cycle;
\draw[black,line width=1pt] (0,-0.5) -- (0,0.5);
\fill[red] (-30:0.2) -- (-150:0.2) -- (90:0.2) -- cycle;
}},
cilly/.style={code={
\draw[black,line width=1pt,fill=gray!50] (0,0) circle (0.5);
\draw[black,line width=1pt,fill=white] (0,0)circle (0.3);
\draw[line width=2pt,red,-{Triangle[bend,length=2pt,width=3.5pt]}] (270:0.4) arc (270:90:0.4);
}}
}
\begin{tikzpicture}
\draw[line]
(0,0) node[box=8mm,chamfered rectangle,chamfered rectangle corners={south west,
south east},chamfered rectangle xsep=2pt,below] (b1) {}|- ++ (0.8,0.2) |-
++(-1.6,0.75)
pic[pos=0.75,local bounding box=d1]{dimmy} |- ++ (0.8,0.75)
|- pic[pos=0.175,rotate=-90,local bounding box=d2]{dimmy} ++ (3.5,2)
pic[pos=0.5,local bounding box=c1]{cilly}
pic[pos=0.75,local bounding box=d3,rotate=180]{dimmy}
-- ++ (0,0.2) node[above,box=8mm,path picture={
\draw[line] ([yshift=-1mm]path picture bounding box.north west)
-- ([yshift=-1mm]path picture bounding box.north east);
\draw[line] ([yshift=1mm]path picture bounding box.south west)
-- ([yshift=1mm]path picture bounding box.south east);}] (b2){};
\foreach \X [count=\Y] in {d3.north,c1.north west,d2.west,d1.west}
{\ifnum\Y=2
\draw [latex-,blue,shorten <=-5pt] (\X) -- ++ (-0.5,0.5)
node[above left=-2pt,black]{\Y};
\else
\draw [latex-,blue,shorten <=2pt] (\X) -- ++ (-0.5,0.5)
node[above left=-2pt,black]{\Y};
\fi}
\end{tikzpicture}
\end{document}