為什麼這兩個 TikZ 指令會產生不同的結果?

為什麼這兩個 TikZ 指令會產生不同的結果?

我正在玩弄交叉點,為光學講座繪製插圖。但是,我遇到了一些問題,如下例所示:

%!TEX encoding = UTF-8 Unicode
%!TEX program = lualatex
\documentclass[11pt,a4paper,fleqn,pdftex]{report}
\usepackage[dvipsnames, table]{xcolor}
\usepackage[utf8]{luainputenc} 
\usepackage[latin,english]{babel}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{shapes.geometric}

\begin{document}
    \begin{figure}[!h]
   \centering
   \begin{tikzpicture}
      \coordinate (M2) at (0,3);
      \def\angleM{10}
% >>>>>>>>>>>>>>>>
      \draw[color=Red, thick] (M2) --  (intersection cs: first line={(M2)--++(90 - \angleM:-8)}, second line={(45:-4)--(45:4)}) circle (2pt);
      \draw[color=Red, thick] (intersection cs: first line={(M2)--++(90 - \angleM:-8)}, second line={(45:-4)--(45:4)}) circle (2pt) -- (M2);
% <<<<<<<<<<<<<<<<
      \draw[thick] (M2) ++(1,0) -- ++(-2,0) node[left]{$M_2$};
      \fill[pattern=north east lines] (M2) ++(1,0) rectangle ++(-2,0.2);
      \draw[very thick] (45:-2) -- (45:2);
   \end{tikzpicture}
\end{figure}
\end{document}

% >>>>>…在此範例中,和之間的兩條線% <<<<…應繪製相同的線,但不像您在此處看到的那樣:

路口

雖然說明幾乎相同(僅繪製方向不同),但線條並未以應有的方式重疊。我可以做什麼來避免這種情況?

答案1

這似乎是一個舍入問題。這個問題和中討論的問題TikZ 不計算交集提示交集座標系存在一些問題(實際上PGF手冊並沒有提及,只是給了它的使用範例)。

無論如何,作為一種解決方法,您可以加載intersections庫然後執行

\path
  [name path=line1] (M2) -- ++(90 - \angleM:-8) 
  [name path=line2] (45:-4) -- (45:4); 
\draw[Red,thick,name intersections={of=line1 and line2}] 
  (intersection-1) circle (2pt) -- (M2);

這只是比使用 長一點intersection cs,但您不會因舍入問題導致的不同結果而煩惱。並排顯示這兩種方法的完整範例:

%!TEX encoding = UTF-8 Unicode
%!TEX program = lualatex
\documentclass[11pt,a4paper,fleqn,pdftex]{report}
\usepackage[dvipsnames, table]{xcolor}
\usepackage[utf8]{luainputenc} 
\usepackage[latin,english]{babel}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{intersections}

\begin{document}

   \begin{tikzpicture}[baseline]
      \coordinate (M2) at (0,3);
      \def\angleM{10}
% >>>>>>>>>>>>>>>>
      \draw[color=Red, thick] (M2) --  (intersection cs: first line={(M2)--++(90 - \angleM:-8)}, second line={(45:-4)--(45:4)}) circle (2pt);
      \draw[color=Red, thick] (intersection cs: first line={(M2)--++(90 - \angleM:-8)}, second line={(45:-4)--(45:4)}) circle (2pt) -- (M2);
% <<<<<<<<<<<<<<<<
      \draw[thick] (M2) ++(1,0) -- ++(-2,0) node[left]{$M_2$};
      \fill[pattern=north east lines] (M2) ++(1,0) rectangle ++(-2,0.2);
      \draw[very thick] (45:-2) -- (45:2);
   \end{tikzpicture}\qquad
%
   \begin{tikzpicture}[baseline]
      \coordinate (M2) at (0,3);
      \def\angleM{10}

      \path
        [name path=line1] (M2) -- ++(90 - \angleM:-8) 
        [name path=line2] (45:-4) -- (45:4); 
      \draw[Red,thick,name intersections={of=line1 and line2}] 
        (intersection-1) circle (2pt) -- (M2);

      \draw[thick] (M2) ++(1,0) -- ++(-2,0) node[left]{$M_2$};
      \fill[pattern=north east lines] (M2) ++(1,0) rectangle ++(-2,0.2);
      \draw[very thick] (45:-2) -- (45:2);
   \end{tikzpicture}

\end{document}

在此輸入影像描述

相關內容