# tikz coordinate calculations gone wild

I'm quite a bit baffled why my coordinate calculations are coming out so differently. All coordinates are where I expect them to be, except coordinate Z.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
\setlength{\parindent}{0pt}
\begin{document}

%% Attempt 01
\begin{tikzpicture}[x=0.25cm,y=0.25cm]
%% why does this draw a 5 by 5 grid when I don't specify "step=1"???
\draw[help lines,blue!20,step=1] (0,0) grid (20,20);
%% quarter circle
\draw[blue]  (  0:20)  arc(0:90:20);

%% Coordinates "B", "Bn", "Bl", "Br" all come out where I expect
%% Coordinate  "Z" is not where I expected it.
\path  [draw,line width=2pt,red]
(0,0)                 coordinate [label=135:P] (Q)
-- +( 90:20)   coordinate (B)  -- +( $(B) !-1em! (Q)$ )  coordinate (Bn) node {$Bn$}
(B) -- +(0:20)     coordinate (Bl) -- +( $(B) !0.5! (Bl)$ )  coordinate [label=90:Z] (Z)
-- +(180:20)   coordinate (Br)
;

%% Coordinate "X" goes where I thought coordinate "Z" would have gone too
\draw ( $(B) !0.5!(Bl)$ ) coordinate [label=90:X] (X) circle (2pt);

\end{tikzpicture}

\vspace{1cm}

Same as example above except that $B$ is defined by \verb=(30:20)=
instead of \verb=(90:20)= relative to \verb=(0,0)=.  Also, I omitted
\verb!step=1! from the \verb=help lines=.

%% Attempt 02---same as attempt 01 except for (1) the placement definition
%% of coordinate "B" and (2) not "step=1" for the "help lines".
\begin{tikzpicture}[x=0.25cm,y=0.25cm]
%% why does this draw a 5 by 5 grid when I don't specify "step=1"???
\draw[help lines,blue!20] (0,0) grid (20,20);
%% quarter circle
\draw[blue]  (  0:20)  arc(0:90:20);

%% Coordinates "B", "Bn", "Bl", "Br" all come out where I expect
%% Coordinate  "Z" is not where I expected it.
\path  [draw,line width=2pt,red,opacity=0.50]
(0,0)                 coordinate [label=135:P] (Q)
-- +( 30:20)   coordinate (B)  -- +( $(B) !-1em! (Q)$ )  coordinate (Bn) node {$Bn$}
(B) -- +(0:20)     coordinate (Bl) -- +( $(B) !0.5! (Bl)$ )  coordinate [label=90:Z] (Z)
-- +(180:20)   coordinate (Br)
;

%% Coordinate "X" goes where I thought coordinate "Z" would have gone too
\draw ( $(B) !0.5!(Bl)$ ) coordinate [label=90:X] (X) circle (2pt);

\end{tikzpicture}

\end{document}


Why are the coordinate calculations---for what seems to me to be the same thing---turning out so differently?

Minor question

Why must I specify "step=1" to get my help lines to look correctly? Without step=1, I only get a five-by-five grid.

-
About the minor question: The default setting of step is 1cm, that's why you get a 5x5 grid if your unit vectors are 0.2cm long. –  Jake Feb 16 '13 at 17:26
@jake. Doesn't that seem buggy that step is not set relative to the units I've specified? –  A.Ellett Feb 16 '13 at 17:27
@jldiaz. I expect Z to be where X is. It's exactly the same coordinate calculation. –  A.Ellett Feb 16 '13 at 17:28
@A.Ellett: I wouldn't call it buggy, you're just expecting something it doesn't do. It behaves exactly as it's specified in the manual. –  Jake Feb 16 '13 at 17:29
You are at the second row moving the path to (B) and then everything with +(...) becomes relative to (B). –  percusse Feb 16 '13 at 17:33

The difference between the line which places the X node and the one which places the Z node is that in the first one you are using absolute coordinates, while in the second one you are using relative coordinates because you preceded the coordinates with a + sign.

The + is not required since you are not giving the new coordinate as a displacement with respect other, but instead you are computing the new coordinates using a calc expression. You can remove the + in all your paths (at least in this case):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
\setlength{\parindent}{0pt}
\begin{document}

%% Attempt 01
\begin{tikzpicture}[x=0.25cm,y=0.25cm]
%% why does this draw a 5 by 5 grid when I don't specify "step=1"???
\draw[help lines,blue!20,step=1] (0,0) grid (20,20);
%% quarter circle
\path  (  0:20)   coordinate (A);
\draw[blue]  (A) arc(0:90:20);

%% Coordinates "B", "Bn", "Bl", "Br" all come out where I expect
%% Coordinate  "Z" is not where I expected it.
\path  [draw,line width=2pt,red]
(0,0)                 coordinate [label=135:P] (Q)
-- +( 90:20)   coordinate (B)  -- ( $(B) !-1em! (Q)$ )  coordinate (Bn) node {$B$}
(B) -- +(0:20)     coordinate (Bl) -- ( $(B) !0.5! (Bl)$ )  coordinate [label=90:Z] (Z)
-- +(180:20)   coordinate (Br)
;

%% Coordinate "X" goes where I thought coordinate "Z" would have gone too
\draw ( $(B) !0.5!(Bl)$ ) coordinate [label=90:X] (X) circle (2pt);

\end{tikzpicture}

\vspace{1cm}

Same as example above except that $B$ is defined by \verb=(30:20)=
instead of \verb=(90:20)= relative to \verb=(0,0)=.  Also, I omitted
\verb!step=1! from the \verb=help lines=.

