我试图绘制两个“斑点”,以及从一个斑点到另一个斑点的曲线,两个斑点之间的颜色不同。以下是 MWE:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usetikzlibrary{hobby}
\begin{document}
\tikzset{point/.style={fill,circle,minimum size=3pt,inner sep=0pt,outer sep=0pt}}
\begin{tikzpicture}
% Blob 1
\draw[
name path=blob1,
] (0,0) to[closed,
curve through={(3,3) .. (6.5,0) .. (11,1) .. (13,-2) .. (9, -4 ) .. (3,-2.5)}
] (1,-4.5);
% Blob 2
\draw[
name path=blob2,
] (5,4) to[closed, curve through={(7,5)}] (7,1);
% Curve from blob 1 to blob 2
\draw[
name path=connector,
] (1,-2) node[point,label=A] (A) {}
to[curve through={(3,0) .. (6,-1)}] (8,3)
(8,3) node[point,label=B] (B) {};
\draw[
name intersections={of=blob1 and connector,by=exit},
name intersections={of=blob2 and connector,by=entry},
]
node[point,red,label=exit] at (exit) {}
node[point,green,label=entry] at (entry) {}
;
\end{tikzpicture}
\end{document}
我想要的是将曲线(假设)从A
到涂成绿色,从到 涂exit
成红色,然后从 到 再次涂成绿色。exit
entry
entry
B
我研究了装饰来实现这一点,我认为如果我能将交叉点转换为路径上的位置,我就可以做到这一点connector
。但我不确定这是否可行。
答案1
PGFPlotsintersection segments
正是为此而生的。
这是无需额外库的替代解决方案。除非您想在那里添加一个点,否则不需要计算交点。
\documentclass[tikz, border=1 cm]{standalone}
\usetikzlibrary{hobby}
\begin{document}
\begin{tikzpicture}
\draw[thick, red] (1,-2) coordinate (A) to[curve through={(3,0) .. (6,-1)}] (8,3) (8,3) coordinate (B);
\draw[fill=white] (0,0) to[closed, curve through={(3,3) .. (6.5,0) .. (11,1) .. (13,-2) .. (9, -4 ) .. (3,-2.5)}] (1,-4.5);
\draw[fill=white] (6.7,3) circle[radius=2];
\begin{scope}
\clip (0,0) to[closed, curve through={(3,3) .. (6.5,0) .. (11,1) .. (13,-2) .. (9, -4 ) .. (3,-2.5)}] (1,-4.5);
\draw[thick, green] (1,-2) to[curve through={(3,0) .. (6,-1)}] (8,3);
\end{scope}
\begin{scope}
\clip (6.7,3) circle[radius=2];
\draw[thick, blue] (1,-2) to[curve through={(3,0) .. (6,-1)}] (8,3);
\end{scope}
\fill (A) circle[radius=1.5pt] node[above left] {A};
\fill (B) circle[radius=1.5pt] node[above left] {B};
\end{tikzpicture}
\end{document}
答案2
您可以spath3
仅出于此目的使用该库。它允许您在一条路径与任何其他路径相交时分割该路径,然后将每个部分作为一条新路径。
像这样:
\documentclass {standalone}
\usepackage {tikz}
\usetikzlibrary{hobby}
\usetikzlibrary{intersections}
\usetikzlibrary{spath3} % for splitting the path 'connector'
\begin{document}
\tikzset{point/.style={fill,circle,minimum size=3pt,inner sep=0pt,outer sep=0pt}}
\begin{tikzpicture}
% Blob 1
\draw[
name path=blob1,
] (0,0) to[closed,
curve through={(3,3) .. (6.5,0) .. (11,1) .. (13,-2) .. (9, -4 ) .. (3,-2.5)}
] (1,-4.5);
% Blob 2
\draw[
name path=blob2,
] (5,4) to[closed, curve through={(7,5)}] (7,1);
% Curve from blob 1 to blob 2
\path[% we create the path, but don't draw it
name path=connector,
] (1,-2) node[point,label=A] (A) {}
to[curve through={(3,0) .. (6,-1)}] (8,3)
(8,3) node[point,label=B] (B) {};
\tikzset
{% here we split the path 'connector' and get its components
spath/split at intersections={connector}{blob1},
spath/split at intersections={connector}{blob2},
spath/get components of={connector}\cpts,
}
% and then we draw the components separately
\draw[green,spath/use=\getComponentOf\cpts{1}];
\draw[red ,spath/use=\getComponentOf\cpts{2}];
\draw[green,spath/use=\getComponentOf\cpts{3}];
\draw[
name intersections={of=blob1 and connector,by=exit},
name intersections={of=blob2 and connector,by=entry},
]
node[point,red,label=exit] at (exit) {}
node[point,green,label=entry] at (entry) {}
;
\end{tikzpicture}
\end{document}