[Tex/LaTex] Right Angle Symbol Alignment with TikZ/Calc

calculationsintersectionstikz-pgf

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/.code={
    \pgfmathsetmacro\quadranta{{1,1,-1,-1}[#1-1]}     % Arrays for selecting quadrant
    \pgfmathsetmacro\quadrantb{{1,-1,-1,1}[#1-1]}},
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?

Best Answer

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:

enter image description here

Code

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

\tikzset{
    right angle quadrant/.code={
        \pgfmathsetmacro\quadranta{{1,1,-1,-1}[#1-1]}     % Arrays for selecting quadrant
        \pgfmathsetmacro\quadrantb{{1,-1,-1,1}[#1-1]}},
    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}

Output

enter image description here