使用相对放置时如何准确测量 TikZ 节点中心之间的距离?

使用相对放置时如何准确测量 TikZ 节点中心之间的距离?

我想在使用相对放置时准确测量 TikZ 节点中心之间的距离。我的答案是大约 1.55 厘米,但这只是大概的。 在此处输入图片描述

下面是我的代码:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary {positioning}
\begin{document}

\begin{tikzpicture}
[place/.style={circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=6mm},
transition/.style={rectangle,draw=black!50,fill=black!20,thick,
inner sep=0pt,minimum size=4mm}]


\node[place] (centeral node) {};
\node[transition] (right node) [right=of centeral node] {};


\draw [|-|] (centeral node.center) -- ++(1.55,0);

\end{tikzpicture}

\end{document}

答案1

由于我不明白这个问题,所以我决定写两个答案。

如果问题是如何将一个节点准确地放置在与另一个节点的一定距离处,那么如果您知道与初始节点的角度或者可以在其水平+垂直移动中分解距离,那么很容易做到这一点。

例如,在下面的代码中,right node.center位于 距离 的准确位置 1.55cm 处centeral node.center。无需测量它们之间的距离。

another place.center放置在精确的距离处,right node.center但角度为 60 度。同样,距离是已知的。

但如果问题是如何测量未知数centeral node.centeranother place.center未知数之间的距离,那么calc图书馆可以提供帮助。这是第三个例子。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary {positioning, calc}
\begin{document}

\begin{tikzpicture}
[place/.style={circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=6mm},
transition/.style={rectangle,draw=black!50,fill=black!20,thick,
inner sep=0pt,minimum size=4mm}]


\node[place] (centeral node) {};
\node[transition] (right node) [right=1.55 of centeral node] {};
\draw [|-|] (centeral node.center) -- (right node.center);

\path (right node.center) --++(60:2.13) node[place] (another place) {};
\draw[|-|] (right node.center) -- ++(60:2.13);

\draw[latex-latex] let \p1 = ($(another place.center)-(centeral node.center)$), \n1={veclen(\x1,\y1)} in (centeral node.center) --  node[above, sloped] {\n1} (another place.center);
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

  • 节点之间的距离等于node distance节点右侧和左侧边界之间的默认值,即(centeral node.east)和之间(right node.west)
  • 为了方便写出它们中心之间的距离,我把这个距离增加到22mm
  • 距离的计算方式与@Ignasi 类似回答(+1)借助calcTikZ 库:
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{calc,
                positioning,
                quotes}
                
\begin{document}
    \begin{tikzpicture}[
    node distance = 22mm,
     place/.style = {circle, draw=blue!50,thick, fill=blue!20,
                     inner sep=0pt,minimum size=6mm},
transition/.style = {draw, thick, fill=black!20,
                     inner sep=0pt,minimum size=4mm},
every edge quotes/.style = {auto, font=\scriptsize, inner sep=1pt}
                     ]
\node[place] (centeral node) {};
\node[transition] (right node) [right=of centeral node] {};
%
\draw[|-|]   
    let \p1 = ($(right node.center) - (centeral node.center)$) in
            (centeral node.center) %
    to ["\x1",
        "\pgfmathparse{scalar(\x1*0.35145980pt)} \pgfmathresult\,mm" '] % 1pt ~ 0.35145980mm
            (right node.center);  
    \end{tikzpicture}
\end{document}

在此处输入图片描述

附录;使用 的默认值node distance10mm(或者如果您愿意,也可以为 1 cm),减小边缘标签的字体大小并增加其inner sep以更好地读取测量值,在节点之间添加 10mm 长的红线,以显示 的默认值node distance,并设置outer sep=0pt它不影响节点距离,上述 MWE 变为:

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{calc,
                positioning,
                quotes}

\usepackage{siunitx}
\begin{document}
    \begin{tikzpicture}[
     place/.style = {circle, draw=blue!50,thick, fill=blue!20,
                     inner sep=0pt, outer sep=0pt, minimum size=6mm},
transition/.style = {draw, thick, fill=black!20,
                     inner sep=0pt, outer sep=0pt, minimum size=4mm},
every edge quotes/.style = {auto, font=\tiny, inner sep=5pt}
                     ]
\node[place] (centeral node) {};
\node[transition] (right node) [right=of centeral node] {};
%
\draw[|-|]
    let \p1 = ($(right node.center) - (centeral node.center)$) in
            (centeral node.center) %
    to ["\x1",
        "\pgfmathparse{scalar(\x1*0.351459803pt)}\pgfmathresult\,mm" '] % 1pt = 0.35145980mm
            (right node.center);
\draw[red] (centeral node.east) -- ++ (10mm,0);% for show default distance between nodes
    \end{tikzpicture}
\end{document}

其结果是:

在此处输入图片描述

值 14,9998 mm ~ 15mm 等于:

<default node distance> + (1/2)<minimum size of left node (=3mm)>+ (1/2)<minimum size of right node (=2mm)>

相关内容