我正在尝试使用交叉点来绘制有关光学的讲座插图。但是,我遇到了一些问题,如以下示例所示:
%!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}