## LaTeX forum ⇒ Graphics, Figures & Tables ⇒ tikz find the closest point to a defined point

Information and discussion about graphics, figures & tables in LaTeX documents.
melondolphin
Posts: 1
Joined: Fri Jun 14, 2019 5:20 pm

### tikz find the closest point to a defined point

Dear all,

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 ...

I also asked this question here in German: https://golatex.de/viewtopic,p,106794.html#106794
and here in English: https://tex.stackexchange.com/questions/495811/tikz-find-the-point-which-is-closest-to-another-point

Stefan Kottwitz
Posts: 9402
Joined: Mon Mar 10, 2008 9:44 pm
Hello,

welcome to the forum!

Than you for providing the links to the other sites. I saw there is already a solution. Also I like TikZ and the great results we can achieve with TikZ: I would be happy to see an example of the drawings you create.

Best regards,

Stefan