9

I like maths and I have a young cousin who struggles quite a lot with it. So I am trying to find different fun activities for her to show how interesting maths can be.

During my Easter visit to her family, I showed her the Four Color Theorem (I gave her a random map to color in the way it says in the theorem) to show how it all works and bet her that she couldn't come up with one where you need more than 4 colors.

I am no longer visiting but am continuing to send her `homeworks' which I compile using LaTeX. I would like to revisit this fun activity but want to be able to draw random maps for her in Tikz, rather than find a picture online.

Something like this, enter image description here and preferably where I can make several random ones. They don't have to be particularly pretty, just `random'.

Thanks.

  • An easier solution would be to use random rectangles on a fixed grid. A fractal like method might also be possible. – John Kormylo May 7 '17 at 18:03
  • 1
    Isn't a bet you know she can't possibly win a bit mean? – cfr May 9 '17 at 0:00
  • A wonderful problem; makes me think about how history evolves borders. Unlike most diffusion equations we might use to model evolution of various shapes (heat equation, Ricci flow), borders usually retain sharp features throughout time. There are small wooden statues found (almost exclusively) on the borders of the counties of Ireland. They are neolithic, so we know that the county boundaries have been roughly intact since before humans could write. Not the only slow moving historical process in Ireland, but maybe the slowest. – Benjamin McKay May 9 '17 at 7:42
  • @cfr, we weren;t betting for money, it's ok. It was just a way of trying to encourage her to try it to convince her that it is true. Instead of her just accepting that what I am telling her is true. I might even show her a basic proof using graph theory (although I won't call it that). – JSharpee May 9 '17 at 8:54
  • This also shades the parts, which you don't want, but non-shaded could just be a simplified version: tex.stackexchange.com/questions/314918/…. The added complexity for your case would be the curving. – cfr May 9 '17 at 9:34
8

Original attempt

Here's an attempt, which can probably be upscaled to whichever size you want:

\documentclass[tikz,border=2mm]{standalone}
\pgfmathsetseed{\pdfuniformdeviate 10000000} %Credit: https://tex.stackexchange.com/a/212755/117534
\begin{document}    
    \begin{tikzpicture}
    \foreach \i in {0,...,4}
    \foreach \j in {0,...,4}
    \coordinate[] (n-\i\j) at (\i + 0.5*rand,\j + 0.5*rand) {};
    \foreach \i in {0,...,4}
    \foreach \j [count=\jj] in {0,...,3}
    \draw (n-\i\j) -- (n-\i\jj) (n-\j\i) -- (n-\jj\i);
    \end{tikzpicture}
\end{document}

A few outputs:

rand1 rand2 rand3 rand4

With Curves

It's a little less controlled now with the curved edges, though. I've added the code:

\pgfmathparse{random(0,60)-30}
\draw (n-\i\j) to [bend right=\pgfmathresult] (n-\i\jj) (n-\j\i) to [bend right=\pgfmathresult] (n-\jj\i);
}

instead, which generates a number from -30 to 30. i.e. it will bend left if the result is negative, right if positive. The full code and example:

\documentclass[tikz,border=2mm]{standalone}
\pgfmathsetseed{\pdfuniformdeviate 10000000} %Credit: https://tex.stackexchange.com/a/212755/117534
\begin{document}    
    \begin{tikzpicture}
    \foreach \i in {0,...,4}
    \foreach \j in {0,...,4}
    \coordinate[] (n-\i\j) at (\i + 0.5*rand,\j + 0.5*rand) {};
    \foreach \i in {0,...,4}
    \foreach \j [count=\jj] in {0,...,3}{%
    \pgfmathparse{random(0,60)-30}
    \draw (n-\i\j) to [bend right=\pgfmathresult] (n-\i\jj) (n-\j\i) to [bend right=\pgfmathresult] (n-\jj\i);
}
    \end{tikzpicture}
\end{document}

rand5

Rectangular grids

I added in a few comments, and declared two commands for you to enter how many rows/columns you want. The key difference here from the code above is that I've split up the drawing of horizontal and vertical lines, to accommodate for rectangular grids.

