# Color the faces of a graph

I want to modify the image (see code below)

by shading in the regions formed by the dashed lines that contain a black dot. The goal is to obtain something like

How can I shade these regions?

Code for image:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\begin{document}
\tikzstyle{open}   = [draw, black, fill=white, shape=circle]
\tikzstyle{closed} = [draw,        fill,       shape=circle]
\tikzstyle{invisibleVertex} = [shape=circle]
\tikzstyle{invisibleEdge} = [draw opacity=0]
\begin{tikzpicture}[scale=1,transform shape,node distance=2.5cm,>=open triangle 60,semithick]
\node[closed] (0)              {};
\node[closed] (1) [right of=0] {};
\node[closed] (2) [below of=1] {};
\node[closed] (3) [left  of=2] {};
\path (0) edge node[open] (m0) {} (1)
(1) edge node[open] (m1) {} (2)
(2) edge node[open] (m2) {} (3)
(3) edge node[open] (m3) {} (0);
\path (m0) edge[->, densely dashed]                                                                        (m1)
edge[<-, densely dashed, out=  45, in=  45, looseness=3, overlay] node[invisibleVertex] (e0) {} (m1)
(m1) edge[->, densely dashed]                                                                        (m2)
edge[<-, densely dashed, out= -45, in= -45, looseness=3, overlay] node[invisibleVertex] (e1) {} (m2)
(m2) edge[->, densely dashed]                                                                        (m3)
edge[<-, densely dashed, out=-135, in=-135, looseness=3, overlay] node[invisibleVertex] (e2) {} (m3)
(m3) edge[->, densely dashed]                                                                        (m0)
edge[<-, densely dashed, out= 135, in= 135, looseness=3, overlay] node[invisibleVertex] (e3) {} (m0);
\node[invisibleVertex, below=0cm of e0] {};
\node[invisibleVertex, left =0cm of e1] {};
\node[invisibleVertex, above=0cm of e2] {};
\node[invisibleVertex, right=0cm of e3] {};
\end{tikzpicture}
\end{document}

-
–  Qrrbrbirlbel Mar 25 '13 at 2:50
It also looks a little like Tikz: joining points on a circle –  cmhughes Mar 25 '13 at 2:59
@TysonWilliams: Sorry if this comes across as harsh, not intended to be, but this site should be seen not just as a way for the person posting the question to accomplish what they need (although that is an important aspect of it), but also as a reference site for someone who comes across a need for a similar type of figure (or issue) and wants to determine out how to accomplish some task. They may be using tikz, or they may not be. –  Peter Grill Mar 25 '13 at 21:52
@TysonWilliams I completely agree with Peter Grill. Let's say that things are not just black and white, especially when it's your own question and the answers people wrote up for you. Maybe they don't work for you, and in that case you are more than welcome not to accept them, and even not to upvote them, but downvoting means there is something inherently wrong with the answer, and just being different from what you expected probably doesn't meet that requirement (at least based on TeX-SX habits and expectations). –  ienissei Mar 25 '13 at 22:38
@TysonWilliams Would it be rude to suggest you take a look at the users to vote the most and compare their downvoting rates with yours. My apologies if I somehow said it in a rasher way than the others, but there is a whole Meta thread on this question, where Joseph Wright, one of our moderators and high-rep users states: "Downvoting is in any case a last resort: we've been pretty clear that voting should be a primarily positive thing." (Continued) –  ienissei Mar 26 '13 at 8:48

It's always complex to do a lot of things at the same time. I searched a trick because there is little bug if I use open triangle 60. The path is not exactly the same. Look at this picture to see the problem (same code as below but with open triangle 60)

Perhaps it's possible to use a simpler way. The problem with edge is we don't get a closed path .

You can compare

\fill (0,0) edge [bend right] (5,0) edge (0,0);


with

\fill (0,0) to [bend right] (5,0) to (0,0);


