# If I have two nodes defined at the end points of a diagonal of a rectangle, how can I define nodes for the other two corners?

I have two nodes which I want to define to be opposite corners of a rectangle. I would like to be able to define nodes at the other pair of opposite corners.

I tried the following, but it gives me errors:

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

%% ATTEMPT 1
%% ERROR: doesn't match its definition
\begin{tikzpicture}
\node (A) at (0,0) {1};
\node (C) at (5,3) {3};
\node (B) at ($(A) ! (C) ! ($ (A) + (1,0) $)$) {B};
\end{tikzpicture}

%% ATTEMPT 2
%% ERROR: doesn't match its definition
\begin{tikzpicture}
\node (A) at (0,0) {1};
\node (C) at (5,3) {3};
\node (tB) at ($(A) + (1,0)$ ) {tB};
\node (B) at ($(A) ! (C) ! (tB)$) {B};
\end{tikzpicture}

\end{document}


Why isn't my projection working?

-

Perhaps the simplest way is to use the perpendicular coordinate system:

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

\begin{tikzpicture}
\node (A) at (0,0) {1};
\node (C) at (5,3) {3};
\node (B) at (A.center|-C.center) {2};
\node (D) at (A.center-|C.center) {4};
\draw[red] (A.center) -- (B.center) -- (C.center) -- (D.center) -- (A.center);
\end{tikzpicture}

\end{document}


(<name1>|-<name2>) has the x-coordinate of <name1> and the y-coordinate of <name2>.

(<name1>-|<name2>) has the x-coordinate of <name2> and the y-coordinate of <name1>.

-
This is extremely useful. Where in the manual can I find this documentation? In other words, what topic is this discussed under? –  A.Ellett Aug 12 '13 at 23:49
@A.Ellett Section "Coordinate system perpendicular", pages 130-131 of the PGF manual. –  Gonzalo Medina Aug 12 '13 at 23:51

Apparently the calc library doesn't like some of the spaces in the coordinate calculation, when I remove them both your examples work, e.g.

\node (B) at ($(A)!(C)!($(A)+(1,0)$)$) {2};
\node (D) at ($(A)!(C)!($(A)+(0,1)$)$) {4};


More specifically, as noted by Gonzalo, it is the spaces in ! (<node name>) ! that causes the error, so

\node (B) at ( $(A) !(C)! ($ (A)+(1,0) $)$ ) {B};
\node (D) at ( $(A) !(C)! ($ (A)+(0,1) $)$ ) {D};


will also work.

### Complete code

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node (A) at (0,0) {1};
\node (C) at (5,3) {3};
\node (B) at ($(A)!(C)!($(A)+(1,0)$)$) {2};
\node (D) at ($(A)!(C)!($(A)+(0,1)$)$) {4};
\draw [red] (A) -- (B) -- (C) -- (D) -- (A);
\end{tikzpicture}
\end{document}


-
Thanks for explaining my syntax error. –  A.Ellett Aug 13 '13 at 0:15