在 Tikz 中定义着色区域

在 Tikz 中定义着色区域

在此处输入图片描述

经过大量工作,我弄清楚了如何制作不对称集。现在我很难确定应该在哪里填充颜色。正如您在图片中看到的那样,当我填充颜色时,它会填充其中的所有内容。我试图弄清楚如何在“封闭球”处停止着色。如果有人有想法,我将不胜感激。

我迄今为止的代码:

\begin{center}
\begin{tikzpicture}
\draw [thick]  plot[smooth, tension=.7] coordinates {(-4,2.5) (-3,3) (-2,2.8) (-0.8,2.5) (-0.5,1.5) (0.5,0) (0,-2)(-1.5,-2.5) (-4,-2) (-3.5,-0.5) (-5,1) (-4,2.5)};
%Open set
\draw [dashed,fill=black!20!green!60!white]  plot[smooth, tension=.7] coordinates {(-3,2.5) (-3.5,2.2) (-4,1.5) (-3,0) (-3,-1) (-2.5,-1.7) (-1.5,-2) (-0.5,-1) (-0.5,0) (-1,1.5) (-2.5,2.5) (-3,2.5)};
%Open ball
\node [dashed] at (-2.1,0.5)[shape=circle,draw,inner sep=12pt] {};
%Closed ball
\node at (-2,0.5)[shape=circle,draw,inner sep=18pt] {};
%Nodes and names
\node at (-2.5,3.5) {$X$};
\node at (-1.8,-1) {$f^{-1}(V)$};
\node at (-2,0.5) {$x_0$};
\end{tikzpicture}
\end{center}

答案1

只需重新排序封闭和开放的圆圈,并用白色填充封闭的圆圈。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [thick]  plot[smooth, tension=.7] coordinates {(-4,2.5) (-3,3) (-2,2.8) (-0.8,2.5) (-0.5,1.5) (0.5,0) (0,-2)(-1.5,-2.5) (-4,-2) (-3.5,-0.5) (-5,1) (-4,2.5)};
%Open set
\draw [dashed,fill=black!20!green!60!white]  plot[smooth, tension=.7] coordinates {(-3,2.5) (-3.5,2.2) (-4,1.5) (-3,0) (-3,-1) (-2.5,-1.7) (-1.5,-2) (-0.5,-1) (-0.5,0) (-1,1.5) (-2.5,2.5) (-3,2.5)};
%Closed ball
\node [fill=white] at (-2,0.5)[shape=circle,draw,inner sep=18pt] {};
%Open ball
\node [dashed] at (-2.1,0.5)[shape=circle,draw,inner sep=12pt] {};
%Nodes and names
\node at (-2.5,3.5) {$X$};
\node at (-1.8,-1) {$f^{-1}(V)$};
\node at (-2,0.5) {$x_0$};
\end{tikzpicture}
\end{document}

输出:

蒂克兹

答案2

如果你真的需要不填您可以使用键来控制绘图的内部部分even odd rule。不幸的是,这会让事情变得更加复杂,因为您无法将draw选项从外边dashed框更改solid为内边框...

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
    \usetikzlibrary{scopes}
    % just to show that the "inner part" of the open set really isn't filled
    % with a color, define a background layer and set it "below" the main
    % layer
    \pgfdeclarelayer{background}
    \pgfsetlayers{background,main}
\begin{document}
\begin{tikzpicture}[
    % at the end of the picture fill the background layer with a shading to
    % prove that there is nothing drawn inside of the open set
    execute at end picture={
        \begin{pgfonlayer}{background}
            \shade [
                shading=radial,
                rounded corners,
            ]
                (current bounding box.south west) rectangle
                (current bounding box.north east);
        \end{pgfonlayer}
    },
]
    \draw [thick]
        plot [smooth, tension=.7] coordinates {
            (-4,2.5) (-3,3) (-2,2.8) (-0.8,2.5) (-0.5,1.5) (0.5,0)
            (0,-2)(-1.5,-2.5) (-4,-2) (-3.5,-0.5) (-5,1) (-4,2.5)
        }
    ;
    %Open set
    \fill [
        fill=black!20!green!60!white,
        even odd rule,
    ]
        plot [smooth, tension=.7] coordinates {
            (-3,2.5) (-3.5,2.2) (-4,1.5) (-3,0) (-3,-1) (-2.5,-1.7) (-1.5,-2)
            (-0.5,-1) (-0.5,0) (-1,1.5) (-2.5,2.5) (-3,2.5)
        }
    % don't fill closed ball
        [radius=1] (-2,0.5) circle
    ;
    % draw dashed line for open set
    \draw [
        dashed,
    ]
        plot [smooth, tension=.7] coordinates {
            (-3,2.5) (-3.5,2.2) (-4,1.5) (-3,0) (-3,-1) (-2.5,-1.7) (-1.5,-2)
            (-0.5,-1) (-0.5,0) (-1,1.5) (-2.5,2.5) (-3,2.5)
        }
    ;
    % draw closed ball
    \draw [radius=1] (-2,0.5) circle;

    %Open ball
    \node [dashed] at (-2.1,0.5)[shape=circle,draw,inner sep=12pt] {};
    %Nodes and names
    \node at (-2.5,3.5) {$X$};
    \node at (-1.8,-1) {$f^{-1}(V)$};
    \node at (-2,0.5) {$x_0$};
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容