4

I have two paths -- crucially, not closed; and I would like to shade just the bounded regions between them. Trying to close the paths using fill and clip, I've had trouble not creating new regions that are bounded.

My paths are just series of straight lines, so of course I could calculate the areas I need to fill exactly, but I want to iterate this shifting the two regions slightly. More precisely, my current code gives me this:

Shifting Partitions

Which is not what I want. I'll explain a bit what's going on.

One of my two paths is the outer boundary of the partition; the other path is the outer boundary shifted. So, the bottom right square, the outer boundary has shifted one square to the left, and the red squares are the squares in between these two lines. Moving to a drawing to the left or up, shifts the other boundary to the left or up, respectively.

So the picture on the bottom start being incorrect at the third in from the right -- the large colored red regions are not actually bounded between the two boundary of the partitions, but are rather an artifact of trying to close up the paths for filling and clipping. Here's what my two paths look like in that square:

Two lines

And here's what I want that square to look like:

Correct coloring

My code for this is not intelligent, and not my first try, but it's what I have at hand:

\begin{tikzpicture}[scale=.1]
\foreach \x in {1,2,...,7}
   \foreach \y in {0,1,...,5}
    {
   \begin{scope}[xshift=-10*\x cm, yshift=10*\y cm]
   \draw[clip] (0,5)--(2,5)--(2,4)--(3,4)--(3,2)--(4,2)--(4,1)--(7,1)--(7,0)--(0,0)--  cycle;
 \draw[fill=red] (0,5)--(2,5)--(2,4)--(3,4)--(3,2)--(4,2)--(4,1)--(7,1)--(7,0)--(0,0)--cycle;
\draw[fill=white] (-\x, \y + 20) |- ++(2,-15) |- ++(1,-1) |- ++(1,-2) |- ++(3,-1) |- ++(13,-1) -- ++(\x,-\y) -| ++(-13,1) -| ++(-3,1) -| ++(-1,2) -| ++(-1,1) -| ++(-2,15) -| ++(40,-40) -| (-\x, \y + 20);
\draw[very thin, gray] (0,0) grid (8,8);
\end{scope}
}
\end{tikzpicture}
  • 1
    Unrelated to the question, but you might check out the turtle library of TikZ which would make it much easier to create such paths. – Qrrbrbirlbel Jul 4 '13 at 23:17
  • Are the pictures you want to create always composed of little squared in white or red? I see a pattern here which would make it much easier to create on a different way and a different type of input by specifying the number of rectangle for the rows and for the “whitening” path. – Qrrbrbirlbel Jul 4 '13 at 23:25
  • @Qrrbrbirlbel Yeah -- it's always little collections of squares (partitions) that I'm doing; it probably would be easier in the long run to set up something where you entered the length of the partition. The main problem I was struggling with was how to isolate the bounded regions -- however you can capture that, I'd be happy. And I'm going to have investigate turtle just nostalgia's sake if nothing else. – Paul Johnson Jul 5 '13 at 8:15
  • Are you sure that your code gives you the wrong picture for the third in from the right on the bottom row? If I take the two paths that you've drawn and intersect them with the grid then I seem to get what you've drawn. What did you expect to get? – Andrew Stacey Jul 5 '13 at 9:23
  • 1
    The only idea that come to my mind is to have two closed regions which instead of being "infinite", they are simply large enough. And a third region (the grid) which will be the one finally drawn. Then, for each square in the grid, you perform a "flood fill" algorightm using as bound the two first regions, and check if the flooding escaped the grid. If so, you keep white those squares. If not, you make them red. Programming all this logic in tikz seems daunting. I would go for a external tool such as python or lua to make the computations and generate the tikz code. – JLDiaz Jul 5 '13 at 10:02
4

How does this match up to what you expect to see? I use the intersections library to find where the two paths actually intersect (note: as they frequently overlap, this method would not work well if the paths had curved segments since the algorithm for curved segments would report a lot of intersections; the algorithm for lines is simpler and so on overlaps only reports the end points of the overlapping segment). We then take the first and last of these and as the path is a step curve, all regions that are actually between the paths are in the rectangle that contains these two intersections. So we clip against that rectangle, and then clip against the curves, and then fill the region.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/122516/86}
\usepackage{tikz}

\usetikzlibrary{intersections}

\newcommand\bdrypath{
 (0,5) --(2,5)--(2,4)--(3,4)--(3,2)--(4,2)--(4,1)--(7,1)--(7,0)
}
\newcommand\ebdrypath{
(0,10) -- \bdrypath -- (14,0)
}

\newcommand\cbdrypath[1]{
\ebdrypath  #1 (0,10)
}

\begin{document}

\begin{tikzpicture}[scale=.1]
\foreach \x in {1,2,...,7}
   \foreach \y in {0,1,...,5}
    {
      \begin{scope}[xshift=-10*\x cm, yshift=10*\y cm]
      \path[name path=bdry] \ebdrypath;
      \path[name path=sbdry,xshift=-\x cm, yshift=\y cm] \ebdrypath;
      \path[name intersections={of=bdry and sbdry, sort by=bdry, total=\n}] (intersection-1) coordinate (start) (intersection-\n) coordinate (end);

      \begin{scope}
      \clip (start) rectangle (end);
      \clip \cbdrypath{-|};
      \clip[xshift=-\x cm, yshift=\y cm] \cbdrypath{|-};
      \fill[red] (0,0) rectangle (7,5);
      \end{scope}
      \begin{scope}
      \clip \cbdrypath{-|};
      \draw[very thin, gray] (0,0) grid (7,5);
      \end{scope}
      \draw \bdrypath -- (0,0) -- cycle;
      \end{scope}
    }
\end{tikzpicture}
\end{document}

bounded region between curves

  • Perfect! I was wondering if something like that would work, but hadn't played with 'intersections', and was unsure of what would happen with overlapping paths, etc. and hadn't tried it yet. I'll try to digest your code later today and ask if I understand it. BTW, a bit on the math of this figure (which isn't too complicated): you'll notice the partition has 19 squares, and this process creates exactly 19 bounded regions; this isn't a coincidence. You could also shift the boundary down and to the right, and get 19 more regions. This is related to the hilbert scheme of points in the plane. – Paul Johnson Jul 5 '13 at 10:54
  • Sounds scarily like algebraic geometry to me! – Andrew Stacey Jul 5 '13 at 11:18

Your Answer

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

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