# How to fill an area below sine wave?

I would like to draw a alpha- and beta-error diagram like However, I do not get the areas near the cut-off coloured...

What I have done yet:

\begin{tikzpicture}
% Achsen zeichnen
\draw[->,thick] (0,0) -- (10,0) node[below] {$x$};
\draw[->,thick] (0,0) -- (0,3.5) node[left] {$y$};
% Achsen beschriften
\draw (1,-.2) -- (1,0) node[below=4pt] {$\scriptstyle1$};
\draw (3.141,-.2) -- (3.141,0) node[below=4pt] {$\pi$};
\draw (6.283,-.2) -- (6.283,0) node[below=4pt] {$2\pi$};
\foreach \y in {0,1,2}
\draw (-.1,\y) -- (.1,\y) node[left=4pt] {$\scriptstyle\y$};

% Cut-off
\draw[-,thick] (5,0) -- (5,2.5) node[above] {cut off};

% solvent
\draw plot[domain=-.5*pi:1.5*pi] (\x+0.5*pi,{sin(\x r)+1});

% insolvent
\draw plot[domain=-.5*pi:1.5*pi] (\x+1.7*pi,{sin(\x r)+1});

\draw[gray,->] (0,0) -- (pi,0);
\end{tikzpicture}


## 2 Answers

Here is a solution. It uses the intersections library, Because of this I drew the sine curves with the builtin sin and cos paths; the intersections library does not work well with function plots.

The intersections library computes the intersection point of the two curves; you don't have to fiddle around trying to figure it out yourself.

The code is not elegant, the paths are repeated quite often, but it gets the job done.

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}
% Achsen zeichnen
\draw[->,thick] (0,0) -- (10,0) node[below] {$x$};
\draw[->,thick] (0,0) -- (0,3.5) node[left] {$y$};
% Achsen beschriften
\foreach \x/\xlabel in {1/1,3.141/\pi,6.283/{2\pi}}
\draw (\x,-.2) -- ++(0,0.2) node[below=4pt] {$\scriptstyle\xlabel$};
\foreach \y in {0,1,2}
\draw (-.1,\y) -- ++(0.1,0) node[left=4pt] {$\scriptstyle\y$};

% solvent
\path[name path = solvent] (0,0) cos (0.5*pi,1) sin (pi,2) cos (1.5*pi,1) sin (2*pi,0);

%unsolvent
\path[shift={(1.2*pi,0)},name path= unsolvent] (0,0) cos (0.5*pi,1) sin (pi,2) cos (1.5*pi,1) sin (2*pi,0);

\begin{scope}
\clip[name intersections={of=solvent and unsolvent}] (intersection-1 |- {(0,0)}) rectangle (2*pi,2);
\fill[blue] (0,0) cos (0.5*pi,1) sin (pi,2) cos (1.5*pi,1) sin (2*pi,0);
\end{scope}

\begin{scope}
\clip[name intersections={of=solvent and unsolvent}] (0,0) rectangle (intersection-1 |- {(0,2)});
\fill[shift={(1.2*pi,0)},red] (0,0) cos (0.5*pi,1) sin (pi,2) cos (1.5*pi,1) sin (2*pi,0);
\end{scope}

% solvent
\draw[blue] (0,0) cos (0.5*pi,1) sin (pi,2) cos (1.5*pi,1) sin (2*pi,0);

%unsolvent
\draw[shift={(1.2*pi,0)},red] (0,0) cos (0.5*pi,1) sin (pi,2) cos (1.5*pi,1) sin (2*pi,0);

%cutoff
\draw [thick,green, name intersections={of=solvent and unsolvent}]
(intersection-1 |- {(0,0)}) -- ++(0,2) node[above] {cut off};

\end{tikzpicture}

\end{document}


The result is: A MetaPost version, included in a LuaLaTeX program. I've used MetaPost's built-in facilities for finding paths intersections and the areas between them, most notably the buildcycle macro which determines the cyclic path around this area. See the MetaPost manual, p. 30.

\documentclass[12pt, border = 1bp]{standalone}
\usepackage{unicode-math}
\usepackage{luamplib}
\mplibtextextlabel{enable}
\mplibsetformat{metafun}
\begin{document}
\begin{mplibcode}

input mpcolornames;

% Drawing a function curve
vardef function(expr xmin, xmax, xstep)(text f_x) =
save x; x := xmin;
(x, f_x)
forever:
hide(x := x + xstep)
exitunless x <= xmax;
.. (x, f_x)
endfor
if x - xstep < xmax: hide(x := xmax) .. (x, f_x) fi
enddef;

beginfig(1);

% Scale and axes: definitions
numeric u, v, xmin, xmax, ymin, ymax;
u = cm; v = 2.5cm; xmin = 0; xmax = 3pi+1; ymin = 0; ymax = 2.25;
path xaxis, yaxis;
xaxis = origin -- (xmax*u, 0);
yaxis = origin -- (0, ymax*v);

% Axes, sin waves, areas: definitions
numeric t[], xintersect; pair intersect;
path wave[], midstraight;
wave1 = (function(-.5pi, 1.5pi, .05)(1 + sin x)) xyscaled (u, v) xshifted .5pi*u;
wave2 = wave1 xshifted pi*u;
intersect = wave1 intersectionpoint wave2;
xintersect = xpart intersect;
midstraight = (xintersect, -.5v) -- intersect;

% Drawing sin waves, areas below and relevant labels
fill buildcycle(xaxis, midstraight, wave1) withcolor .8white;
fill buildcycle(midstraight, reverse wave2, xaxis) withcolor LightSteelBlue;
draw wave1; draw wave2; draw midstraight;
dotlabel.top("$\mu_0$", point 0.5 along wave1);
label.rt("$\alpha$", point .3 along (wave1 cutbefore wave2));
dotlabel.top("$\mu_1$", point 0.5 along wave2);
label.lft("$\beta$", point 0.7 along (wave2 cutafter wave1));

% Axes drawings and relevant labels
drawarrow xaxis; drawarrow yaxis;
label.bot("$x$", (xmax*u, 0));
label.lft("$p(x)$", (0, ymax*v));
label.bot("Annahme", point 0 of wave2);
label.bot("Ablehnung", point infinity of wave1);
label.bot("$+k_\alpha$", point 0 of midstraight);

% Comments
path comment_line;
comment_line = ((xintersect - 1.25pi*u, 0)-- (xintersect + 1.25pi*u, 0))
yshifted -.3v;
drawdblarrow comment_line;
labeloffset := .5v;
label.bot(btex
\begin{tabular}{c}
$\beta$-Fehler kleiner,\\ $\alpha$-Fehler gr\"o\ss er
\end{tabular} etex, point .25 of comment_line);
label.bot(btex
\begin{tabular}{c} $\alpha$-Fehler kleiner,\\ $\beta$-Fehler gr\"o\ss er
\end{tabular} etex, point .75 of comment_line);
endfig;

\end{mplibcode}
\end{document}


Output: 