
光学に関する講義のイラストを描くために、交差点を操作しています。しかし、次の例に示すように、いくつか問題があります。
%!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}
% >>>>>…
この例では、との間の 2 本の線は% <<<<…
同じ線を描く必要がありますが、次のようにはなっていません。
手順はほぼ同じですが(描画方向のみ異なります)、線が適切に重なりません。これを回避するにはどうすればよいでしょうか?
答え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}