# Given four nodes forming a quadrilateral, how do I find the node at the intersection of the diagonals [duplicate]

I'm trying to find and name a node at the intersection of the diagonals of a quadrilateral formed by four other nodes. However, I'm not drawing a path. All I can seem to find in the tikz manual are examples involving drawing paths. Is there a way to do this without drawing anything?

For example,

\documentclass{article}
\usepackage{tikz}
\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\node (A) at (0,0) {1};
\node (B) at (1,0) {2};
\node (C) at (5,3) {3};
\node (D) at (-2,7) {4};
\end{tikzpicture}

\end{document}


How do I find and label the node at the intersection of the diagonals AC and BD?

Actually, most of the quadrilaterals I'm working with are rectangles with sides parallel to the axes. So, I'd be interested in solution that might only work for rectangles too.

-

## marked as duplicate by Qrrbrbirlbel, Heiko Oberdiek, Werner, lockstep, Andrew SwannAug 13 '13 at 6:50

You can use the intersections library:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\node (A) at (0,0) {1};
\node (B) at (1,0) {2};
\node (C) at (5,3) {3};
\node (D) at (-2,7) {4};
\draw[cyan,name path=d1] (A.center) -- (C.center);
\draw[cyan,name path=d2] (B.center) -- (D.center);
\path[name intersections={of=d1 and d2, by={l}}]
node[fill,circle,inner sep=1.5pt] (I) at (l) {};
\end{tikzpicture}

\end{document}


In the simpler case of rectangles you can use the calc library to calculate the middle point of one of the diagonals:

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

\begin{document}

\begin{tikzpicture}
\node (A) at (0,0) {1};
\node (B) at (3,0) {2};
\node (C) at (0,7) {3};
\node (D) at (3,7) {4};
\draw[cyan] (A.center) -- (D.center);
\draw[cyan] (B.center) -- (C.center);
\node[fill,circle,inner sep=1.5pt] at ( $(A)!0.5!(D)$ ) (I) {};
\end{tikzpicture}

\end{document}


If this is going to be used multiple times, one could think of a command; something along these lines:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\pagestyle{empty}

\newcounter{mycnt}

\newcommand\Inter[5]{%
\stepcounter{mycnt}
\draw[cyan,name path={d\themycnt}] (#1.center) -- (#2.center);
\draw[cyan,name path={e\themycnt}] (#3.center) -- (#4.center);
\path[name intersections={of={d\themycnt} and {e\themycnt}, by={f\themycnt}}]
node[fill,circle,inner sep=1.5pt] (#5) at ({f\themycnt}) {};
}

\begin{document}

\begin{tikzpicture}
\node (A) at (0,0) {1};
\node (B) at (1,0) {2};
\node (C) at (5,3) {3};
\node (D) at (-2,7) {4};
\draw[orange] (A.center) -- (B.center) -- (C.center) -- (D.center) -- cycle;
\Inter{A}{C}{B}{D}{i1}
\node (E) at (7,-2) {5};
\node (F) at (8,5) {6};
\node (G) at (10,3) {7};
\node (H) at (11,-1) {8};
\draw[orange] (E.center) -- (H.center) -- (G.center) -- (F.center) -- cycle;
\Inter{E}{G}{F}{H}{i2}
\end{tikzpicture}

\end{document}


The synatx is \Inter{<name1>}{<name2>}{<name3>}{<name4>}{<name-int>}, where <name1>, <name2> correspond to the end points of one diagonal; <name3>, <name4> correspond to the end points of the other diagonal, and <name-int> is the name asignated to the node in the intersection point.

Perhaps it would be better to construct the vertices using \coordinate instead of \node.

-
Instead of using a count(er) it may be better to use a group and some auxiliary (throwaway) names like aux@l1, aux@l2 and aux@i. – Qrrbrbirlbel Aug 13 '13 at 5:56