It's possible to use foreach to write a shorter code.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,backgrounds}
\begin{document}
\tikzstyle{open}   = [draw, black, fill=white, shape=circle]
\tikzstyle{closed} = [draw,        fill,       shape=circle]
\tikzstyle{invisibleVertex} = [shape=circle]
\tikzstyle{invisibleEdge} = [draw opacity=0]
\begin{tikzpicture}[scale=1,node distance=2.5cm,>=triangle 60,semithick]
\node[closed] (0)              {};
\node[closed] (1) [right of=0] {};
\node[closed] (2) [below of=1] {};
\node[closed] (3) [left  of=2] {};
\path (0) edge node[open] (m0) {} (1)
(1) edge node[open] (m1) {} (2)
(2) edge node[open] (m2) {} (3)
(3) edge node[open] (m3) {} (0);

\begin{scope}[on background layer]
\fill[fill=green!40]  (m1.45)   to[ out=  45, in=  45,looseness=3]  (m0.45) to (m0.-45) to  (m1.-45) to  (m1.45);
\fill[fill=green!40]  (m2.-45)  to[ out=  -45, in= -45,looseness=3] (m1.-45) to (m1.45) to  (m2.45) to  (m2.-45);
\fill[fill=green!40]  (m3.-135) to[ out= -135, in= -135,looseness=3] (m2.-135) to (m2.135) to  (m3.135) to  (m3.-135);
\fill[fill=green!40]  (m0.-225) to[ out= -225, in= -225,looseness=3] (m3.-225) to (m3.225) to (m0.225) to  (m0.-225);

\end{scope}

\draw[->,densely dashed]  (m0) to   (m1);
\draw[densely dashed,->]  (m1.45)   to[ out=  45, in=  45, looseness=3] node[invisibleVertex] (e0) {} (m0.45) ;

\draw[->,densely dashed]  (m1) to   (m2);
\draw[densely dashed,->]  (m2.-45)   to[out=  -45, in=  -45, looseness=3 ] node[invisibleVertex] (e1) {} (m1.-45) ;

\draw[->,densely dashed]  (m2) to   (m3);
\draw[densely dashed,->]  (m3.-135)   to[ out=  -135, in=  -135, looseness=3] node[invisibleVertex] (e2) {} (m2.-135);
\draw[->,densely dashed]  (m3) to   (m0);
\draw[densely dashed,->]  (m0.-225)   to[ out=  -225, in=  -225, looseness=3] node[invisibleVertex] (e3) {} (m3.-225) ;

\node[invisibleVertex, below=0cm of e0] {};
\node[invisibleVertex, left =0cm of e1] {};
\node[invisibleVertex, above=0cm of e2] {};
\node[invisibleVertex, right=0cm of e3] {};

\end{tikzpicture}
\end{document}


Without open triangle it's possible to do :

\begin{scope}[on background layer]
\fill[fill=green!40]  (m1.45)   to[out=  45, in=  45,looseness=3]   (m0.45)   to (m0.-45) to  (m1.-45) to  (m1.45);
\fill[fill=green!40]  (m2.-45)  to[out=  -45, in= -45,looseness=3]  (m1.-45)  to (m1.45)  to  (m2.45)  to  (m2.-45);
\fill[fill=green!40]  (m3.-135) to[out= -135, in= -135,looseness=3] (m2.-135) to (m2.135) to  (m3.135) to  (m3.-135);
\fill[fill=green!40]  (m0.-225) to[out= -225, in= -225,looseness=3] (m3.-225) to (m3.225) to (m0.225)  to  (m0.-225);
\end{scope}

