5

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}

sample image

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$} ; % Added for comparison \draw[red] (p2) -- ($(p2)!-1cm!90:(c)$); \end{tikzpicture}

\end{document}

enter image description here

Stefano M
  • 164
  • 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 06 '15 at 17:33
  • ,@StefanoM -- What do you mean by leaning leftwards? How should P2 be? – AboAmmar Oct 06 '15 at 17:33
  • It looks like a precision problem with the projection computation if it involves angles. – percusse Oct 06 '15 at 18:18
  • @Zarko no, eache vector at P_i should be orthogonal to its base line. My impression (confirmed on a printout and checked with a square ruler) is that vector at P_2 is not orthogonal to its base line. – Stefano M Oct 06 '15 at 18:54
  • @AboAmmar the vector at P2 should be orthogonal to its baseline, but it lies to the left of the exact normal. Checked on paper with a square ruler. Maybe I'm over sensitive, but the result is really disturbing for me. – Stefano M Oct 06 '15 at 18:57
  • @StefanoM, and how much discrepancy you are find? In degrees? Or in angular minutes? The most of numerical calculations have numerical errors ... the question is, if they can be negligible. – Zarko Oct 06 '15 at 19:10
  • @zarko it really hurts my eyes: obviously you will never have an exact square angle, but usually (with almost every CAD program I've used or even in free hand drawings) I do not notice discrepancies. In this case the image quality is unacceptable for me. Let me reformulate the question: Is my tikz code wrong? Shuoud I avoid the calc module and do the computations by hand? – Stefano M Oct 06 '15 at 19:17
  • Just wondering here, the helper point T, what is up with the 20 degree angle? Is p2 suppose to be orthogonal to C -- b2, I do not understand what you are doing in this image. – daleif Oct 06 '15 at 19:52
  • 2
    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 06 '15 at 20:05
  • 1
    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 06 '15 at 20:12
  • @daleif the length of the vectors should be proportional to the distance PC. Helper point T is on a line at an (arbitrary but constant) angle from the base line, so that this condition is enforced by the intercept theorem. Gonzalo edit shows clearly that the correct way is the other way round: instead of projecting T on the base line, I should find T as the intersection between the perpendicular at P and the 20° helper line. A small error on the vector length will not be noticeable, while a small error on the angle is evident. – Stefano M Oct 06 '15 at 20:47
  • @GonzaloMedina: thanks, your edit clarifies the problem and shows a possible solution. – Stefano M Oct 06 '15 at 20:48

3 Answers3

2

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))closeup 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}

enter image description here

Stefano M
  • 164
2

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$} ;
% Added for comparison
\draw[red] (p2) -- ($(p2)!-1cm!90:(c)$);
\end{tikzpicture}

\end{document}

enter image description here

Sam
  • 188
0

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:

enter image description here

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.

Zarko
  • 296,517
  • 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 06 '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 07 '15 at 12:13