# Filling an area between a parabola and a shape

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.

-

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.

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

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:

-
Thank you for a clear answer! However as I commented under the next reply, what I failed to express is I needed to fill the tiniest triangle (from 2 to 3), I think it makes this a bit harder. –  Welmar T. Mar 21 at 22:45
@WelmarT. I’ve edited to reflect the changes. Hope it makes sense. –  alexwlchan Mar 21 at 22:53
It does, thanks! –  Welmar T. Mar 21 at 22:59

It is too short for typing exercise with PSTricks. I don't know which one you want exactly so I provide you with 2 cases as follows.

## Case 1

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-eucl,pst-plot}
\def\f{(x^2/3)}
\def\g{(2*x/3)}
\def\h{((-x+7)/2)}

\pstVerb{/I2P {AlgParser cvx exec} def}

\begin{document}
\begin{pspicture}[saveNodeCoors,algebraic,linejoin=2](-1,-1)(4,4)
\psaxes{->}(0,0)(-1,-1)(3.5,3.5)[$x$,0][$y$,90]
\pstInterFF[PointName=none,PointSymbol=none]{\f I2P}{\h I2P}{2}{A}
\pscustom*[linecolor=blue!50]
{
\psplot{0}{3}{\g}
\psplot{3}{N-A.x}{\h}
\psplot{N-A.x}{0}{\f}
\closepath
}
\psplot{0}{3.2}{\f}
\pspolygon(*3 {\g})(*1 {\h})
\uput[u](*3.2 {\f}){\scriptsize $f(x)=\frac{x^2}{3}$}
\end{pspicture}
\end{document}


## Case 2

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-eucl,pst-plot}
\def\f{(x^2/3)}
\def\g{(2*x/3)}
\def\h{((-x+7)/2)}

\pstVerb{/I2P {AlgParser cvx exec} def}

\begin{document}
\begin{pspicture}[saveNodeCoors,algebraic,linejoin=2,PointName=none,PointSymbol=none](-1,-1)(4,4)
\psaxes{->}(0,0)(-1,-1)(3.5,3.5)[$x$,0][$y$,90]
\pstInterFF{\f I2P}{\g I2P}{2}{A}
\pstInterFF{\f I2P}{\h I2P}{2}{B}
\pscustom*[linecolor=blue!50]
{
\psplot{N-A.x}{3}{\g}
\psplot{3}{N-B.x}{\h}
\psplot{N-B.x}{N-A.x}{\f}
\closepath
}
\psplot{0}{3.2}{\f}
\pspolygon(*3 {\g})(*1 {\h})
\uput[u](*3.2 {\f}){\scriptsize $f(x)=\frac{x^2}{3}$}
\end{pspicture}
\end{document}


## Notes:

The latest pst-eucl can accept infix notation so I2P operator is no longer needed.

-
Thanks! However, what I failed to express is I needed to fill the tiniest triangle (from 2 to 3), I think it makes this a bit harder. –  Welmar T. Mar 21 at 22:42

Here is a PSTricks solution:

\documentclass{article}

\begin{document}

\begin{pspicture}[algebraic](-0.35,-0.4)(4.4,3.7)
\psaxes[tickcolor = black!20, xticksize = 0 3, yticksize = 0 3]{->}(0,0)(3.3,3.3)[$x$,0][$y$,90]
\pscustom*[linecolor = blue!50]{
\psline(3,2)(!177 sqrt 3 sub 4 div 31 177 sqrt sub 8 div)
\psplot{2}{2.576}{x^2/3} % plot from 2 to (sqrt(177)-3)/4
\psline(!2 4 3 div)(3,2)
\closepath
}
\psplot{0}{3.2}{x^2/3}
\uput[0](!3.2 256 75 div){$y = \frac{x^{2}}{3}$}
\pspolygon(0,0)(1,3)(3,2)
\end{pspicture}

\end{document}


-

A MetaPost solution. The wanted area is found and filled using the handy buildcycle macro.

input mpcolornames;
input latexmp;
setupLaTeXMP(options="12pt", packages = "amsmath", textextlabel = enable, mode = rerun);

u := 1.5cm; xmin := -.5; xmax := 3.5; ymin := -.5; ymax := 3.5; len := 3bp;
vardef f(expr x) = (x**2)/3 enddef;
path p; p = origin -- (3, 2) -- (1, 3) -- cycle;
path curve; curve = origin
for i = 0.01 step 0.01 until 3:
-- (i, f(i))
endfor;

beginfig(1);
labeloffset := 6bp;
for i = 1 upto 3:
draw u*(i, 0) -- u*(i, ymax) withcolor LightGray;
draw u*(0, i) -- u*(xmax, i) withcolor LightGray;
draw (i*u, -len) -- (i*u, len); label.bot("$" & decimal i & "$", (i*u, 0));
draw (-len, i*u) -- (len, i*u); label.lft("$" & decimal i & "$", (0, i*u));
endfor;
labeloffset := 3bp;
label.llft("$O$", origin); label.bot("$x$", (xmax*u, 0)); label.lft("$y$", (0, ymax*u));
fill buildcycle(subpath(1, 2) of p, reverse curve, subpath (epsilon, 3) of p)
scaled u withcolor SlateBlue1;
draw p scaled u;
draw curve scaled u;
picture curvelabel;
curvelabel = thelabel.rt("$y=\dfrac{x^2}{3}$", u*point infinity of curve);
unfill bbox curvelabel; draw curvelabel;
drawarrow (xmin*u, 0) -- (xmax*u, 0);
drawarrow (0, ymin*u) -- (0, ymax*u);
endfig;
end.


-