在tikz 中绘制了两条相交曲线(不一定是直线),它们有一个交点,但希望在交点处不重合,
就是这样的效果:
而不是这样的效果:
我考虑了一个思路:
理论上以上是可以形成我想要的效果,但操作太麻烦,代码也更复杂(有时候近似是二倍原来的代码数量),特别是图比较复杂时,有时想找到所有有交点的曲线都难。
我考虑过用 foreach 实现,这个有可能可以,但我目前还没做到。
请问哪位大佬有其他的更简洁的方法?或者我这个思路如何用 foreach 或者其他自动化命令实现?谢谢!
虽然已经有最佳答案了,我也来补充一个治标的方法,需要保证连续的线可以使用双线 double 绘制,选项 [draw = white, double=black,double distance =0.4pt](当然如果图线颜色和底色并非黑白的时候,以及线宽非默认时要自己调整),例:
\begin{tikzpicture}
\draw(0,0)--(1,1);
\draw[draw=white,double=black,double distance =0.4pt](0,1)--(1,0);
\end{tikzpicture}
参考knots包虽然已经有最佳答案了,我也来补充一个治标的方法,需要保证连续的线可以使用双线
double绘制,选项[draw = white, double=black,double distance =0.4pt]
也许比较治本的方法。
\documentclass[tikz,border=5pt]{standalone}
% https://tex.stackexchange.com/a/762955/322482
% https://github.com/loopspace/spath3/issues/37
\ExplSyntaxOn
\msg_redirect_name:nnn { kernel } { variant-same-as-base } { info }
\ExplSyntaxOff
\usetikzlibrary{knots}
\ExplSyntaxOn
\msg_redirect_name:nnn { kernel } { variant-same-as-base } { error }
\ExplSyntaxOff
\begin{document}
\begin{tikzpicture}
\begin{knot}[clip width=5, flip crossing=1]
\strand[red, ultra thick] (0,0) .. controls +(1,0) and +(-1,0) .. (2,1) .. controls +(1,0) and +(-1,0) .. (4,0);
\strand[blue, ultra thick] (0,1) .. controls +(1,0) and +(-1,0) .. (2,0) .. controls +(1,0) and +(-1,0) .. (4,1);
\end{knot}
\end{tikzpicture}
\end{document}
\documentclass[tikz,border=5pt]{standalone}
% https://tex.stackexchange.com/a/762955/322482
% https://github.com/loopspace/spath3/issues/37
\ExplSyntaxOn
\msg_redirect_name:nnn { kernel } { variant-same-as-base } { info }
\ExplSyntaxOff
\usetikzlibrary{intersections,spath3}
\ExplSyntaxOn
\msg_redirect_name:nnn { kernel } { variant-same-as-base } { error }
\ExplSyntaxOff
\begin{document}
\begin{tikzpicture}
\path[spath/save=pathA] (0,0) .. controls +(1,0) and +(-1,0) .. (2,1) .. controls +(1,0) and +(-1,0) .. (4,0);
\path[spath/save=pathB] (0,1) .. controls +(1,0) and +(-1,0) .. (2,0) .. controls +(1,0) and +(-1,0) .. (4,1);
\tikzset{
spath/split at intersections={pathA}{pathB},
spath/insert gaps after components={pathA}{5pt}{1},
spath/insert gaps after components={pathB}{5pt}{2},
}
\draw[red, ultra thick, spath/use=pathA];
\draw[blue, ultra thick, spath/use=pathB];
\end{tikzpicture}
\end{document}
那个最佳答案里的 knot 宏包实际上也有问题,不能满足我的要求
...? 或许补充更多原始需求的信息...?
类似花老师的这个需求:
谢谢!