我想绘制一个任意域,我可以沿着任意曲线将其分成两部分,这样我就可以使用不同的颜色为两个子域着色。我尝试使用业余爱好库绘制域,但我不知道如何单独着色。我在下面附上我尝试的代码。
\documentclass[tikz]{standalone}
\usetikzlibrary{hobby}
\begin{document}
\begin{tikzpicture}[use Hobby shortcut,closed=true]
\fill[draw=black, fill=orange] (-3.5,0.5) .. (-3,2.5) .. (-1,3.5).. (1.5,3).. (4,2.5).. (5,0.5) ..(2.5,-2).. (0,-1.5).. (-2.5,-2).. (-3.5,0.5);
\draw[black] (-3.5, 0.5) to [out=0, in=-135] (-1.5,1) to[out=45, in=-118] (1.5,0) to[out=62, in=150] (5,0.5);
\node[above] at (0,1.5) {Colour A};
\node[above] at (0, -1) {Colour B};
\end{tikzpicture}
\end{document}
非常感谢!
答案1
例如,无需修改曲线即可使用剪辑:
\documentclass[tikz]{standalone}
\usetikzlibrary{hobby}
\begin{document}
\begin{tikzpicture}[use Hobby shortcut,closed=true]
%\fill[draw=black, fill=orange]
\def\OuterCurve{%
(-3.5,0.5) .. (-3,2.5) .. (-1,3.5) .. (1.5,3) .. (4,2.5) ..
(5,0.5) ..(2.5,-2).. (0,-1.5).. (-2.5,-2).. (-3.5,0.5)
}
\coordinate (Left) at (-3.5, 0.5);
\coordinate (Right) at (5, 0.5);
\def\InnerCurve{%
(Left) to [out=0, in=-135] (-1.5,1) to[out=45, in=-118]
(1.5,0) to[out=62, in=150] (Right)
}
% Fill areas
\begin{scope}
\clip[use Hobby shortcut, closed=true] \OuterCurve;
\begin{scope}
\clip \InnerCurve
-- (current bounding box.east |- Right)
-- (current bounding box.south east)
-- (current bounding box.south west)
-- (current bounding box.west |- Left)
-- cycle
;
\fill[green] (current bounding box.south west)
rectangle (current bounding box.north east)
;
\end{scope}
\clip \InnerCurve
-- (current bounding box.east |- Right)
-- (current bounding box.north east)
-- (current bounding box.north west)
-- (current bounding box.west |- Left)
-- cycle
;
\fill[orange] (current bounding box.south west)
rectangle (current bounding box.north east)
;
\end{scope}
% Draw black curves
\draw[use Hobby shortcut, closed=true] \OuterCurve;
\draw \InnerCurve;
\node[above] at (0,1.5) {Colour A};
\node[above] at (0, -1) {Colour B};
\end{tikzpicture}
\end{document}
评论:
剪切是附加的,
\clip
同一范围内的进一步命令通过将当前剪切路径与新路径相交来减少剪切区域。剪切的范围可以由环境控制
scope
。\end{scope}
恢复先前的剪切路径。