该图试图绘制机器人操纵器的关节和连杆。最初,我将关节的点定义为node
s,这导致后来在它们之间绘制线条时发生了一些偏移。根据 SE 的回答,我将其改为一个三步过程,首先定义coordinate
s,然后在坐标处绘制关节,然后用线将它们连接起来。
但是,现在线条进入了关节内部。我希望拥有节点的属性,即线条只绘制到节点的边界和坐标的属性,其中线条精确地绘制到坐标?(我意识到我可以在绘制链接后简单地绘制关节从而覆盖它们,但出于其他原因,我更喜欢将节点作为未填充的圆圈,如本图所示)
我如何实现这个目标?
下面是 MWE 及其带有节点和坐标的结果。
\documentclass[tikz]{standalone}
\usetikzlibrary{math}
\begin{document}
\foreach \mynodes/\numnodes in {{1/0,2/90,3/0}/3}{%
\begin{tikzpicture}[scale=1]
\draw (-1,-1) [help lines] grid (3,2);
% define coordinates for joints
\node (0,0)(n0){};
\path (0,0)
foreach \nodeid/\nodeangle in \mynodes{
++(\nodeangle:1) node(n\nodeid){}
};
%draw links
\draw [red, ultra thick](0,0)
foreach \nodeid in {1,...,\numnodes}{
-- (n\nodeid)
};
%draw joints
\foreach \nodeid in {0,1,...,\numnodes}{%
\draw [black, thick](n\nodeid) circle [radius=1mm];
};
\end{tikzpicture}
}
\end{document}
答案1
另一种可能性是“一步到位”的方法,在我看来,这种方法更清晰、更简单。为此,我使用 TikZ 库calc
:
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[
every node/.style = {circle, draw=red, thick,
minimum size=2mm, inner sep=0pt}
]
\draw (-1,-1) [help lines] grid (3,2);
% graph
\node (n1) at (0,0) {};
\foreach \nodeangle [count=\lastid from 1, count=\nodeid from 2] in {0,90,0}
{
\node (n\nodeid) at ($(n\lastid) + (\nodeangle:10mm)$) {};
\draw (n\lastid) -- (n\nodeid);
}
\end{tikzpicture}
\end{document}
结果:
编辑: 上面提出的解决方案只有一个 Tikz 图片,而您的方法中有三个。有了它,您就有了三个独立的图片,它们相互重叠。在这种情况下,一些关于正确绘制节点的信息似乎丢失了。
答案2
您可以分段绘制路径:
\documentclass[tikz]{standalone}
\usetikzlibrary{math}
\begin{document}
\foreach \mynodes/\numnodes in {{1/0,2/90,3/0}/3}{%
\begin{tikzpicture}[scale=1]
\draw (-1,-1) [help lines] grid (3,2);
% define coordinates for joints
\node (0,0)(n0){};
\path (0,0)
foreach \nodeid/\nodeangle in \mynodes{
++(\nodeangle:1) node(n\nodeid){}
};
%draw links
\foreach \nodeid in {1,...,\numnodes}{
\draw [red, ultra thick](n\the\numexpr\nodeid-1) --(n\nodeid);
};
%draw joints
\foreach \nodeid in {0,1,...,\numnodes}{%
\draw [black, thick](n\nodeid) circle [radius=1mm];
};
\end{tikzpicture}
}
\end{document}
答案3
也许您可以考虑使用chains
库来绘制这种图表。在向链中添加元素的同时,还会绘制它们之间的连接。
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[%
on grid,
scale=1,
start chain=going right,
every join/.style={thick, red, shorten >=1pt, shorten <=1pt},
item/.style={
draw,
thick,
on chain,
circle,
minimum size=3mm,
inner sep=0pt,
join
}
]
\draw (-1,-1) [help lines] grid (3,2);
\node [item] {};
\node [item] {};
\node [item, on chain= going above] {};
\node [item] {};
\end{tikzpicture}
\end{document}
更新
使用chains
不限制节点之间的任何放置关系。可以将所有节点放置在chain
它们上面,或者在它们之间形成chain
和时将它们放置在您想要的位置joining
。
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[%
scale=1,
start chain,
every join/.style={thick, red, shorten >=1pt, shorten <=1pt},
item/.style={
draw,
thick,
on chain,
circle,
minimum size=3mm,
inner sep=0pt,
join
}
]
\foreach \i in {1,...,5}
\node [item] (\i) at (3*rand,3*rand) {};
\end{tikzpicture}
\end{document}