我使用sort by
交点键沿线排序,期望交点编号为 1 到 5。为什么它们的编号是 4、5、3、1、2?我尝试了一下,似乎无法弄清楚顺序是如何确定的。代码和图像如下。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\draw[name path=grid] [xstep=3,ystep=2] (0,0) grid (9,8);
\draw[->, name path=line] (2,1) -- (7,7);
\draw[name intersections={of=grid and line, sort by=line, name=i, total=\t}]
\foreach \s in {1,...,\t}{(i-\s) node {\s}};
\end{tikzpicture}
\end{document}
答案1
这是 PGF 中的一个错误,我已经修复了它
使用 PGF 3.1.5 及更新版本,您问题中的示例将产生正确的顺序。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\draw[name path=grid] [xstep=3,ystep=2] (0,0) grid (9,8);
\draw[->, name path=line] (2,1) -- (7,7);
\draw[name intersections={of=grid and line, sort by=line, name=i, total=\t}]
\foreach \s in {1,...,\t}{(i-\s) node {\s}};
\end{tikzpicture}
\end{document}
答案2
迷你更新:我仔细查看了一下文件pgflibraryintersections.code.tex
,根据我现在的理解,最简单的选择可能是将线画成曲线。其他一切似乎都是为了真正改善交集库,这远远超出了我的能力。更具体地说,在pgflibraryintersections.code.tex
其中写道
请注意,可能无法获得段内的精确时间(当前,仅针对曲线路径计算,而不一定针对线路径计算)。
因此,如果用直曲线替换该线,即一条假装是曲线的线,您将得到预期的顺序 1,2,3,4,5。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\draw[name path=grid] [xstep=3,ystep=2] (9,8) grid (0,0);
\draw[->, name path=line] (2,1) to[bend left=0] (7,7);
\draw[name intersections={of=grid and line, sort by=line, name=i, total=\t}]
\foreach \s in {1,...,\t}{(i-\s) node {\s}};
\end{tikzpicture}
\end{document}
有趣的是,这样做时计算时间明显增加。同样,考虑到以下陈述,这并不太令人惊讶
注意:在撰写本文时,该库的运行时间为二次方。实验表明,它在计算两个图的约 12 个交点时表现良好,每个图有 600 个样本。当样本数量超过 700 个时,它就会失败。
答案3
- 嗯,我几乎以为我已经拥有它了。
- 这不是一个答案,但也许是其他人的一个开始。
- 我尝试从手册中的示例开始,然后使其适应您的问题。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
% https://tex.stackexchange.com/questions/417485
% Please don't get started :)
% Needed, otherwise color shift (ASdobe Reader, see for yourself)
\pdfpageattr{/Group <</S /Transparency /I true /CS /DeviceRGB>>}
\begin{document}
\section*{Your Code}
% ~~~~~~~~
\begin{tikzpicture}
\draw[name path=grid] [xstep=3,ystep=2] (0,0) grid (9,8);
\draw[->, name path=line] (2,1) -- (7,7);
\draw[name intersections={of=grid and line, sort by=line, name=i, total=\t}]
\foreach \s in {1,...,\t}{(i-\s) node {\s}};
\end{tikzpicture}
\section*{Taken from the Manual}
% ~~~~~~~~
\begin{tikzpicture}
\clip (-0.5,-0.75) rectangle (3.25,2.25);
\foreach \pathname/\shift in {line/0cm, curve/2cm}{
\tikzset{xshift=\shift}
\draw [->, name path=curve] (1,1.5) .. controls (-1,1) and (2,0.5) .. (0,0);
\draw [->, name path=line] (0,-.5) -- (1,2) ;
\fill [name intersections={of=line and curve,sort by=\pathname, name=i}]
[red, opacity=0.5, every node/.style={left=.25cm, black, opacity=1}]
\foreach \s in {1,2,3}{(i-\s) circle (2pt) node {\footnotesize\s}};
}
\end{tikzpicture}
\section*{Playing Around}
% ~~~~~~~~
\begin{tikzpicture}
\draw [->, name path = myCurve, blue] (1,1.5) .. controls (-1,1) and (2,0.5) .. (0,0);
\node[anchor = west, blue] at (1,1.5) {Curve Start};
\node[anchor = east,yshift = -2.5mm, blue] at (0,0) {Curve End};
\draw [->, name path = myLine] (0,-.5) -- (1,2) ;
\fill [name intersections = {of = myLine and myCurve, sort by = myCurve, name = myIntersectionIndex}]
[red, opacity = 0.5, every node/.style={left = 2mm, black, opacity=1}]
% ~~~
\foreach \myForEachCounter in {1,2,3}{
(myIntersectionIndex-\myForEachCounter) circle (2pt) node {\footnotesize\myForEachCounter}
};
% ~~~
\end{tikzpicture}
\section*{Adressing Your Problem 1/2}
% ~~~~~~~~
\begin{tikzpicture}
\draw [->, name path = myLine] (1,1) -- (5,5) ;
\draw[name path = myGrid] [xstep = 3, ystep = 2] (0,0) grid (9,8);
\fill [name intersections = {of = myLine and myGrid, sort by = myLine, name = myIntersectionIndex}]
[red, opacity = 0.5, every node/.style={left = 2mm, black, opacity=1}]
% ~~~
\foreach \myForEachCounter in {1,2,3}{
(myIntersectionIndex-\myForEachCounter) circle (2pt) node {\footnotesize\myForEachCounter}
};
% ~~~
\end{tikzpicture}
\section*{Adressing Your Problem 2/2}
% ~~~~~~~~
\begin{tikzpicture}
\draw [->, name path = myLine] (0.5,2.5) -- (9,9) ;
\draw[name path = myGrid] [xstep = 2, ystep = 2] (0,0) grid (10,10);
\fill [name intersections = {of = myLine and myGrid, sort by = myLine, name = myIntersectionIndex}]
[red, opacity = 0.5, every node/.style={left = 2mm, black, opacity=1}]
% ~~~
\foreach \myForEachCounter in {1,2,3,4,5,6,7}{
(myIntersectionIndex-\myForEachCounter) circle (2pt) node {\footnotesize\myForEachCounter}
};
% ~~~
\end{tikzpicture}
\end{document}