[Tex/LaTex] Filling an area between a parabola and a shape

tikz-pgf

I looked at similar situations and I can't even understand how pgfplots would help me since I need to fill an area between two lines and one parabola. Here is my code:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}

\begin{tikzpicture}

\coordinate (A1) at (0,0);
\coordinate (A2) at (1,3);
\coordinate (A3) at (3,2);

\draw[very thin,color=gray] (0,0) grid (3.2,3.2);    
\draw[->,name path=xaxis] (-0.1,0) -- (3.2,0) node[right] {$x$};
\draw[->,name path=yaxis] (0,-0.1) -- (0,3.2) node[above] {$y$};
\foreach \x in {0,...,3}
        \draw (\x,1pt) -- (\x,-3pt)
        node[anchor=north] {\x};
\foreach \y in {0,...,3}
        \draw (1pt,\y) -- (-3pt,\y) 
            node[anchor=east] {\y};

\draw[name path=plot,domain=0:3.2] plot (\x,{\x^2/3}) node[right] {$y=\frac{x^2}{3}$}; 

coordinate [name intersections={of= A2--A3 and plot,by=intersect-1}];
coordinate [name intersections={of= A1--A3 and plot,by=intersect-2}];

\filldraw[thick,fill=blue,fill opacity=0.4] (A1) -- (A2) -- (A3) -- cycle;

\end{tikzpicture}
\end{document}

Thank you for your help, I know it's a simple question but to be honest it's my first graph in TeX overall.

Best Answer

You can do this using the \clip command in TikZ. To quote from the TikZ/PGF manual (section 2.11, page 35):

Clipping is pretty easy in TikZ. You can use the \clip command to clip all subsequent drawing. It works like \draw, only it does not draw anything, but uses the given path to clip everything subsequently.

So I’ve added two clips to your plot:

\clip (A1) -- (A2) -- (A3) -- (3,0) -- cycle;
\clip [domain=0:3.2] plot (\x, \x^2/3) -- (A2) -- (A1);

Edit: Now that I know what you wanted to clip, this is still fairly easy. For the first command, we drop the -- (3,0), or we’d clip too much. For the second command, the -- (A2) -- (A1) clips above the parabola. If we replace this by -- (3.2,0) -- (0,0), then we clip under the line. See updated screenshot and code below.

To make it clear what these two lines of code are clipping, here’s an extra screenshot:

enter image description here

The first line of code clips the area within the red dashed border. The second is the area within the blue dotted border.

The first clips the area between the two lines. The second clips the area above the parabola, and extends to go out to cover the area between the lines. If I didn’t add the extra points, then it would clip a straight line to the origin, which misses out part of the area you wanted to shade.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}

\begin{tikzpicture}

\coordinate (A1) at (0,0);
\coordinate (A2) at (1,3);
\coordinate (A3) at (3,2);

\draw[very thin,color=gray] (0,0) grid (3.2,3.2);    
\draw[->,name path=xaxis] (-0.1,0) -- (3.2,0) node[right] {$x$};
\draw[->,name path=yaxis] (0,-0.1) -- (0,3.2) node[above] {$y$};
\foreach \x in {0,...,3}
        \draw (\x,1pt) -- (\x,-3pt)
        node[anchor=north] {\x};
\foreach \y in {0,...,3}
        \draw (1pt,\y) -- (-3pt,\y) 
            node[anchor=east] {\y};

\draw[name path=plot,domain=0:3.2] plot (\x,{\x^2/3}) node[right] {$y=\frac{x^2}{3}$};

\draw (A1) -- (A2);
\draw (A2) -- (A3);
\draw (A1) -- (A3);

\clip (A1) -- (A2) -- (A3) -- cycle;
\clip [domain=0:3.2] plot (\x, \x^2/3) -- (3.2,0) -- (0,0);

\fill [blue, opacity=0.4] (0,0) rectangle (3,3);

\end{tikzpicture}
\end{document}

and it looks like this:

enter image description here