Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to re-create a diagram from a textbook, which has various regions defined by polynomials. Ideally I'd like to fill them with the correct colours, to match the text that goes with it.

While I can create and fill the five 'shapes', I'm so far unable to fill the remainder of the background, without also re-filling the other regions (with the exception of the central circle).

Is there an (easy) way to do this? Or do I need to re-think the diagram entirely?

\documentclass[tikz,border=12pt,svgnames]{standalone}

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,x=0.5cm,y=0.5cm,thin,samples=100,fill opacity=0.5]
\draw[help lines,step=0.5cm] (0,0) grid (14,8);
\draw[fill=red,domain=0:5] plot(\x,{-(1/3)*(\x+1)*(\x-5)}) |- (5,0) -- (0,0) -- cycle;
\draw[fill=Green,domain=10:14] plot(\x,{((\x-12)^3)/4+2}) |- (14,0) -- (10,0) -- cycle;
\draw[fill=Blue,domain=0:4] plot(\x,{((\x-2)^3)/4+6}) |- (4,8) -- (0,8) -- cycle;
\draw[fill=black,domain=9:14] plot(\x,{(\x-9)*(\x-15)/3+8}) |- (14,8) -- (9,8) -- cycle;
\draw[fill=Green,domain=9:14] (7,4) circle (3);
\draw[fill=pink,even odd rule,domain=9:14] (0,0) rectangle (14,8) (7,4) circle (3);
\end{tikzpicture}
\end{document}

enter image description here

share|improve this question
    
Please add your output obtained from your code. –  ferahfeza May 9 at 15:06

2 Answers 2

up vote 7 down vote accepted

You can build the plot/background layers as opaque, then make them transparent as a group over the gridlines.

Result

results2

Code

\documentclass[tikz,border=12pt,svgnames]{article}

\usepackage{tikz}
\newsavebox{\myimagebox}

\begin{document}


% Save top layer in a box
% Composed as opaque layer, then made transparent as a group
\savebox{\myimagebox}{%
\begin{tikzpicture}[line cap=round,line join=round,x=0.5cm,y=0.5cm,thin,samples=100,transparency group,opacity=.5]
\draw[fill=pink,even odd rule,domain=9:14] (0,0) rectangle (14,8) (7,4) circle (3);
\draw[fill=red,domain=0:5] plot(\x,{-(1/3)*(\x+1)*(\x-5)}) |- (5,0) -- (0,0) -- cycle;
\draw[fill=Green,domain=10:14] plot(\x,{((\x-12)^3)/4+2}) |- (14,0) -- (10,0) -- cycle;
\draw[fill=Blue,domain=0:4] plot(\x,{((\x-2)^3)/4+6}) |- (4,8) -- (0,8) -- cycle;
\draw[fill=black,domain=9:14] plot(\x,{(\x-9)*(\x-15)/3+8}) |- (14,8) -- (9,8) -- cycle;
\draw[fill=Green,domain=9:14] (7,4) circle (3);
\end{tikzpicture}%
}

% Layer top over the grid lines
\begin{tikzpicture}[line cap=round,line join=round,x=0.5cm,y=0.5cm,thin,samples=100]
\draw[help lines,step=0.5cm] (0,0) grid (14,8);
\node[inner sep=0pt] (s) at (7,4){\usebox\myimagebox};
% Repeat these to highlight the black outlines again.
\draw[even odd rule,domain=9:14] (0,0) rectangle (14,8) (7,4) circle (3);
\draw[domain=0:5] plot(\x,{-(1/3)*(\x+1)*(\x-5)}) |- (5,0) -- (0,0) -- cycle;
\draw[domain=10:14] plot(\x,{((\x-12)^3)/4+2}) |- (14,0) -- (10,0) -- cycle;
\draw[domain=0:4] plot(\x,{((\x-2)^3)/4+6}) |- (4,8) -- (0,8) -- cycle;
\draw[domain=9:14] plot(\x,{(\x-9)*(\x-15)/3+8}) |- (14,8) -- (9,8) -- cycle;
\draw[domain=9:14] (7,4) circle (3);

\end{tikzpicture}

\end{document}
share|improve this answer
    
That is certainly a solution, however I feel there should be a way to construct the diagram without having to worry about layers. Also, removing the opacity/having the gridlines on top is a change in style compared to the rest of the (rather large!) document - I'd rather not have to change the hundreds of diagrams that I've already produced. –  JustinT May 9 at 17:14
    
@JustinT I have adjusted my answer... –  cslstr May 9 at 18:04

Here is another method. We fill outside the curves and then inverse clip the central circle.

\documentclass[tikz,border=12pt,svgnames]{standalone}
\tikzset{
  invclip/.style={
    clip,
    insert path={{[reset cm] (-16000pt,-16000pt) rectangle (16000pt,16000pt)}}},
  saveuse path/.code 2 args={
    \pgfkeysalso{#1/.estyle={insert path={#2}}}%
    \global\expandafter\let\csname pgfk@\pgfkeyscurrentpath/.@cmd\expandafter\endcsname 
                           \csname pgfk@\pgfkeyscurrentpath/.@cmd\endcsname
    \pgfkeysalso{#1}}}
\def\invclip#1;{\pgfinterruptboundingbox\path[invclip]#1;\endpgfinterruptboundingbox}

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,x=0.5cm,y=0.5cm,thin,samples=100,fill opacity=0.5]
\draw[help lines,step=0.5cm] (0,0) grid (14,8);
\draw[fill=red,domain=0:5] plot(\x,{-(1/3)*(\x+1)*(\x-5)})  |- (5,0) -- (0,0) -- cycle;
\draw[fill=Green,domain=10:14]  plot(\x,{((\x-12)^3)/4+2})  |- (14,0) -- (10,0) -- cycle;
\draw[fill=Blue,domain=0:4] plot(\x,{((\x-2)^3)/4+6}) |- (4,8) -- (0,8) -- cycle;
\draw[fill=black,domain=9:14] plot(\x,{(\x-9)*(\x-15)/3+8}) |- (14,8) -- (9,8) -- cycle;

%% inverse clip the circle
\draw [fill=Green,saveuse path={circle}{(7,4) circle (3)}] ;
\invclip [circle];

%% fill the outer parts. Make all colors same. I made them different just for demo
\path[fill=blue!20,domain=0:4]    plot(\x,{((\x-2)^3)/4+6}) -| (9,4);
\path[fill=green!20,domain=10:14]  plot(\x,{((\x-12)^3)/4+2})  -| (5,0);
\path[fill=pink,domain=9:14]   plot(\x,{(\x-9)*(\x-15)/3+8}) |- (9,4);
\path[fill=orange,domain=0:5]   plot(\x,{-(1/3)*(\x+1)*(\x-5)}) |- (0,4);

\end{tikzpicture}
\end{document}

enter image description here

With same colors:

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.