\path (m0) edge[->, densely dashed]                                                                        (m1)
edge[<-, densely dashed, out=  45, in=  45, looseness=3, overlay] node[invisibleVertex] (e0) {} (m1)
(m1) edge[->, densely dashed]                                                                        (m2)
edge[<-, densely dashed, out= -45, in= -45, looseness=3, overlay] node[invisibleVertex] (e1) {} (m2)
(m2) edge[->, densely dashed]                                                                        (m3)
edge[<-, densely dashed, out=-135, in=-135, looseness=3, overlay] node[invisibleVertex] (e2) {} (m3)
(m3) edge[->, densely dashed]                                                                        (m0)
edge[<-, densely dashed, out= 135, in= 135, looseness=3, overlay] node[invisibleVertex] (e3) {} (m0);


Update : I defined a new arrow because it' seems that the definition of open triangle' is wrong. I definednew open triangle 60.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,backgrounds}
\makeatletter
\pgfarrowsdeclare{new open triangle 60}{new open triangle 60}
{
\pgfutil@tempdima=0.5pt%
\pgfarrowsleftextend{+-\pgfutil@tempdimb}
\pgfarrowsrightextend{+\pgfutil@tempdimb}
}
{
\pgfutil@tempdima=0.5pt%
\pgfsetdash{}{+0pt}
\pgfsetmiterjoin
\pgfsetfillcolor{white}
\pgfpathlineto{\pgfqpoint{0.5\pgfutil@tempdima}{0\pgfutil@tempdima}}
\pgfpathclose
\pgfusepathqfillstroke
}
\makeatother

... etc
\begin{tikzpicture}[scale=1,node distance=2.5cm,>= new open triangle 60,semithick]
... etc.


Update and remark

Finally there are some adverse side effects with the actual solution. For example :

With triangle 60 and new open triangle 60 the tips are misplaced but the lines are correct. Finally with open triangle 60 the tips are well placed but the lines are incorrect.

I don't know if it's easy to modify the definition of the arrows to get correct positions for the tips and the lines.

A possibility like mwibrow wrote it's to use the decoration library. The next code is an adaptation.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds}
\usetikzlibrary{arrows,positioning,backgrounds,decorations.markings}
\begin{document}
\tikzset{background grid/.style={draw=red,step=.5cm},background rectangle/.style={fill=blue!20}}
\makeatletter
\pgfarrowsdeclare{new open triangle 60}{new open triangle 60}
{
\pgfutil@tempdima=0.5pt%
\pgfarrowsleftextend{+-\pgfutil@tempdimb}
\pgfarrowsrightextend{+\pgfutil@tempdimb}
}
{
\pgfutil@tempdima=0.5pt%
\pgfsetdash{}{+0pt}
\pgfsetmiterjoin
\pgfsetfillcolor{white}% fill the tip
\pgfpathlineto{\pgfqpoint{0.5\pgfutil@tempdima}{0\pgfutil@tempdima}}
\pgfpathclose
}
\makeatother

\begin{tikzpicture}  [  show background rectangle,show background grid]
\node [draw,circle](A){A};
\node [draw,circle](B) at (3,0){B};
\draw [<->,>=triangle 60,semithick] (A) to [ out=  90, in=  80](B);
\draw [<->,>=new open triangle 60,semithick] (A) to [ out=  -90, in=  -80](B);
\draw [orange,semithick] (A) to [ out=  90, in=  80](B);
\draw [orange,semithick] (A) to [ out=  -90, in=  -80](B);
\end{tikzpicture}
\begin{tikzpicture}  [  show background rectangle,show background grid]
\node [draw,circle](A){A};
\node [draw,circle](B) at (3,0){B};
\draw [<->,>=triangle 60,semithick] (A) to [ out=  90, in=  80](B);
\draw [<->,>=open triangle 60,semithick] (A) to [ out=  -90, in=  -80](B);
\draw [orange,semithick] (A) to [ out=  90, in=  80](B);
\draw [orange,semithick] (A) to [ out=  -90, in=  -80](B);
\end{tikzpicture}

\tikzstyle{open}   = [draw, black, fill=white, shape=circle]
\tikzstyle{closed} = [draw,        fill,       shape=circle]

