使用intersections
库时我们可以通过命名来获取所有交叉点的位置,但是如何恢复“sort by”路径上对应的交叉点时间呢?
我知道交叉时间一定隐藏在代码的某个地方,因为贝塞尔曲线的工作方式如下:首先我们找到时间,然后获得给定时间点的位置。
在以下模拟代码中,我将研究如何定义intersection/times
样式(或类似内容),以便我能够按照“排序依据”路径(arc
在本例中已命名)恢复交叉时间。然后我想使用它们来装饰同一条路径。
\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{decorations.markings,intersections}
\tikzset{
intersection/times/.code={
\def\a{.25} % <- mock value
\def\b{.75} % <- mock value
},
test mark/.style 2 args={ % <- mark two posions on the path
decoration={ markings,
mark =at position #1 with {\draw[purple,-latex](0,0) -- (1,0);},
mark =at position #2 with {\draw[purple,-latex](0,0) -- (-1,0);}
}, decorate
},
dot/.style={insert path={node[inner sep=1pt,circle,fill=red]{}}}
}
\def\testpath{(1,0) arc(270:90:1cm)}
\begin{document}
\begin{tikzpicture}[scale=2]
\draw[red, name path=arc] \testpath;
\draw[blue, densely dotted, name path=line] (0,0) -- (1,1) --(0,2);
\draw[
name intersections={
of=arc and line, sort by=arc,
by={a,b},
times={\a,\b} % <- this is a mock style that recover the intersection times
},
test mark={\a}{\b} % <- mark at the intersection times
] \testpath (a)[dot] (b)[dot]; % <- mark at the intersection positions
\end{tikzpicture}
\end{document}
答案1
这是对您原始问题的真实答案的第一个版本,由于使用了,因此仍然很丑陋\pgfextra
。(我认为您正在寻找的宏是,它可以与迷你文档一起\pgfintersectiongetsolutiontimes
找到。)pgflibraryintersections.code.tex
不过,从评论来看,我认为你真正想要实现的目标已经可以通过以下fillbetween
库来实现:pgf图(不是钛钾Z)。我在下面说明了这一点,其中我使用此方法生成了您想要的输出(并将相应的部分涂成蓝色以进行说明)。我仍然认为你的问题非常有趣,最终如果一个人可以在不加载 pgfplots 的情况下拥有所有这些功能,那就太好了。然而,看看这个fillbetween
库的复杂性,人们可能会怀疑答案不会是一行。;-)
\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{decorations.markings,intersections}
\usepackage{pgfplots} %<-added
\usepgfplotslibrary{fillbetween} %<-added
\pgfplotsset{compat=1.16} %<-added
\tikzset{
intersection/times/.code={
\def\a{.25} % <- mock value
\def\b{.75} % <- mock value
},
test mark/.style 2 args={ % <- mark two posions on the path
decoration={ markings,
mark =at position #1 with {\draw[purple,-latex](0,0) -- (1,0);},
mark =at position #2 with {\draw[purple,-latex](0,0) -- (-1,0);}
}, decorate
},
dot/.style={insert path={node[inner sep=1pt,circle,fill=red]{}}}
}
\def\testpath{(1,0) arc(270:90:1cm)}
\begin{document}
\begin{tikzpicture}[scale=2]
\draw[red, name path=arc] \testpath;
\draw[blue, densely dotted, name path=line] (0,0) -- (1,1) --(0,2);
\path[
name intersections={
of=arc and line, sort by=arc,
by={a,b},
total=\Nint
},
]
\pgfextra{\typeout{number\space of\space intersections:\space \Nint}
\pgfintersectiongetsolutiontimes{1}{\tmp}{\dummy}
\pgfmathsetmacro{\a}{\tmp/2}
\pgfintersectiongetsolutiontimes{2}{\tmp}{\dummyTwo}
\pgfmathsetmacro{\b}{\tmp/2}
\typeout{\a,\b,\dummy}}
\testpath (a)[dot] (b)[dot]; % <- mark at the intersection positions
% desired output with fillbetween library
\path [draw,blue,
name path=middle arc,
intersection segments={
of=arc and line,
sequence={A1}
},
postaction={test mark={0}{1}}];
\end{tikzpicture}
\end{document}