\documentclass[tikz,border=2mm]{standalone}
\pgfmathsetseed{\pdfuniformdeviate 10000000} %Credit: https://tex.stackexchange.com/a/212755/117534
\begin{document}    
    \begin{tikzpicture}
    \pgfmathtruncatemacro{\rownum}{6} % Set number of rows here
    \pgfmathtruncatemacro{\colnum}{3} % Set number of columns here
    \pgfmathtruncatemacro{\rowtemp}{\rownum-1}
    \pgfmathtruncatemacro{\coltemp}{\colnum-1}
    % Place coordinates in a grid like fashion; randomized
    \foreach \i in {0,...,\rownum}
    \foreach \j in {0,...,\colnum}
    \coordinate[] (n-\i\j) at (\i + 0.45*rand,\j + 0.45*rand) {};
    % Draw vertical lines
    \foreach \i in {0,...,\rownum}
    \foreach \j [count=\jj] in {0,...,\coltemp}{%
    \pgfmathparse{random(0,60)-30}
    \draw (n-\i\j) to [bend right=\pgfmathresult] (n-\i\jj);
}   
    % Draw horizontal lines
    \foreach \i [count=\ii] in {0,...,\rowtemp}
    \foreach \j in {0,...,\colnum}{%
    \pgfmathparse{random(0,60)-30}
    \draw (n-\i\j) to [bend right=\pgfmathresult] (n-\ii\j);
    }
    \end{tikzpicture}
\end{document}

rand6

A little more 'randomness'

I think this is as far as I can push it with this approach. Here I use an \ifthenelse{}{}{} conditional to remove some of the horizontal lines to give it less of a grid look, and for it to look more organic. Note in the picture below how not all of the nodes are joined by lines.

You can easily modify the code below to remove some vertical lines instead of horizontal lines (not both!). Only thing is that there will be sharp points/corners at random areas, and I have no idea how to make them smooth.

\documentclass[tikz,border=2mm]{standalone}
\usepackage{ifthen}
\pgfmathsetseed{\pdfuniformdeviate 10000000} %Credit: https://tex.stackexchange.com/a/212755/117534
\begin{document}    
    \begin{tikzpicture}
    \pgfmathtruncatemacro{\rownum}{6} % Set number of rows here
    \pgfmathtruncatemacro{\colnum}{3} % Set number of columns here
    \pgfmathtruncatemacro{\rowtemp}{\rownum-1}
    \pgfmathtruncatemacro{\coltemp}{\colnum-1}
    % Place coordinates in a grid like fashion; randomized
    \foreach \i in {0,...,\rownum}
    \foreach \j in {0,...,\colnum}
    \coordinate[] (n-\i\j) at (\i + 0.45*rand,\j + 0.45*rand) {};
    % Draw vertical lines
    \foreach \i in {0,...,\rownum}
    \foreach \j [count=\jj] in {0,...,\coltemp}{%
    \pgfmathparse{random(0,60)-30}
    \draw (n-\i\j) to [bend right=\pgfmathresult] (n-\i\jj);
}   
    % Draw horizontal lines
    \foreach \i [count=\ii] in {0,...,\rowtemp}{%
    \foreach \j in {0,...,\colnum}{%
    \pgfmathtruncatemacro{\randnum}{random(0,60)-30}
    \ifthenelse{\isodd{\randnum/2} \OR \j=0 \OR \j=\colnum}{% <------------------ Ensure end horz lines are drawn
        \draw (n-\i\j) to [bend right=\randnum] (n-\ii\j)}{};
    }}
    \end{tikzpicture}
\end{document}

rand7

  • Very nice, thank you. It's a minor point, but do you know how to make the edges curved? – JSharpee May 9 '17 at 8:51
  • @JSharpee see edit, please. – Troy May 9 '17 at 9:14
  • that looks really really good! One last thing: I couldn't figure out a way to have an non-square grid. Is there a way to have (for example) a 4 by 5 grid etc? Even better is there a way to have a 'random' grid - so column 1 has 4 rows, column 2 has 3 rows, column 3 has 5 rows, etc? – JSharpee May 9 '17 at 11:54
  • @JSharpee see edit for rectangular grids. random grid will require a diff approach I'm afraid (can't modify what I have here I think). If I have time I may give it a shot sometime in the future.. – Troy May 9 '17 at 13:41
  • Adding a pathmorphing code before each path gives even better "random" results. For example, using the code of "mixing" from tex.stackexchange.com/questions/340121/… to modify all the paths while drawing gives better results of the map – Damitr Mar 14 at 9:22

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.