# Right Angle Symbol Alignment with TikZ/Calc

I used a solution from @Jake from this post Insertion of Perpendicular Symbol at Intersection of Two Lines, but the alignment is not quite right. I have my suspicions why, but I don't understand the pgf code enough to figure it out.

Here is my MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,snakes,backgrounds,arrows}
\usetikzlibrary{calc}

\tikzset{
right angle quadrant=1, % Make sure it is set, even if not called explicitly
right angle length/.code={\def\rightanglelength{#1}},   % Length of symbol
right angle length=2ex, % Make sure it is set...
right angle symbol/.style n args={3}{
insert path={
let \p0 = ($(#1)!(#3)!(#2)$) in     % Intersection
let \p1 = ($(\p0)!\quadranta*\rightanglelength!(#3)$), % Point on base line
\p2 = ($(\p0)!\quadrantb*\rightanglelength!(#2)$) in % Point on perpendicular line
let \p3 = ($(\p1)+(\p2)-(\p0)$) in  % Corner point of symbol
(\p1) -- (\p3) -- (\p2)
}
}
}

\begin{document}

\begin{tikzpicture}[line width=1pt,>=triangle 45,x=1.0cm,y=1.0cm,point/.style={name={#1}},sharp corners]
\clip (-0.1,-0.1)   rectangle   (6.1,6.1);
\draw   [step=0.5cm,dotted,line width=0.35pt]
(0,0)       grid        (6,6);
%   Parallel Line Graph
%   Axis
\draw   [line width=0.75pt]         (2,0)                   --(2,6)
(0,2)                   --(6,2);
%   Lines & Right Angle
\node   [point=A]                   at                  (5.5,5) {};
\node   [point=B]                   at                  (1,0.5) {};
\node   [point=C]                   at                  (5.5,0.5)   {};
\node   [point=D]                   at                  (0.5,5) {};
\draw   [<->]       (A) -- (B);
\draw   [<->]       (C) -- (D);
\draw [right angle quadrant=4,right angle symbol={A}{B}{D}] ($(A)!(D)!(B)$);
\end{tikzpicture}%
\end{document}


I imagine the solution is within the macro, but can someone help me understand where?

-
The line from (D) to ($(A)!(D)!(B)$) point is rectangular to the line from (A) to (B). Or in other words: The line from (A) to (B) and the line from (C) to (D) do not intersect at ($(A)!(D)!(B)$). Or in other words: The lines do not intersect in a right angle. — If you use for (C) (5,.5), it works. — Also: Do not use nodes if you declare coordinates. – Qrrbrbirlbel May 16 '13 at 21:04
Good catch! I guess I should have checked the actual angle of the lines when using calc. Regarding the ALSO, should I use path? Or some other command? – Calhistorian May 16 '13 at 22:21
\coordinate (A) at (5.5,5); would suffice. Almost everything in TikZ is a \path. (\coordinate simply expands to \path coordinate as \node does to \path node). You can do specify all coordinates in one path: \path coordinate (A) at (…) coordinate (B) at (…) …;. – Qrrbrbirlbel May 16 '13 at 22:40
@Qrrbrbirlbel your comments sounds like an answer? Can you write one? – Kurt May 17 '13 at 14:50
@Kurt I initially voted to close down, though apparently TeX.SE doesn’t agree, my answer is up. – Qrrbrbirlbel May 17 '13 at 17:23

The line from (D) to ($(A)!(D)!(B)$) point is rectangular to the line from (A) to (B).

Or in other words: The line from (A) to (B) and the line from (C) to (D) do not intersect at ($(A)!(D)!(B)$).

Or in other words: The lines do not intersect in a right angle.

If you use for (C) (5,.5), it works.

Also do not use nodes if you declare coordinates, use coordinates instead. The nodes are highlighted (draw=red) in this image:

## Code

\documentclass[tikz,convert]{standalone}
\usetikzlibrary{arrows,calc}

\tikzset{
right angle quadrant=1, % Make sure it is set, even if not called explicitly
right angle length/.code={\def\rightanglelength{#1}},   % Length of symbol
right angle length=2ex, % Make sure it is set...
right angle symbol/.style n args={3}{
insert path={
let \p0 = ($(#1)!(#3)!(#2)$),     % Intersection
\p1 = ($(\p0)!\quadranta*\rightanglelength!(#3)$), % Point on base line
\p2 = ($(\p0)!\quadrantb*\rightanglelength!(#2)$), % Point on perpendicular line
\p3 = ($(\p1)+(\p2)-(\p0)$) in  % Corner point of symbol
(\p1) -- (\p3) -- (\p2)
}
}
}

\begin{document}
\begin{tikzpicture}[line width=1pt,>=triangle 45,x=1.0cm,y=1.0cm,point/.style={name={#1}},sharp corners]
\clip (-0.1,-0.1)   rectangle   (6.1,6.1);
\draw [step=0.5cm,dotted,line width=0.35pt] (0,0) grid (6,6);

\draw  [line width=0.75pt] (2,0) -- (2,6)
(0,2) -- (6,2);
\coordinate (A) at (5.5,5);
\coordinate (B) at (1,0.5);
\coordinate (C) at (5,0.5);
\coordinate (D) at (0.5,5);
%\node [draw=red, point=A] at (5.5,5)   {};
%\node [draw=red, point=B] at (1,0.5)   {};
%\node [draw=red, point=C] at (5,0.5) {};
%\node [draw=red, point=D] at (0.5,5)   {};

\draw [<->] (A) -- (B);
\draw [<->] (C) -- (D);

\draw [right angle quadrant=4,right angle symbol={A}{B}{D}] ($(A)!(D)!(B)$);
\end{tikzpicture}
\end{document}


-