\begin{tikzpicture}[scale=1,node distance=2.5cm,>=open triangle 60,semithick]

\node[closed] (1)  {};
\node[open] (m1) [below of=1] {};
\node[open] (m0) [left of=1] {};

\begin{scope}[on background layer]
\fill[fill=green!40]  (m1.45)   to[ out=  45, in=  45,looseness=3]  (m0.45) to (m0.-45) to  (m1.-45) to  (m1.45);
\end{scope}

\begin{scope}[decoration={
markings,%
mark=at position 0.999  with {\arrow[red]{new open triangle 60}}}]
\draw[->,densely dashed]  (m0) to   (m1);
\draw[densely dashed,postaction={decorate}]  (m1.45)   to[ out=  45, in=  45, looseness=3]  (m0.45) ;
\end{scope}

\end{tikzpicture}
\end{document}

-
I don't mind changing the arrow tips to triangle 60 to make this simpler. –  Tyson Williams Mar 25 '13 at 12:30
Why does using open triangle 60 instead of triangle 60 cause a small gap to appear between the shaded region and the dashed edge? –  Tyson Williams Mar 25 '13 at 15:01
@TysonWilliams I don't know : a bug or a problem in my code –  Alain Matthes Mar 25 '13 at 15:55
@TysonWilliams See my update : It's probably a bug in the definition of the arrow. –  Alain Matthes Mar 25 '13 at 16:36
PGF shortens lines according to the arrow tip code (see the manual). With open arrow tips this shortening is quite a lot so that the line does not show behind the arrow head. With curves I think this shortening takes place parallel to the nearest control point and end point of the curve (this isn't in the manual). The end point is moved but no adjustment is made to the nearest control point to maintain the curve (the maths is tricky). Decorations sort-of do the maths right (but at some computational cost). Alain Matthes' solution is not only nice, but the simplest solution in this case. –  Mark Wibrow Mar 26 '13 at 14:00

Just for fun with PSTricks.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-eucl}
\usepackage{multido}

\psset
{
arrowlength=2,
PointName=none,
PointSymbol=none,
arrowinset=0,
arrowlength=1,
linewidth=1pt,
hatchsep=0.5pt,
hatchwidth=0.5pt,
hatchcolor=lightgray,
dash=3pt 1pt,
}

\pstVerb
{
/H2S {2 sqrt 2 div} def
}

\def\Atom#1{%
\pstGeonode(!H2S neg 0){L}(!0 H2S){F}(0,1.5){T}(!H2S 0){R}
\pscustom[fillstyle=vlines,hatchangle=#1,linestyle=none]{\psellipticarc(R|T){(R)}{(L)}\closepath}
%
\psset{nodesep=5.5pt}
\pcline(L)(F)
\pcline(R)(F)
\psset{linestyle=dashed,arrows=->}
\psellipticarc[arcsep=11pt](0,0)(R|T){(R)}{(L)}
\pcline(L)(R)
%
\psset{linestyle=solid,fillstyle=solid,fillcolor=white}
\pscircle[fillcolor=black](F){5pt}
\pscircle(L){5pt}
\pscircle(R){5pt}
}
\begin{document}
\begin{pspicture}[showgrid=false](-1.5,-1.5)(1.5,1.5)
\multido{\i=45+90}{4}{\rput{!\i\space 90 sub}(!H2S \i\space PtoC){\Atom{45}}}
\end{pspicture}
\end{document}


## How is the diagram constructed?

\documentclass{beamer}
\usepackage{filecontents}
/H2S 2 sqrt 2 div def
\end{filecontents*}

\usepackage{pst-eucl}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=12pt
\usepackage{multido}

\psset
{
arrowlength=2,
PointName=none,
PointSymbol=none,
arrowinset=0,
arrowlength=1,
linewidth=1pt,
hatchsep=1pt,
hatchwidth=1pt,
hatchcolor=red,
dash=3pt 1pt,
}

\def\Atom{%
\pstGeonode(!H2S neg 0){L}(!0 H2S){F}(0,1.5){T}(!H2S 0){R}
\pscustom[fillstyle=solid,fillcolor=red,linestyle=none]{\psellipticarc(R|T){(R)}{(L)}\closepath}\pause
%
\psset{nodesep=5.5pt}
\pcline(L)(F)
\pcline(R)(F)\pause
\psset{linestyle=dashed,arrows=->}
\psellipticarc[arcsep=11pt](0,0)(R|T){(R)}{(L)}
\pcline(L)(R)\pause
%
\psset{linestyle=solid,fillstyle=solid,fillcolor=white}
\pscircle[fillcolor=black](F){5pt}
\pscircle(L){5pt}
\pscircle(R){5pt}
}

\def\Picture{%
\begin{pspicture}[showgrid=bottom](-2,-2)(2,2)
\multido{\i=45+90}{4}{\rput{!\i\space 90 sub}(!H2S \i\space PtoC){\Atom}\pause}
\end{pspicture}}

\begin{document}
\begin{frame}
\Picture
\end{frame}
\end{document}

-
You didn't use a single line of code from my example that I am interested in. –  Tyson Williams Mar 25 '13 at 12:29
@TysonWilliams: I think downvoting a different solution is really bad practise: both Karl's students and g-kov just pointed out you how to do the same thing in other manners, which ultimately are answers to your question. Thus I upvoted both. –  Claudio Fiandrino Mar 25 '13 at 12:37
@TysonWilliams Karl's students gives a fine PSTricks solution to your problem (and not a completely different answer as you say) and I can't see anywhere that you ask for a TikZ solution only. Furthermore, if you don't find his answer useful, then just leave it; don't downvote it. –  Svend Tveskæg Mar 25 '13 at 20:56
Note: In the animation part, there is an unnecessary frame at the last page of the PDF output. I will not remove it because I am lazy! –  kiss my armpit Mar 25 '13 at 21:34
@SvendMortensen I am not saying that Karl's student's solution isn't a fine solution. I am merely saying that I requested a solution that was a modification of what I started with and his answer isn't that. I don't know what you do with TikZ, but I use it in large documents so it is highly beneficial to minimize the number of dependencies. I am already using TikZ in many other ways in the document and neither Karl's students nor you have convinced me that I should convert from TikZ to PSTricks because of the issues I face with this particular image. –  Tyson Williams Mar 25 '13 at 21:51

Compare Asymptote version: asy -f pdf colored-graph.asy

size(300);
pen lineAb=black+3pt;
pen lineAt=white+1.2pt;
pen lineB=dashed+darkblue+1.3pt;
pen circA=lightyellow;
pen circB=darkblue;
pen rimA=red;
pen rimB=blue;
guide circ=unitcircle;
real d=5;
pair a,b,c,u;
a=(0,-d); b=(d,-d);
c=(d,0); u=1.618b;
guide ga=shift(a.x,a.y)*circ;
guide gc=shift(c.x,c.y)*circ;
guide garc=a{dir(-45)}..u..{dir(135)}c;
pair xa=intersectionpoint(ga,c--a);
pair xc=intersectionpoint(gc,a--c);
pair xab=intersectionpoint(ga,a--b);
pair xbc=intersectionpoint(gc,b--c);
pair xau=intersectionpoint(ga,garc);
pair xuc=intersectionpoint(gc,garc);

transform t=rotate(phi);
draw(t*(xab--b--xbc),lineAb);
draw(t*(xab--b--xbc),lineAt);
draw(t*(xc--xa),lineB,Arrow);
draw(t*(xau{dir(-45)}..u..{dir(135)}xuc),lineB,Arrow);
filldraw(t*shift(a.x,a.y)*circ,circA,rimA);
filldraw(t*shift(b.x,b.y)*circ,circB,rimB);
}

