我正在尝试自动绘制节点之间的连接,这样如果蓝色圆圈重叠,它们之间就会绘制连接。我觉得这可以用 if 函数来实现,但我完全不知道如何实现它?
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[ele/.style={fill=black,circle,minimum width=2pt,inner sep=1pt}]
\begin{scope}[elem/.style={circle, draw = black, line width = 0.2pt, fill = blue!60,fill opacity=0.3, minimum width = 30pt}]
\node[elem] (a) at (2.8209087137275, 1.18093727264829) {};
\node[elem] (b) at (2.95833231728538, 3.58594837328201) {};
\node[elem] (c) at (0.311441915093690, 1.55228774215654) {};
\node[elem] (d) at (1.69060866126193, 3.54516924716448) {};
\node[elem] (e) at (1.31799287927655, 0.304557156170160) {};
\node[elem] (f) at (1.61347687509477, 4.79163906483583) {};
\node[elem] (g) at (2.27043233468693, 2.38135190156370) {};
\node[elem] (h) at (1.20381702801476, 1.12542610389225) {};
\node[elem] (i) at (0.919304590945695, 2.48862221210784) {};
\node[elem] (j) at (1.64512903092519, 2.01677357394698) {};
\node[elem] (k) at (1.24383442716840, 3.78580579686650) {};
\node[elem] (l) at (1.58646607060451, 2.93089871716034) {};
\node[elem] (m) at (0.874518089942222, 2.13017381291781) {};
\node[elem] (n) at (1.46214937616206, 4.79124963787566) {};
\node[elem] (o) at (2.63312304723148, 0.206799557528458) {};
\node[ele] (a) at (2.8209087137275, 1.18093727264829) {};
\node[ele] (b) at (2.95833231728538, 3.58594837328201) {};
\node[ele] (c) at (0.311441915093690, 1.55228774215654) {};
\node[ele] (d) at (1.69060866126193, 3.54516924716448) {};
\node[ele] (e) at (1.31799287927655, 0.304557156170160) {};
\node[ele] (f) at (1.61347687509477, 4.79163906483583) {};
\node[ele] (g) at (2.27043233468693, 2.38135190156370) {};
\node[ele] (h) at (1.20381702801476, 1.12542610389225) {};
\node[ele] (i) at (0.919304590945695, 2.48862221210784) {};
\node[ele] (j) at (1.64512903092519, 2.01677357394698) {};
\node[ele] (k) at (1.24383442716840, 3.78580579686650) {};
\node[ele] (l) at (1.58646607060451, 2.93089871716034) {};
\node[ele] (m) at (0.874518089942222, 2.13017381291781) {};
\node[ele] (n) at (1.46214937616206, 4.79124963787566) {};
\node[ele] (o) at (2.63312304723148, 0.206799557528458) {};
\end{scope}
\end{tikzpicture}
\end{document}
答案1
这可以通过对所有节点进行双重 for 循环来实现。对于每一对节点,计算两个节点之间的距离,如果距离小于某个阈值,则在两个节点之间绘制一条边。
可以使用一些内置的 pgf 数学函数来计算两个节点之间的距离(或者更准确地说是两个节点中心之间的距离),即\pgfpointdiff
(计算两个点之间的矢量)和\pgfmathveclen
(计算矢量的长度)。要在 if 语句中实际使用它,您可以使用它将\pgfmathparse
浮点比较转换为布尔值 1 或 0。
通过反复试验找到了节点距离的阈值(此处为 28.3)。请注意,它还不完全正确,因为顶部的节点与下面的节点没有连接。实际上可能可以计算出两个圆是否重叠,但这留作练习 :)
\documentclass{article}
\usepackage{tikz}
\makeatletter
% adapted from https://tex.stackexchange.com/a/63838/
\newcommand\setveclength[3]{% newmacro, node1, node2
\pgfpointdiff{\pgfpointanchor{#2}{center}}{\pgfpointanchor{#3}{center}}
\pgfmathveclen{\pgf@x}{\pgf@y}
\edef#1{\pgfmathresult}
}
\makeatother
\begin{document}
\begin{tikzpicture}[ele/.style={fill=black,circle,minimum width=2pt,inner sep=1pt}]
\begin{scope}[elem/.style={circle, draw = black, line width = 0.2pt, fill = blue!60,fill opacity=0.3, minimum width = 30pt}]
\node[elem] (a) at (2.8209087137275, 1.18093727264829) {};
\node[elem] (b) at (2.95833231728538, 3.58594837328201) {};
\node[elem] (c) at (0.311441915093690, 1.55228774215654) {};
\node[elem] (d) at (1.69060866126193, 3.54516924716448) {};
\node[elem] (e) at (1.31799287927655, 0.304557156170160) {};
\node[elem] (f) at (1.61347687509477, 4.79163906483583) {};
\node[elem] (g) at (2.27043233468693, 2.38135190156370) {};
\node[elem] (h) at (1.20381702801476, 1.12542610389225) {};
\node[elem] (i) at (0.919304590945695, 2.48862221210784) {};
\node[elem] (j) at (1.64512903092519, 2.01677357394698) {};
\node[elem] (k) at (1.24383442716840, 3.78580579686650) {};
\node[elem] (l) at (1.58646607060451, 2.93089871716034) {};
\node[elem] (m) at (0.874518089942222, 2.13017381291781) {};
\node[elem] (n) at (1.46214937616206, 4.79124963787566) {};
\node[elem] (o) at (2.63312304723148, 0.206799557528458) {};
\node[ele] (a1) at (2.8209087137275, 1.18093727264829) {};
\node[ele] (b1) at (2.95833231728538, 3.58594837328201) {};
\node[ele] (c1) at (0.311441915093690, 1.55228774215654) {};
\node[ele] (d1) at (1.69060866126193, 3.54516924716448) {};
\node[ele] (e1) at (1.31799287927655, 0.304557156170160) {};
\node[ele] (f1) at (1.61347687509477, 4.79163906483583) {};
\node[ele] (g1) at (2.27043233468693, 2.38135190156370) {};
\node[ele] (h1) at (1.20381702801476, 1.12542610389225) {};
\node[ele] (i1) at (0.919304590945695, 2.48862221210784) {};
\node[ele] (j1) at (1.64512903092519, 2.01677357394698) {};
\node[ele] (k1) at (1.24383442716840, 3.78580579686650) {};
\node[ele] (l1) at (1.58646607060451, 2.93089871716034) {};
\node[ele] (m1) at (0.874518089942222, 2.13017381291781) {};
\node[ele] (n1) at (1.46214937616206, 4.79124963787566) {};
\node[ele] (o1) at (2.63312304723148, 0.206799557528458) {};
\foreach \firstnode in {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o}{%
\foreach \secondnode in {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o}{%
\setveclength{\mydist}{\firstnode}{\secondnode}
% adapted from https://tex.stackexchange.com/a/294691/
\pgfmathparse{\mydist < 28.3 ? int(1) : int(0)}
\ifnum\pgfmathresult=1
\draw (\firstnode.center) -- (\secondnode.center);
\fi
}
}
\end{scope}
\end{tikzpicture}
\end{document}
结果: