Error in projection modifier with tikz calc?

The image below was obtained with

\begin{tikzpicture}
\draw (0,0) coordinate (a1) -- (4,3) coordinate (b1) ;
\draw (1,3) coordinate (a2) -- (6,1) coordinate (b2) ;
\coordinate (c) at (intersection of a1--b1 and a2--b2) ;
\coordinate (T) at ($(c)!0.7!20:(a1)$);
\coordinate (p1) at ($(a1)!(T)!(b1)$) ;
\draw[->,thick] (p1) -- (T) ;
\coordinate (T) at ($(c)!-0.7!20:(a2)$);
% problem below: (p2) seems offset with respect to the correct
% orthogonal projection on (a2)--(b2)
\coordinate (p2) at ($(a2)!(T)!(b2)$) ;
\draw[->,thick] (p2) -- (T) ;
\fill (c)  circle (1pt) node[below]{$C$} ;
\fill (p1) circle (1pt) node[above]{$P_1$} ;
\fill (p2) circle (1pt) node[below]{$P_2$} ;
\end{tikzpicture}


What I need are vectors at P orthogonal to PC. While P1 is acceptable, the vector at P2 is disturbingly leaning leftwards.

How can I increase the accuracy of this image?

Edit

Reading the comments I fear that the above picture is like the infamous blue/black or white/gold dress: is vector at P2 orthogonal to its base line? For me definitely not. My question is what should I do to avoid this problem. Should I abandon calc and revert to trusted hand calculations?

Edit

The problem becomes more clear if we add a "true" perpendicular for comparison (see the red line below):

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

\begin{document}

\begin{tikzpicture}
\draw (0,0) coordinate (a1) -- (4,3) coordinate (b1) ;
\draw (1,3) coordinate (a2) -- (6,1) coordinate (b2) ;
\coordinate (c) at (intersection of a1--b1 and a2--b2) ;
\coordinate (T) at ($(c)!0.7!20:(a1)$);
\coordinate (p1) at ($(a1)!(T)!(b1)$) ;
\draw[->,thick] (p1) -- (T) ;
\coordinate (T) at ($(c)!-0.7!20:(a2)$);
% problem below: (p2) seems offset with respect to the correct
% orthogonal projection on (a2)--(b2)
\coordinate (p2) at ($(a2)!(T)!(b2)$) ;
\draw[->,thick] (p2) -- (T) ;
\fill (c)  circle (1pt) node[below]{$C$} ;
\fill (p1) circle (1pt) node[above]{$P_1$} ;
\fill (p2) circle (1pt) node[below]{$P_2$} ;
\draw[red] (p2) -- ($(p2)!-1cm!90:(c)$);
\end{tikzpicture}

\end{document}


• Vector P2 is perfectly orthogonal to its base (line through P2 and C). It seems, that you like that vector p1 be parallel to line P2 - C. Is this suspect correct? – Zarko Oct 6 '15 at 17:33
• ,@StefanoM -- What do you mean by leaning leftwards? How should P2 be? – AboAmmar Oct 6 '15 at 17:33
• It looks like a precision problem with the projection computation if it involves angles. – percusse Oct 6 '15 at 18:18
• Add this to your code \draw[red] (p2) -- ($(p2)!-1cm!90:(c)$); which produces a "true" perpendicular. Compile and add a to your question a zoomed image of the result and everyone will see the problem. – Gonzalo Medina Oct 6 '15 at 20:05
• In fact, I added it myself as an edit to your question and uploaded an image of the result. Now the problem should be clearer for everyone. I hope it's OK. – Gonzalo Medina Oct 6 '15 at 20:12

The error is caused by \pgfpointnormalised. A solution to obtain a better precision is given in this answer (based on this one).

Applied on the MWE:

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

% use the Mark Wibrow's correction
\makeatletter
\def\pgfpointnormalised#1{%
\pgf@process{#1}%
\pgfmathatantwo{\the\pgf@y}{\the\pgf@x}%
\let\pgf@tmp=\pgfmathresult%
\pgfmathcos@{\pgf@tmp}\pgf@x=\pgfmathresult pt\relax%
\pgfmathsin@{\pgf@tmp}\pgf@y=\pgfmathresult pt\relax%
}
\makeatother

\begin{document}

