2

In an answer to a previous question (Drawing curves with LaTeX), this curve was drawn

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[set mark/.style args={#1 at #2}{postaction={decorate,
decoration={markings,mark=at position #2 with #1}}}]
 \begin{scope}[xshift=10cm]
   \draw[set mark={{\node at (0,0.3) {1};}} at 0,set mark={\coordinate (1);} at 0.25,
  set mark={{\node at (0,0.3) {2};}} at 0.5,set mark={\coordinate (2);} at 0.75] plot[smooth cycle,tension=1.1] coordinates {(60:1) (-70:1) (-120:1) (110:1)};
  \draw (1) to[out=30,in=150] node[midway,below right] {3} (2);
 \end{scope}
\end{tikzpicture}
\end{document}

Is it possible to rewrite the code above in order to put colors in the regions of the space divided by the curve?

2
  • yes, it is possible. in general with option fill=<color> in your \draw` command. – Zarko Jan 25 '19 at 19:46
  • 1
    @marmot, i agere your statements in answer, that question should be well defined. according to your opinion, i misunderstood this question. – Zarko Jan 25 '19 at 20:16
4

Here is a solution based on fillbetween. In this case you could also work with clip, but in general this won't work.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings,backgrounds}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[set mark/.style args={#1 at #2}{postaction={decorate,
decoration={markings,mark=at position #2 with #1}}}]
 \begin{scope}[local bounding box=graph]
   \draw[set mark={{\node at (0,0.3) {1};}} at 0,set mark={\coordinate (1);} at 0.25,
  set mark={{\node at (0,0.3) {2};}} at 0.5,set mark={\coordinate (2);} at 0.75,
  name path global=contour]
   plot[smooth cycle,tension=1.1] coordinates {(60:1) (-70:1) (-120:1) (110:1)};
  \draw[name path global=divide] (1) to[out=30,in=150] node[midway,below right] {3} (2);
 \end{scope}
 \begin{scope}[on background layer]
  \fill[blue,intersection segments={of=contour and divide,
 sequence={L*}}];
  \fill[red,intersection segments={of=contour and divide,
 sequence={R2--L2[reverse]}}];
 \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

And as for your comment here, this is a way to attach arrows to the divide line.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings,backgrounds}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[set mark/.style args={#1 at #2}{postaction={decorate,
decoration={markings,mark=at position #2 with #1}}},
attach arrow/.style={set mark={{\draw[-latex] (0,0.3) -- (0,0);}} at #1}]
 \begin{scope}[local bounding box=graph]
   \draw[set mark={{\node at (0,0.3) {1};}} at 0,set mark={\coordinate (1);} at 0.25,
  set mark={{\node at (0,0.3) {2};}} at 0.5,set mark={\coordinate (2);} at 0.75,
  name path global=contour]
   plot[smooth cycle,tension=1.1] coordinates {(60:1) (-70:1) (-120:1) (110:1)};
  \draw[name path global=divide,attach arrow/.list={0.1,0.2,...,0.9}] (1) to[out=30,in=150] node[midway,below right] {3} (2);
 \end{scope}
 \begin{scope}[on background layer]
  \fill[blue,intersection segments={of=contour and divide,
 sequence={L*}}];
  \fill[red,intersection segments={of=contour and divide,
 sequence={R2--L2[reverse]}}];
 \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

Please note also that this is a public Q & A site. It lives from well-defined questions getting well-defined answers. If a question gets "blurred" by many additional requests, the answer won't be very useful for anyone but the one asking the question. On the other hand, if you ask a new well-defined question, then others may benefit. Askinq questions is free of charge.

2
  • That works very well, thanks. Is it possible to move the arrow so that the "base" is on the line (instead of the "tip") and to adjust the arrow's length? – user180244 Jan 25 '19 at 22:23
  • @Zyl Yes, of course. The arrow parameters are determined by the attach arrow style. If you use e.g. attach arrow/.style={set mark={{\draw[latex-] (0,0.2) -- (0,0);}} at #1}, the arrow will be shorter (since 0.3 has been replaced by 0.2) and the head will point away from the line (since latex- has been replaced by -latex). – user121799 Jan 25 '19 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy