更新

更新

我有点困惑,为什么我的坐标计算结果会如此不同。所有坐标都在我预期的位置,除了协调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}

在此处输入图片描述

为什么对于我认为是同一件事的坐标计算结果却如此不同?

小问题

为什么我必须指定“step=1”才能使帮助线正确显示?如果没有step=1,我只能得到一个 5x5 的网格。

答案1

放置 X 节点的行和放置 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
    \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}

结果

更新

根据评论和对预期输出的更好理解,我建议完全消除+路径中的相对坐标(以 开头的坐标),并将其替换为显式计算(可能涉及极坐标)。因此,例如,要获取 右侧 20 个单位处的点(B),请使用($(B)+(0:20)$),而不是将原点移动到(B)然后写入+(0:20)

混合绝对坐标和相对坐标使得结果难以猜测和控制。使用显式计算可以更好地理解和控制。使用这种方法,您的代码将是:

\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}

结果(我希望这次我猜对了):

在此处输入图片描述

相关内容