%% Attempt 02---same as attempt 01 except for (1) the placement definition
%% of coordinate "B" and (2) not "step=1" for the "help lines".
\begin{tikzpicture}[x=0.25cm,y=0.25cm]
%% why does this draw a 5 by 5 grid when I don't specify "step=1"???
\draw[help lines,blue!20] (0,0) grid (20,20);
%% quarter circle
\path  (  0:20)   coordinate (A);
\draw[blue]  (A) arc(0:90:20);

%% Coordinates "B", "Bn", "Bl", "Br" all come out where I expect
%% Coordinate  "Z" is not where I expected it.
\path  [draw,line width=2pt,red,opacity=0.50]
(0,0)                 coordinate [label=135:P] (Q)
-- +( 30:20)   coordinate (B)  -- ( $(B) !-1em! (Q)$ )  coordinate (Bn) node {$B$}
(B)   -- +(0:20)     coordinate (Bl) -- ( $(B) !0.5! (Bl)$ )  coordinate [label=90:Z] (Z)
-- +(180:20)   coordinate (Br)
;

%% Coordinate "X" goes where I thought coordinate "Z" would have gone too
\draw ( $(B) !0.5!(Bl)$ ) coordinate [label=90:X] (X) circle (2pt);

\end{tikzpicture}
\end{document}


# Update

Based on the comments and on a better understanding of the intended output, I suggest to completely eliminate relative coordinates (those beginning with +) in your path, and replacing those for explicit calculations (which can involve polar coordinates). So, for example, to get a point 20 units to the right of (B), use ($(B)+(0:20)$), instead of moving the origin to (B) and then write +(0:20).

Mixing absolute and relative coordinates make difficult to guess and control the result. Use explicit calculations to a better understanding and control. With this approach your code would be:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
\setlength{\parindent}{0pt}
\begin{document}

%% Attempt 01
\begin{tikzpicture}[x=0.25cm,y=0.25cm]
%% why does this draw a 5 by 5 grid when I don't specify "step=1"???
\draw[help lines,blue!20,step=1] (0,0) grid (20,20);
%% quarter circle
\path  (  0:20)   coordinate (A);
\draw[blue]  (A) arc(0:90:20);

%% Coordinates "B", "Bn", "Bl", "Br" all come out where I expect
%% Coordinate  "Z" is not where I expected it.
\path  [draw,line width=2pt,red]
(0,0)                        coordinate [label=135:P] (Q)
-- ($(Q)+(90:20)$)   coordinate (B)  -- ( $(B) !-1em! (Q)$ )  coordinate (Bn) node {$B$}
(B) -- ($(B)+(0:20)$)    coordinate (Bl) -- ( $(B) !0.5! (Bl)$ )  coordinate [label=90:Z] (Z)
-- ($(B)+(180:20)$)  coordinate (Br)
;

%% Coordinate "X" goes where I thought coordinate "Z" would have gone too
\draw ( $(B) !0.5!(Bl)$ ) coordinate [label=90:X] (X) circle (2pt);

\end{tikzpicture}
\vspace{1cm}

Same as example above except that $B$ is defined by \verb=(30:20)=
instead of \verb=(90:20)= relative to \verb=(0,0)=.  Also, I omitted
\verb!step=1! from the \verb=help lines=.

%% Attempt 02---same as attempt 01 except for (1) the placement definition
%% of coordinate "B" and (2) not "step=1" for the "help lines".
\begin{tikzpicture}[x=0.25cm,y=0.25cm]
%% why does this draw a 5 by 5 grid when I don't specify "step=1"???
\draw[help lines,blue!20] (0,0) grid (20,20);
%% quarter circle
\path  (  0:20)   coordinate (A);
\draw[blue]  (A) arc(0:90:20);

%% Coordinates "B", "Bn", "Bl", "Br" all come out where I expect
%% Coordinate  "Z" is not where I expected it.
\path  [draw,line width=2pt,red,opacity=0.50]
(0,0)                 coordinate [label=135:P] (Q)
-- ($(Q)+(30:20)$)    coordinate (B)  -- ( $(B) !-1em! (Q)$ )   coordinate (Bn) node {$B$}
(B)  -- ($(B)+(0:20)$)     coordinate (Bl) -- ( $(B) !0.5! (Bl)$ )  coordinate [label=90:Z] (Z)
-- ($(B)+(180:20)$)   coordinate (Br)
;

%% Coordinate "X" goes where I thought coordinate "Z" would have gone too
\draw ( $(B) !0.5!(Bl)$ ) coordinate [label=90:X] (X) circle (2pt);

\end{tikzpicture}
\end{document}


And the result (I hope I got it right this time):

-
Almost! :) But now Bl is not placed 20 "units" to the left of B. This is really just a boiled down version of something much bigger. Most of the relative coordinates are needed because I'm placing "most" coordinates relative to the center of a circle. Z is the one exception. But, I can draw that absolutely on its own, though I have to do that after I've completed drawing all the other relative coordinates. Thanks though. –  A.Ellett Feb 16 '13 at 17:57
Actually, it's Z, Bl, and Br that are the exceptions. But from this snippet I can see your point about the apparent lack of a need for all the relative coordinates. –  A.Ellett Feb 16 '13 at 18:00
I appreciate your feedback. But I disagree with making all the calculations absolute. The point of the relative coordinates is that I can change where I place Q and the whole picture will move with Q. Your method will work, but only through a fair amount of convoluted calculations when tikz already provides a means to relativizing coordinates with respect to a given one. My mistake was to forget that my anchoring coordinate was Q and not B. –  A.Ellett Feb 16 '13 at 18:58