# Tikz Fill the sub-area of a convex hull without knowing the coordinates of border

Hello my fellow friends on the internet,

I want to shade a partial surface of a convex hull. The shaded area shall be limited by the coordinate (0.5,0.5) and the part of the convex hull where both the x and y coordinates are greater than or equal to 0.5. What I did was to guess the two values x and y in which the (0.5,y) and (x,0.5) lines intersect with the convex hull. However, I wonder if there's not a simpler solution, so that I can shade the area without knowing the (1.25,0.5) and (0.5,1.25) coordinates (so that tikz has to find the coordinates itself).

I would also like to mark the axes at some specific positions (see picture). Thanks a lot in advance!

\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}

Like this:

\documentclass[tikz=true]{standalone} %
\usepackage{pgfplots}
\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}

Using Metapost and the magic of declarative programming....

Compile with lualatex or work out how to adapt it for plain 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}

You can simply clip against the convex hull. EDIT: Added annotations, without Jasper Habicht's kind reminder I would not have noticed that I forgot to put them.

\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}

• If someone else wants to answer this I'll be happy to remove my answer.... – user121799 Aug 1 '18 at 21:38
• Adding ticks can be achieved, for example, by adding \draw [thin, gray] ([xshift=-2]0,0.5) -- ([xshift=2]0,0.5) node[anchor=east, font=\scriptsize, outer sep=5pt] {0.5}; and \draw [thin, gray] ([yshift=-2]1,0) -- ([yshift=2]1,0) node[anchor=north, font=\scriptsize, outer sep=5pt] {1}; etc. somewhere in the code. – Jasper Habicht Aug 1 '18 at 21:45