我正在尝试做一些看似简单的事情,但 Latex 一直在抱怨。我想画两条线(不是函数,只是使用 \draw 命令)并填充它们之间的区域。有人能给我一个简单的答案吗?谢谢。这是我想到的一个例子......
\begin{tikzpicture}
\draw[name path = A] (2,2) circle (3cm);
\draw[name path = B] (0.3,-0.5)..controls(2.55,2)..(0.3,4.5);
\draw[name path = C] (3.7,-0.5)..controls(1.5,2)..(3.7,4.5);
\draw (2,5)--(2,-1);
\fillbetween[of=A and B];
\draw[fill] (2,2) circle [radius=0.1];
\end{tikzpicture}
答案1
您也可以使用交叉段,尽管这需要pgfplots
包和fillbetween
库。
需要图层才能使填充显示在线下方。
输出
代码
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}
\begin{document}
\begin{tikzpicture}
\draw[clip, name path = A] (2,2) circle (3cm);
\draw[name path = B] (0.3,-0.5)..controls(2.55,2)..(0.3,4.5);
\draw[name path = C] (3.7,-0.5)..controls(1.5,2)..(3.7,4.5);
\draw (2,5)--(2,-1);
%\fillbetween[of=A and B];
%\draw[fill] (2,2) circle [radius=0.1];
\begin{pgfonlayer}{bg}
\fill [orange!50,
intersection segments={
of=A and B,
sequence={L2--R2}
}];
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
答案2
通过裁剪,可以避免计算交点和圆弧,限制填充区域和圆内的线条,也有助于去除超出曲线的部分。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\clip (2,2) circle (3cm);
\draw[fill=green]
(current bounding box.south west) --
(0.3,-0.5)..controls(2.55,2)..(0.3,4.5)
-- (current bounding box.north west) -- cycle;
\draw[] (3.7,-0.5)..controls(1.5,2)..(3.7,4.5);
\draw (2,5)--(2,-1);
\draw[fill] (2,2) circle [radius=0.1];
\end{scope}
\draw (2,2) circle (3cm);
\end{tikzpicture}
\end{document}
答案3
裁剪和填充绘制:
代码:
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\draw[name path = A] (2,2) circle (3cm);
\path[name path = B] (0.3,-0.5)..controls(2.55,2)..(0.3,4.5);
\path[name path = C] (3.7,-0.5)..controls(1.5,2)..(3.7,4.5);
\draw (2,5)--(2,-1);
\path[name intersections={of=A and B,by={a,b}}];
\path[name intersections={of=A and C,by={c,d}}];
\begin{scope}
\clip (a) rectangle ([xshift=-2cm]b);
\filldraw[fill=green]
(2,2) circle (3cm);
\end{scope}
\begin{scope}
\clip (a) rectangle ([xshift=2cm]b);
\filldraw[fill=green]
(0.3,-0.5)..controls(2.55,2)..(0.3,4.5);
\end{scope}
\begin{scope}
\clip (c) rectangle ([xshift=-2cm]d);
\draw
(3.7,-0.5)..controls(1.5,2)..(3.7,4.5);
\end{scope}
\draw[fill] (2,2) circle [radius=0.1];
\end{tikzpicture}
\end{document}
最初,不绘制曲线路径,\path
而是用于寻找曲线与圆的交点;然后使用这些点与裁剪一起执行操作(绘制、填充)。