我想要填充 TikZ 绘图中某些形状的部分区域(在下图中标记为“此处”):
\documentclass{standalone}
\usepackage[tikz]{bclogo}
\usetikzlibrary{arrows,shapes,backgrounds,calc,arrows}
\begin{document}
\begin{tikzpicture}[axis/.style={very thick, ->, >=stealth'}]
% axis
\draw[axis] (9,2.5) -- (13.5,2.5) node(xline)[right]
{$x_N$};
\draw[axis] (11.25,0.5) -- (11.25,4.5) node(yline)[above] {$\hat{x}_N$};
\draw[->, ultra thick](7,2.5) -- (8.5,2.5);
\draw [very thick] plot [smooth cycle] coordinates {(2,4) (5,3) (2,0.5) (1,1.75) (1,4) };
%\draw[very thick] (0.5,3) to [ curve through ={(5.5,2) . . ( 3,0.25) . . (1,0.25) }]( 0.5,3 ) ;
\draw[dashed, very thick] (11.25,2.5) ellipse (1cm and 1.5cm);
\draw[dashed, very thick] (5,3) ellipse (1.5cm and 1cm);
\draw[step=0.25cm,gray,very thin] (0,0) grid (15,5);
\node (a) at (4,3) {Here};
\node (b) at (11.7,3) {Here};
\node (a) at (11.7,2) {Here};
\end{tikzpicture}
\end{document}
我怎样才能实现这个目标?
答案1
这就是你想要的吗?这里scope
使用了环境。请注意,clip
命令必须包含在scope
环境中才能限制剪辑的效果。如果没有scope
环境,剪辑效果将持续到代码结束。
代码
\documentclass{standalone}
\usepackage[tikz]{bclogo}
\usetikzlibrary{arrows,shapes,backgrounds,calc,arrows,patterns}
\begin{document}
\begin{tikzpicture}[axis/.style={very thick, ->, >=stealth'}]
% axis
\draw[axis] (9,2.5) -- (13.5,2.5) node(xline)[right]
{$x_N$};
\draw[axis] (11.25,0.5) -- (11.25,4.5) node(yline)[above] {$\hat{x}_N$};
\draw[->, ultra thick](7,2.5) -- (8.5,2.5);
\begin{scope}
\draw [very thick] plot [smooth cycle] coordinates {(2,4) (5,3) (2,0.5) (1,1.75) (1,4) };
\clip (5,3) ellipse (1.5cm and 1cm);
\draw [fill=red,very thick] plot [smooth cycle] coordinates {(2,4) (5,3) (2,0.5) (1,1.75) (1,4) };
\end{scope}
\begin{scope}
\draw [dashed,very thick] (11.25,2.5) ellipse (1cm and 1.5cm);
\clip (11.25,2.5) ellipse (1cm and 1.5cm);
\fill [red] (11.25,0) rectangle (15,5);
\end{scope}
\draw[dashed, very thick] (5,3) ellipse (1.5cm and 1cm);
\draw[step=0.25cm,gray,very thin] (0,0) grid (15,5);
\node (a) at (4,3) {Here};
\node (b) at (11.7,3) {Here};
\node (a) at (11.7,2) {Here};
\end{tikzpicture}
\end{document}
编辑:使用patterns
来自 tikzlibrary 并输入pattern=crosshatch dots
选项fill/draw
,可以得到
答案2
这是一个使用非零规则和奇偶规则填充路径的解决方案。我所做的就是使用这些命令调整您的代码。它可以做得更干净。此外,您应该使用范围来分组项目。
这个例子主要是为了展示用非零和奇偶规则可以做什么。
\documentclass{standalone}
\usepackage[tikz]{bclogo}
\usetikzlibrary{arrows,shapes,backgrounds,calc,arrows}
\begin{document}
\begin{tikzpicture}[axis/.style={very thick, ->, >=stealth'}]
%note the inversion of the plot points. This is to make the nonzero rule
%work properly on this path
\fill [nonzero rule,blue,postaction={even odd rule,fill=white}]
plot [smooth cycle] coordinates {(1,4)(1,1.75)(2,0.5)(5,3)(2,4)}
(5,3) ellipse (1.5cm and 1cm);
\draw [blue,very thick] plot [smooth cycle] coordinates {(2,4) (5,3) (2,0.5) (1,1.75) (1,4) };
\draw[red,dashed, very thick] (5,3) ellipse (1.5cm and 1cm);
\fill[gray] (11.25,2.5) ellipse (1cm and 1.5cm);
\fill[even odd rule,white]
(11.25,2.5) ellipse (1cm and 1.5cm)
(11.25,0.5) rectangle (13.25,4.5);
\draw[dashed, very thick] (11.25,2.5) ellipse (1cm and 1.5cm);
\draw[->, ultra thick](7,2.5) -- (8.5,2.5);
% axis
\draw[axis] (9,2.5) -- (13.5,2.5) node(xline)[right]
{$x_N$};
\draw[axis] (11.25,0.5) -- (11.25,4.5) node(yline)[above] {$\hat{x}_N$};
\draw[step=0.25cm,gray,very thin] (0,0) grid (15,5);
\node (a) at (4,3) {Here};
\node (b) at (11.7,3) {Here};
\node (a) at (11.7,2) {Here};
\end{tikzpicture}
\end{document}