currentpen=orange+opacity(0.5);
base(); base(90,palegreen);
base(180,pink); base(-90,paleyellow);
filldraw(ga,circA,rimA);


-
You didn't use a single line of code from my example that I am interested in. –  Tyson Williams Mar 25 '13 at 12:29
@TysonWilliams An Asymptote solution is a valid graphics solution similar to other TikZ/PSTricks solutions. As you might have seen on the other questions (PSTricks to TikZ or vice versa), it's a great added value to have more than one type of workflow. –  percusse Mar 25 '13 at 12:33
You stated that you want to modify the image, not the code. The answer present a modified version of the image. Please next time beware of your wishes, they might come true. –  g.kov Mar 25 '13 at 12:42
@Tyson Williams: I might be wrong, but MWE is used as a reference point to start from, not for merely finding a missing commas or something, it shows you efforts to achieve the result. The answer however could be completely different. The answer to a question about shading regions on a graph might be useful for someone else, and different approaches just help to choose a suitable for the user. –  g.kov Mar 25 '13 at 14:33
@TysonWilliams You are free to choose the most helpful answer for you and mark it as accepted. The site is for helping all readers, not just you. –  egreg Mar 25 '13 at 23:11

Just to back up Alain Matthes' point about \foreach, here's another version. Although the nodes are not named (minus points here, but it could be automated), and the setting up of styles/parameters is quite lengthy, the actual drawing part is relatively short. Decorations were used instead of arrows, which is slight overkill, but is a bit more flexible if repositioning was required.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{decorations.markings}

\def\r{1cm}
\def\a{3pt}

\tikzset{
dot/.style={
insert path={
node [circle, draw, fill=#1, inner sep=2pt] {}
}
},
arrow edge/.style={
dashed,
draw,
postaction={
decoration={
markings,
mark=at position #1 with {
\draw [solid, black, fill=white, xshift=-\a]
(0:\a) -- (120:\a) -- (240:\a) -- cycle;
}
},
decorate
}
},
square/.style={
solid,
draw,
thick
}
}

\begin{document}

\begin{tikzpicture}

\foreach \i in {0, 90, 180, 270}{
\path [fill=gray!25, arrow edge=-\a]
(\i:\r) [rotate=\i-45] arc (0:180:\r/sqrt 2 and \r*sqrt 2);
\path [arrow edge=-\a]
(\i+90:\r) -- (\i:\r);
}

\path [square]
(-\r,-\r) rectangle (\r, \r);

\foreach \i in {0, 90, 180, 270}
\path
(\i:\r) [dot=white]
(\i+45:\r*sqrt 2) [dot=black];

\end{tikzpicture}
\end{document}


-
Actually, I usually prefer not to use \foreach. I think it makes the code unreadable. Can you implement your solution without using \foreach? –  Tyson Williams Mar 25 '13 at 12:34
Fine solution! Very interesting way. –  Alain Matthes Mar 25 '13 at 12:42
@TysonWilliams: Using \foreach is better solution because it applies the so-called DRY (Don't Repeat Yourself) programming paradigm to your problem while it still keeps the code readable and clean. –  kiss my armpit Mar 25 '13 at 12:43
@Karl'sstudents As I already said, I disagree. I don't think the \foreach functionality in TikZ very readable. Worse yet, I am not the only one that will use this code and the others that will know and understand TikZ considerably less than me. Of course I believe in the DRY paradigm. If were willing to specify these paths twice, I am sure that I could figure out how to first get the graph to look correct (which I have already done) and then repeat the paths just to be able to fill. However, I am quite unwilling to settle for this solution. –  Tyson Williams Mar 25 '13 at 14:43
@TysonWilliams: as someone who regularly has to plough through other people's unreadable code, I am sympathetic to your position. I also concede the approach given may not generalise very well. However, the point of the answer was specifically to show how \foreach` can reduce the amount of coding for this image. –  Mark Wibrow Mar 25 '13 at 16:07