2

I want to draw an intersection graph and for this purpose I have an array with coordinates.

Now I loop through them and calculate the distance between the coordinates and if it below the radius of the circles a line should get drawn.

I can calculate the distance with some kind of \path let \p1 ... \n1 = veclen() thing but then I can't use this distance in a comparison and only use it in the one path.

How could I proceed?

Here's my current tikzpicture:

\begin{tikzpicture}[scale=0.5]
\newcommand{\centers}{(0,1),(0,0),(5,3),(2,1),(0.5,1),(4,3),(0,5),(1.6,3.5),(6,0.5)}

\foreach [count=\i] \coord in \centers{\draw \coord circle(1) node{\i};} 
\foreach \coord in \centers{\fill \coord circle(0.15);}        


 \foreach [count=\i]\a in \centers{
    \foreach [count=\j]\b in \centers{
    \ifthenelse{\i < \j}{
   % calculating distance between \a and \b 
   % and drawing lines here
    }{};
  }
 }

\end{tikzpicture}

Here's a drawing of what I want to achieve: enter image description here

  • I'm not sure what you want to do exactly. Also, why are you drawing filled circles on top of the nodes? – Alenanno Apr 3 '16 at 13:30
  • The circles are geometric objects and the points on the circles correspond to vertices in the intersection graph of the disk arrangement. If two circles touch each other their corresponding vertices (filled circles) should be connected with an edge (line) – oerpli Apr 3 '16 at 16:55
  • Using your rule, only one line gets drawn. Is that your expected result? – Alenanno Apr 3 '16 at 18:55
  • Maybe "touch" was a poor word choice. If the circles with radius 1 touch or intersect the circle with radius 0.15 get connected. – oerpli Apr 4 '16 at 8:30
2

You can use \ifnum and \ifdim like this :

\documentclass[tikz,border=7mm]{standalone}
\usetikzlibrary{calc}

\begin{document}
  \newcommand{\centers}{(0,1),(0,0),(5,3),(2,1),(0.5,1),(4,3),(0,5),(1.6,3.5),(6,0.5)}
  \begin{tikzpicture}
    % link centers if circles intersect
    \foreach[count=\i] \a in \centers {
      \foreach[count=\j] \b in \centers {
        \ifnum \j < \i
          \draw[red] let \p1=\a, \p2=\b, \n1={veclen(\x1-\x2,\y1-\y2)} in
            {\ifdim \n1 < 2 cm \a -- \b \fi};
        \fi
      }
    }
    % draw circles
    \foreach [count=\i] \coord in \centers{\draw \coord circle(1);}
    \foreach \coord in \centers{\fill \coord circle(2pt);}
  \end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
0

I solved the problem with some extra macros for veclen.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\def\veclen#1=#2->#3;{%
  \edef\x{\noexpand\@veclen\noexpand#1#2#3}%
  \x
}
\def\@veclen#1(#2,#3)(#4,#5){%
  \pgfmathsetmacro#1{veclen(#2-#4,#3-#5)}%
}
\makeatother

\begin{document}
\begin{tikzpicture}[scale=0.5]
  \def\thresh{2.0}
  \def\centers{(0,1),(0,0),(5,3),(2,1),(0.5,1),(4,3),(0,5),(1.6,3.5),(6,0.5)}

  \foreach \coord [count=\i] in \centers {
    \node[draw,fill,circle,inner sep=1pt] (n-\i) at \coord {};
    \draw (n-\i) circle (1);
  }

  \foreach \a [count=\i] in \centers{
    \foreach \b [count=\j] in \centers{
      \ifnum\i<\j
        \veclen\dist=\a->\b;
        \ifdim\dist pt<\thresh pt
          \draw[red] (n-\i) -- (n-\j);
        \fi
      \fi
    }
  }
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.