\begin{tikzpicture}
\draw (0,0) coordinate (a1) -- (4,3) coordinate (b1) ;
\draw (1,3) coordinate (a2) -- (6,1) coordinate (b2) ;
\coordinate (c) at (intersection of a1--b1 and a2--b2) ;
\coordinate (T) at ($(c)!0.7!20:(a1)$);
\coordinate (p1) at ($(a1)!(T)!(b1)$) ;
\draw[->,thick] (p1) -- (T) ;
\coordinate (T) at ($(c)!-0.7!20:(a2)$);
% problem below: (p2) seems offset with respect to the correct
% orthogonal projection on (a2)--(b2)
\coordinate (p2) at ($(a2)!(T)!(b2)$) ;
\draw[->,thick] (p2) -- (T) ;
\fill (c)  circle (1pt) node[below]{$C$} ;
\fill (p1) circle (1pt) node[above]{$P_1$} ;
\fill (p2) circle (1pt) node[below]{$P_2$} ;
\draw[red] (p2) -- ($(p2)!-1cm!90:(c)$);
\end{tikzpicture}

\end{document}


The problem boils down to the precision of calc and the computation of ($(a2)!(T)!(b2)$). Following the suggestion by Gonzalo, I added two reference lines:

% Added for comparison
\draw[red, very thin] (p2) -- ($(p2)!-1cm!90:(c)$);
\draw[green, very thin] (T) -- ++(-0.4,-1.0) ;


obtaining (zoom on (p2))

Point (p2) is shifted with respect to the orthogonal projection of (T) by a quantity of about 0.8pt. This may seem negligible but is far above the accuracy that you obtain on a hand made technical drawing.

Eventually I solved by this walk around, avoiding the projection modifier:

\begin{tikzpicture}
\draw (0,0) coordinate (a1) -- (4,3) coordinate (b1) ;
\draw (1,3) coordinate (a2) -- (6,1) coordinate (b2) ;
\coordinate (c) at (intersection of a1--b1 and a2--b2) ;
\coordinate (p1) at ($(c)!0.7!(a1)$);
\coordinate (T) at ($(p1)!0.34!-90:(c)$) ;
\draw[->,thick] (p1) -- (T) ;
\coordinate (p2) at ($(c)!-0.7!(a2)$);
\coordinate (T) at ($(p2)!0.34!-90:(c)$) ;
\draw[->,thick] (p2) -- (T) ;
\fill (c)  circle (1pt) node[below]{$C$} ;
\fill (p1) circle (1pt) node[above]{$P_1$} ;
\fill (p2) circle (1pt) node[below]{$P_2$} ;
%reference
\draw[green, very thin] (T) -- ++(-0.4,-1.0) ;
\end{tikzpicture}


It seems that problem is vector "P1", so from givem MWE I only consider the part concerning this vector:

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\draw (1,3) coordinate (a2) -- (6,1) coordinate (b2) ;
\coordinate (T) at ($(a2)!0.5!(b2)$);
\fill (T)  circle (1pt) node[below] {$P_1$} ;
\coordinate (p1) at ($(T)!22mm!90:(b2)$) ;
\draw[->,red, very thin] (T) -- (p1) ;
\draw[blue,dashed, very thin] (T) -- + (68.198590513648188229755133913056:24mm) ;
\end{tikzpicture}
\end{document}


And gives the following result:

Red vector belong to calculation of direction by \coordinate (p1) at ($(T)!22mm!90:(b2)$) and dashed blue line to manual calculation of direction. As far I can see on my monitor, both lines are in the same direction.

Let me pointed, that used calculation of vector direction is slightly different as in OP's MWE. If this make difference, I didn't test. Regarding to above results, I conclude that manual calculation will not give better drawing result as is obtained by use of tikz library calc.

• The question is how can I improve the accuracy of the projection modifier ($(a2)!(T)!(b2)$) when T does not lie on (a2)--(b2). Your (T) lies on (a2)--(b2) and unquestionably ($(T)!22mm!90:(b2)$) is at a right angle to (a2)--(b2). – Stefano M Oct 6 '15 at 20:59
• @StefanoM, you have right. My later "investigation" shows, that error, which you find, depends on distance between point and projection base. Bigger is distance, smaller is error. It seems that algorithm for calculation of projection is sensitive on calculus accuracy or the algorithm contain a bug. – Zarko Oct 7 '15 at 12:13