Tikz 在不知道边界坐标的情况下填充凸包的子区域

Tikz 在不知道边界坐标的情况下填充凸包的子区域

各位网友大家好,

我想对凸包的部分表面进行着色。着色区域应受坐标 (0.5,0.5) 和凸包中 x 和 y 坐标均大于或等于 0.5 的部分限制。我所做的就是猜测 (0.5,y) 和 (x,0.5) 线与凸包相交的两个 x 和 y 值。但是,我想知道是否有更简单的解决方案,这样我就可以在不知道 (1.25,0.5) 和 (0.5,1.25) 坐标的情况下对该区域进行着色(这样 tikz 必须自己找到坐标)。

我还想在某些特定位置标记轴(见图)。提前谢谢!

\documentclass[a4paper, 12pt, parskip]{scrreprt}  
\usepackage{tikz}  
\usetikzlibrary{calc} %For graphics
\begin{document}

\begin{figure}[ht]
\centering
\begin{tikzpicture}
    \coordinate (Origin) at (0,0);
    \coordinate (XAxisMin) at (-1.5,0);
    \coordinate (XAxisMax) at (2.5,0);
    \coordinate (YAxisMin) at (0,-1.5);
    \coordinate (YAxisMax) at (0,2.5);
    \draw [thin, gray, -latex] (XAxisMin) -- (XAxisMax);
    \draw [thin, gray, -latex] (YAxisMin) -- (YAxisMax);
    \node[draw,circle,inner sep=2pt,fill] at (-1,-1) {}; %{} label
    \node[draw,circle,inner sep=2pt,fill] at (0,0) {};
    \node[draw,circle,inner sep=2pt,fill] at (2,1) {};
    \node[draw,circle,inner sep=2pt,fill] at (1,2) {};
    \draw (-1,-1) -- (1,2) -- (2,1) -- cycle; %This is the convex hull
    \filldraw[fill=gray, fill opacity=0.3, draw=black] (0.5,0.5) -- (1.25,0.5) -- (2,1) -- (1,2) -- (0.5,1.25) -- cycle;
\end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

答案1

像这样:

\documentclass[tikz=true]{standalone} %
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc} %For graphics

\begin{document}
\begin{tikzpicture}

\begin{axis}[%
    axis y line=center,
    axis x line=middle,
    ymin = -3,ymax = 3,
    xmin = -3,xmax = 3,
    xtick={1,2},
    ytick={0.5}
    ]%

\node[draw,circle,inner sep=2pt,fill] at (axis cs:-1,-1) {};
\node[draw,circle,inner sep=2pt,fill] at (axis cs:0,0) {};
\node[draw,circle,inner sep=2pt,fill] at (axis cs:2,1) {};
\node[draw,circle,inner sep=2pt,fill] at (axis cs:1,2) {};
\draw (-1,-1) -- (1,2) -- (2,1) -- cycle; %This is the convex hull
\filldraw[fill=gray, fill opacity=0.3, draw=black] (axis cs:0.5,0.5) -- (axis cs:1.25,0.5) -- (axis cs:2,1) -- (axis cs:1,2) -- (axis cs:0.5,1.25) -- cycle;

\end{axis}
\end{tikzpicture}

\end{document}

这里没有描述

答案2

使用元帖子以及声明式编程的魔力......

在此处输入图片描述

编译lualatex或研究如何使其适应简单的mpost

\documentclass[border=5mm]{standalone}
\usepackage{luatex85}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    numeric u; u = 1 cm;
    z0 = origin;
    z1 = -(1, 1) scaled u;
    z2 =  (2, 1) scaled u;
    z3 =  (1, 2) scaled u;

    z4 = (1/2, 1/2) scaled u;

    z5 = whatever[z1, z2];
    z6 = whatever[z1, z3];

    x4 = x6; 
    y4 = y5;

    path xx, yy;
    xx = (3/2 left -- 5/2 right) scaled u;
    yy = xx rotated 90;

    drawarrow xx withcolor 1/2 white;
    drawarrow yy withcolor 1/2 white;

    fill z4--z5--z2--z3--z6--cycle withcolor 7/8 white;

    draw z1--z2--z3--cycle;  
    draw z5--z4--z6;

    for i=0 upto 3: fill fullcircle scaled dotlabeldiam shifted z[i]; endfor

endfig;
\end{mplibcode}
\end{document}

答案3

您可以简单地剪辑凸包。编辑:添加了注释,如果没有 Jasper Habicht 的善意提醒,我不会注意到我忘记添加它们。

\documentclass[a4paper, 12pt, parskip]{scrreprt}  
\usepackage{tikz}  
\usetikzlibrary{calc} %For graphics
\begin{document}

\begin{figure}[ht]
\centering
\begin{tikzpicture}
    \coordinate (Origin) at (0,0);
    \coordinate (XAxisMin) at (-1.5,0);
    \coordinate (XAxisMax) at (2.5,0);
    \coordinate (YAxisMin) at (0,-1.5);
    \coordinate (YAxisMax) at (0,2.5);
    \draw [thin, gray, -latex] (XAxisMin) -- (XAxisMax);
    \draw [thin, gray, -latex] (YAxisMin) -- (YAxisMax);
    \node[draw,circle,inner sep=2pt,fill] at (-1,-1) {}; %{} label
    \node[draw,circle,inner sep=2pt,fill] at (0,0) {};
    \node[draw,circle,inner sep=2pt,fill] at (2,1) {};
    \node[draw,circle,inner sep=2pt,fill] at (1,2) {};
    \foreach \X in {1,2}
    {\draw[thin] (\X,0.1) -- ++(0,-0.2) node[below]{\X};
    }
    \draw[thin] (0.1,0.5) -- ++(-0.2,0) node[left]{0.5};
    \draw[clip] (-1,-1) -- (1,2) -- (2,1) -- cycle; %This is the convex hull
    \filldraw[fill=gray, fill opacity=0.3, draw=black] (0.5,0.5) rectangle (3,3);
\end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

相关内容