我正在尝试填充由爱好库创建的两条曲线之间的空间。我尝试了这个帖子,但是没有成功。有人能帮我吗?谢谢
\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{decorations.shapes}
\usetikzlibrary{hobby}
\usetikzlibrary{calc}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[scale=1, use Hobby shortcut]
\draw[name path = A]
(3.18, 1.05)..
(2.7,.58)..
(2.45,-.95)..
(1.81,-1.35)
;
\draw[name path = B]
(3.18, 1.05)..
(1.73,1.01)..
(0,.57)..
(-1.07,0)..
(-1.43, -.26)..
(-1.76,-.61)..
(-1.81,-.89)..
(-1.56,-.91)..
(-.95, -.78)..
(0,-.66)..
(.79,-.7)..
(1.23,-.88)..
(1.81,-1.35)
;
% \path[name intersections={of=A and B,by={a,b}}];
% \begin{scope}
% \clip (a) rectangle ([xshift=-2cm]b);
% \filldraw[fill=green]
% (2,2) circle (3cm);
% \end{scope}
\end{tikzpicture}
\end{document}
答案1
这是 Kpym 使用我的spath3
库。最初(在首次撰写时)这需要将一些内部命令提升到用户级别。感谢 Kpym 的提示,我已经正确实现了这一点,并且更新现已进入 CTAN 和 TeXLive 存储库。
以下是使用该库实现此目的的两种方法spath3
。一种方法是分别保存两个路径,然后在第三个命令中将它们合并。第二种方法是保存其中一个路径,然后将其插入第二个路径之后。两种情况下的输出相同。
\documentclass{article}
%\url{https://tex.stackexchange.com/q/484027/86}
\usepackage{tikz}
\usetikzlibrary{hobby, calc, spath3}
\begin{document}
\begin{tikzpicture}[use Hobby shortcut]
\path[spath/save = A]
(3.18, 1.05)..
(2.7,.58)..
(2.45,-.95)..
(1.81,-1.35)
;
\path[spath/save = B]
(3.18, 1.05)..
(1.73,1.01)..
(0,.57)..
(-1.07,0)..
(-1.43, -.26)..
(-1.76,-.61)..
(-1.81,-.89)..
(-1.56,-.91)..
(-.95, -.78)..
(0,-.66)..
(.79,-.7)..
(1.23,-.88)..
(1.81,-1.35)
;
\filldraw[
fill=blue,
draw=black,
ultra thick,
spath/restore=A
]
[spath/append reverse=B]
-- cycle;
\end{tikzpicture}
\begin{tikzpicture}[use Hobby shortcut]
\path[overlay,spath/save = A]
(3.18, 1.05)..
(2.7,.58)..
(2.45,-.95)..
(1.81,-1.35)
;
\filldraw[fill=blue, draw=black, ultra thick]
(3.18, 1.05)..
(1.73,1.01)..
(0,.57)..
(-1.07,0)..
(-1.43, -.26)..
(-1.76,-.61)..
(-1.81,-.89)..
(-1.56,-.91)..
(-.95, -.78)..
(0,-.66)..
(.79,-.7)..
(1.23,-.88)..
([Hobby finish]1.81,-1.35) % needed to trigger hobby path generation before adding the new path
[spath/append reverse=A] -- cycle
;
\end{tikzpicture}
\end{document}
答案2
您正在加载pgfplots
及其库fillbetween
,因此您可能还想使用它们。有时,挑战在于找到相关的段,因为恰巧 Ti钾Z 找到“太多”交点。另一个需要注意的是,这里您不需要这样做,您只需还原其中一条路径中坐标的顺序并将它们拼凑在一起即可(右图)。
\documentclass{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{hobby}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[scale=1, use Hobby shortcut]
\draw[name path=A]
(3.18, 1.05)..
(2.7,.58)..
(2.45,-.95)..
(1.81,-1.35)
;
\draw[name path=B]
(3.18, 1.05)..
(1.73,1.01)..
(0,.57)..
(-1.07,0)..
(-1.43, -.26)..
(-1.76,-.61)..
(-1.81,-.89)..
(-1.56,-.91)..
(-.95, -.78)..
(0,-.66)..
(.79,-.7)..
(1.23,-.88)..
(1.81,-1.35)
;
\path[%draw=red,
fill=blue,intersection segments={of=A and B,
sequence={L2[reverse]--R2}}];
\begin{scope}[xshift=5cm]
\draw[fill=blue] (3.18, 1.05)..
(1.73,1.01)..
(0,.57)..
(-1.07,0)..
(-1.43, -.26)..
(-1.76,-.61)..
(-1.81,-.89)..
(-1.56,-.91)..
(-.95, -.78)..
(0,-.66)..
(.79,-.7)..
(1.23,-.88)..
(1.81,-1.35)
(1.81,-1.35)..
(2.45,-.95)..
(2.7,.58)..
(3.18, 1.05);
\end{scope}
\end{tikzpicture}
\end{document}
答案3
由于路径的端点相同,因此您可以保存两条路径,反转其中一条路径,然后将它们组合起来以填充中间部分。
最好的解决方案可能是使用包含您需要的一切的“spath3”库,但我不知道如何使用该库来反转路径:(
show path construction
因此,鉴于我的无知,我创建了以下代码来保存和反转路径这个答案。
\documentclass[varwidth,border=7pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{
store savedtikzpath in/.code=\xdef#1{\savedtikzpath},
append tikz path/.style = {
decoration={show path construction,
moveto code=\xdef\savedtikzpath{\savedtikzpath (\tikzinputsegmentfirst)},
lineto code=\xdef\savedtikzpath{\savedtikzpath -- (\tikzinputsegmentlast)},
curveto code=\xdef\savedtikzpath{\savedtikzpath .. controls%
(\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb) ..(\tikzinputsegmentlast)},
closepath code=\xdef\savedtikzpath{\savedtikzpath -- cycle}
},
decorate,
postaction = {store savedtikzpath in=#1}
},
prepend reversed tikz path/.style = {
decoration={show path construction,
moveto code=\xdef\savedtikzpath{(\tikzinputsegmentfirst) \savedtikzpath},
lineto code=\xdef\savedtikzpath{(\tikzinputsegmentlast) -- \savedtikzpath},
curveto code=\xdef\savedtikzpath{(\tikzinputsegmentlast) .. controls%
(\tikzinputsegmentsupportb) and (\tikzinputsegmentsupporta) .. \savedtikzpath},
closepath code=\xdef\savedtikzpath{\savedtikzpath -- cycle}
},
decorate,
postaction = {store savedtikzpath in=#1}
},
append tikz path/.default = \savedtikzpath,
save tikz path/.style = {append tikz path=#1},
save tikz path/.prefix code={\xdef\savedtikzpath{}},
save reversed tikz path/.style = {prepend reversed tikz path=#1},
save reversed tikz path/.prefix code={\xdef\savedtikzpath{}},
}
\usetikzlibrary{hobby}
\begin{document}
\begin{tikzpicture}[use Hobby shortcut]
\path[save tikz path=\pathA] (3.18, 1.05).. (2.7,.58).. (2.45,-.95).. (1.81,-1.35);
\path[save reversed tikz path=\pathB] (3.18, 1.05).. (1.73,1.01).. (0,.57).. (-1.07,0).. (-1.43, -.26).. (-1.76,-.61).. (-1.81,-.89).. (-1.56,-.91).. (-.95, -.78).. (0,-.66).. (.79,-.7).. (1.23,-.88).. (1.81,-1.35);
\filldraw[ultra thick, red,fill=blue] \pathA -- \pathB -- cycle;
\end{tikzpicture}
\end{document}