# tikz, find the point which is closest to another point

At the moment, I try to learn automation of graphic drawing with latex (tikz) and R.

I need to determine the coordinates of the point with the smallest distance to a defined point "North"

Thats what I found out so far:

\begin{tikzpicture}

\coordinate (North) at (0,5);

\path let
\p1 = ($(North)-(node_1)$), \n1 = {veclen(\x1,\y1)}
, \p2 = ($(North)-(node_2)$), \n2 = {veclen(\x2,\y2)}
, \p3 = ($(North)-(node_3)$), \n3 = {veclen(\x3,\y3)}
, \p4 = ($(North)-(node_4)$), \n4 = {veclen(\x4,\y4)}
in coordinate (dummy1) at (\x1, \y1)
coordinate (dummy2) at (\x2, \y2)
coordinate (dummy3) at (\x3, \y3)
coordinate (dummy4) at (\x4, \y4);

\end{tikzpicture}


The points node_1 to node_4 are determined in advance. My basic idea was to implement something like an if loop:

if \n1 == min(n1, \n2, \n3,\n4) then coordinate (nearest_to_north) at (node_1) elseif \n2 == min(n1, \n2, \n3,\n4) then coordinate (nearest_to_north) at (node_2) elseif...

The whole Latex-codes is embedded in an *.Rnw-File so it would also be possibel to implement the loop in R-code but I also dont understand how to transfair the latex commands \n1, \x1, \y1 to R.

The tikz syntax is very confusing to me...so Im looking forward to any suggestions or help :roll: :roll: ...

I also asked this question here in German: https://golatex.de/viewtopic,p,106794.html#106794 and here in English: https://latex.org/forum/viewtopic.php?f=45&t=32581&p=109553#p109553

This does what I think you are asking, to illustrate it I create 5 random points p1,...,p5 and another random point Q, and ask TikZ to tell me which of the 5 random points is closest to Q. To show that it works I attach an animation.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\foreach \Ani in {1,...,20}
{\begin{tikzpicture}
\path[use as bounding box] (-5.1,-5.1) rectangle (5.1,5.1); % for animation
\foreach \X in {1,...,5}
{\path (8*rnd-4,8*rnd-4) coordinate (p\X)
node[fill,circle,inner sep=1pt,label=below:\X]{};}
\path (8*rnd-4,8*rnd-4) coordinate (Q)
node[fill,blue,circle,inner sep=1pt,label=below:$Q$]{};
\path  foreach \X in {1,...,5} {let \p\X=($(p\X)-(Q)$),\n\X={veclen(\x\X,\y\X)}
in \pgfextra{\ifnum\X=1
\xdef\MinPt{1}
\xdef\MinLen{\n1}
\else
\pgfmathsetmacro{\NewMinLn}{min(\n\X,\MinLen)}
\ifdim\NewMinLn pt<\MinLen
\xdef\MinPt{\X}
\fi
\xdef\MinLen{\NewMinLn pt}
\fi}} [draw,red] (p\MinPt) -- (Q);
\end{tikzpicture}}
\end{document}


Of course, I have no idea which syntax you need for your use case. One can make this definitely a style but I do not know what the requirements are. Here is a possible way.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[name closest point of/.style args={#1 to #2 by #3}{insert path={
foreach \Coord [count=\XX] in {#1}
{let \p\XX=($(\Coord)-(#2)$),\n\XX={veclen(\x\XX,\y\XX)}
in \pgfextra{\ifnum\XX=1
\xdef\MinPt{1}
\xdef\MinLen{\n1}
\else
\pgfmathsetmacro{\NewMinLn}{min(\n\XX,\MinLen)}
\ifdim\NewMinLn pt<\MinLen
\xdef\MinPt{\XX}
\fi
\xdef\MinLen{\NewMinLn pt}
\fi}} \pgfextra{\foreach \Coord [count=\XX] in {#1}
{\ifnum\XX=\MinPt
\xdef\ClosestPoint{\Coord}
\fi}} (\ClosestPoint) coordinate (#3)
}}]
\path (1,2)coordinate (pft) (2,3) coordinate (blub) (3,-4) coordinate (duck)
(-4,-5) coordinate(koala) (-5,2) coordinate (squirrel);
\path foreach \X in {blub,pft,duck,koala,squirrel}
{(\X) node[circle,fill,inner sep=1pt,label=below:\X]{}};
\path[name closest point of={blub,pft,duck,koala} to squirrel by mouse]
[draw=red] (squirrel) -- (mouse);
\end{tikzpicture}
\end{document}


• @JohnKormylo Thanks! This is certainly true, yet compilation time is not an issue at all here, the compilation is very quick. But you are right, and one would then spare a tiny extra bit of time from not loading calc. – marmot Jun 15 at 14:15
• @JohnKormylo Not in this case. I have one fixed point and want to find the closest one in a set of other points, so here it is linear. It would we quadratic if one would be interested in the minimal distance between all points. – marmot Jun 15 at 14:20
• Thanks for you solution. It does what I was looking for. Anyway, I decided to implement this solution because it better fits into my existing code: golatex.de/… – RD_gis Jun 17 at 9:02