TikZ坐标变换导致节点位置计算错误

TikZ坐标变换导致节点位置计算错误

为什么以下例子中的坐标变换会导致 的位置计算错误E

\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}

\begin{tikzpicture}[x=0.5in, y=0.5in]%% <-- calculation of "E" is incorrect
%%\begin{tikzpicture}%% <-- calculation of "E" is correct

  \coordinate (Q) at (0,0);
  \coordinate (A) at (-170:1);
  \coordinate (B) at (-70:1);
  \coordinate (C) at (-20:1);
  \coordinate (D) at (50:1);

  \tkzInterLL(A,C)(B,D) 
  \tkzGetPoint{E}

  \draw (A) --  (C);
  \draw (B) --  (D);

  \node[circle,fill,inner sep=1pt]  at (E) {};

\end{tikzpicture}

\end{document}

答案1

发生这种情况的原因是作者tkz-euclide假设每个单位向量都是1cm。您可以通过查看定义来了解发生了什么\tkz@InterLL(使用的私有宏,它返回存储在中的\tkzInterLL交点...呼!)。coordinate E\tkzGetPoint{E}

这里是:

\def\tkz@InterLL(#1,#2)(#3,#4)#5{%
%\path (intersection of #1--#2 and #3--#4) coordinate(#5);%
\pgfextractx{\pgf@x}{\pgfpointanchor{#1}{center}}
\pgfextracty{\pgf@y}{\pgfpointanchor{#1}{center}} 
\tkz@ax\pgf@x %
\tkz@ay\pgf@y %
\pgfextractx{\pgf@x}{\pgfpointanchor{#2}{center}}
\pgfextracty{\pgf@y}{\pgfpointanchor{#2}{center}} 
\tkz@bx\pgf@x %
\tkz@by\pgf@y %
\pgfextractx{\pgf@x}{\pgfpointanchor{#3}{center}}
\pgfextracty{\pgf@y}{\pgfpointanchor{#3}{center}} 
\tkz@cx\pgf@x %
\tkz@cy\pgf@y %
\pgfextractx{\pgf@x}{\pgfpointanchor{#4}{center}}
\pgfextracty{\pgf@y}{\pgfpointanchor{#4}{center}} 
\tkz@dx\pgf@x %
\tkz@dy\pgf@y %
\FPeval\tkz@deltax{\pgf@sys@tonumber{\tkz@ax}-\pgf@sys@tonumber{\tkz@bx}}
\FPdiv\tkz@deltax{\tkz@deltax}{28.45274}
\FPeval\tkz@deltaxx{\pgf@sys@tonumber{\tkz@cx}-\pgf@sys@tonumber{\tkz@dx}}
\FPdiv\tkz@deltaxx{\tkz@deltaxx}{28.45274}
\FPeval\tkz@deltay{\pgf@sys@tonumber{\tkz@ay}-\pgf@sys@tonumber{\tkz@by}}
\FPdiv\tkz@deltay{\tkz@deltay}{28.45274}
\FPeval\tkz@deltayy{\pgf@sys@tonumber{\tkz@cy}-\pgf@sys@tonumber{\tkz@dy}}
\FPdiv\tkz@deltayy{\tkz@deltayy}{28.45274}
\FPeval\tkz@deltaxy{(\pgf@sys@tonumber{\tkz@ax}*\pgf@sys@tonumber{\tkz@by})-(\pgf@sys@tonumber{\tkz@ay}*\pgf@sys@tonumber{\tkz@bx})}
\FPdiv\tkz@deltaxy{\tkz@deltaxy}{28.45274}
\FPdiv\tkz@deltaxy{\tkz@deltaxy}{28.45274}
\FPeval\tkz@deltaxxyy{(\pgf@sys@tonumber{\tkz@cx}*\pgf@sys@tonumber{\tkz@dy})-(\pgf@sys@tonumber{\tkz@cy}*\pgf@sys@tonumber{\tkz@dx})}
\FPdiv\tkz@deltaxxyy{\tkz@deltaxxyy}{28.45274}
\FPdiv\tkz@deltaxxyy{\tkz@deltaxxyy}{28.45274}
\FPeval\tkz@div{(\tkz@deltax*\tkz@deltayy)-(\tkz@deltay*\tkz@deltaxx)}
\FPeval\tkz@numx{(\tkz@deltaxy*\tkz@deltaxx)-(\tkz@deltax*\tkz@deltaxxyy)}
\FPeval\tkz@numy{(\tkz@deltaxy*\tkz@deltayy)-(\tkz@deltay*\tkz@deltaxxyy)}
\FPdiv\tkz@xs{\tkz@numx}{\tkz@div}
\FPdiv\tkz@ys{\tkz@numy}{\tkz@div}
\FPround\tkz@xs{\tkz@xs}{5}
\FPround\tkz@ys{\tkz@ys}{5}
\path[coordinate](\tkz@xs,\tkz@ys) coordinate (#5);
}

28.45274看到中间的那些s 了吗?这是将假定的单位向量长度转换1cm为单位,pt以便以后进行舍入和使用。

因此,作为测试,让我们尝试根据 定义比例,in使其等同于1cm

\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}

\begin{tikzpicture}[x=0.3937in, y=0.3937in] % works
%\begin{tikzpicture}[x=0.3937in, y=1cm]      % works
%\begin{tikzpicture}[x=1cm, y=0.3937in]      % works
%\begin{tikzpicture}[x=1cm, y=1cm]           % works
%\begin{tikzpicture}                         % works

  \coordinate (Q) at (0,0);
  \coordinate (A) at (-170:1);
  \coordinate (B) at (-70:1);
  \coordinate (C) at (-20:1);
  \coordinate (D) at (50:1);

  \tkzInterLL(A,C)(B,D) 
  \tkzGetPoint{E}

  \draw (A) --  (C);
  \draw (B) --  (D);

  \node[circle,fill,inner sep=1pt]  at (E) {};

\end{tikzpicture}

\end{document}

在此处输入图片描述

每条线都会给出正确的结果。但如果我执行x=0.5iny=2cm,该点就会在其中一个轴上错位。如果两个单位向量都改变,该点就会在两个轴上错位。

总结当使用 时tkz-euclide,您可以使用任何您想要的单位向量,只要它们等同于x=1cm, y=1cm

相关内容