1

I am trying to automatically draw connections between nodes, such that if the blue circles overlap, they are to have a connection drawn between them. I feel like this can this be done with an if function, but I have absolutely no idea how to implement it?

\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 Answers1

2

This can be done with a double for loop over all nodes. For each pair you calculate the distance between the two nodes, and if it is smaller than a certain threshold you draw an edge between the two nodes.

Calculating the distance between two nodes (or more precisely between two node centers) can be done using some built-in pgf math functions, i.e., \pgfpointdiff (calculates a vector between two points) and \pgfmathveclen (calculates the length of a vector). To actually use that in an if statement you can use \pgfmathparse to turn a float comparison into a boolean 1 or 0.

The threshold value for the node distance (here 28.3) was found with trial and error. Note that it is not completely correct yet, as the node on the top is not connected to the one below. It is probably possible to actually calculate if two circles overlap, but that is left as an exercise :)

\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}

Result:

enter image description here

Marijn
  • 37,699