17

Some irregular curves and their surrounding shadows are all needed to draw a graph in graph theory. I'm not very good at drawing this with Tikz, but I want to do my best to draw the following graph.

Enter image description here

I used some code from Drawing Königsberg landscape showing the bridges and it seems that no good and concise. But I didn't draw it well enough, and the code wasn't clean enough.

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing, calc}
\definecolor{babypink}{rgb}{0.96, 0.76, 0.76}
\tikzset{%
    contour/.style={dashed,%
        very thick,%
        decoration={%
            random steps,%
            segment length=4pt,%
            amplitude=0.5pt%
        },%
        rounded corners=1pt,%
        decorate%
    }%
}

\begin{document}
    \begin{tikzpicture}[x=10cm, y=9.19cm]
        \filldraw[babypink] ($(0, 1) + (0.241, -0.622)$) -- ($(0, 1) + (0.235,
        -0.587)$)
        --
        ($(0, 1) + (0.240, -0.540)$) -- ($(0, 1) + (0.249, -0.524)$) --
        ($(0, 1) + (0.252, -0.498)$) -- ($(0, 1) + (0.266, -0.482)$) --
        ($(0, 1) + (0.271, -0.462)$) -- ($(0, 1) + (0.288, -0.454)$) --
        ($(0, 1) + (0.300, -0.434)$) -- ($(0, 1) + (0.308, -0.418)$) --
        ($(0, 1) + (0.320, -0.412)$) -- ($(0, 1) + (0.328, -0.404)$) --
        ($(0, 1) + (0.399, -0.399)$) -- ($(0, 1) + (0.453, -0.393)$) --
        ($(0, 1) + (0.518, -0.386)$) -- ($(0, 1) + (0.549, -0.388)$) --
        ($(0, 1) + (0.609, -0.404)$) -- ($(0, 1) + (0.624, -0.410)$) --
        ($(0, 1) + (0.644, -0.438)$) -- ($(0, 1) + (0.663, -0.486)$) --
        ($(0, 1) + (0.670, -0.519)$) -- ($(0, 1) + (0.668, -0.546)$) --
        ($(0, 1) + (0.658, -0.590)$) -- ($(0, 1) + (0.648, -0.612)$) --
        ($(0, 1) + (0.636, -0.648)$) -- ($(0, 1) + (0.633, -0.666)$) --
        ($(0, 1) + (0.617, -0.677)$) -- ($(0, 1) + (0.596, -0.700)$) --
        ($(0, 1) + (0.535, -0.708)$) -- ($(0, 1) + (0.500, -0.709)$) --
        ($(0, 1) + (0.457, -0.717)$) -- ($(0, 1) + (0.412, -0.708)$) --
        ($(0, 1) + (0.372, -0.702)$) -- ($(0, 1) + (0.336, -0.695)$) --
        ($(0, 1) + (0.291, -0.679)$) -- ($(0, 1) + (0.268, -0.652)$) --
        cycle;
        \draw[contour] ($(0, 1) + (0.241, -0.622)$) -- ($(0, 1) + (0.235, -0.587)$) --
        ($(0, 1) + (0.240, -0.540)$) -- ($(0, 1) + (0.249, -0.524)$) --
        ($(0, 1) + (0.252, -0.498)$) -- ($(0, 1) + (0.266, -0.482)$) --
        ($(0, 1) + (0.271, -0.462)$) -- ($(0, 1) + (0.288, -0.454)$) --
        ($(0, 1) + (0.300, -0.434)$) -- ($(0, 1) + (0.308, -0.418)$) --
        ($(0, 1) + (0.320, -0.412)$) -- ($(0, 1) + (0.328, -0.404)$) --
        ($(0, 1) + (0.399, -0.399)$) -- ($(0, 1) + (0.453, -0.393)$) --
        ($(0, 1) + (0.518, -0.386)$) -- ($(0, 1) + (0.549, -0.388)$) --
        ($(0, 1) + (0.609, -0.404)$) -- ($(0, 1) + (0.624, -0.410)$) --
        ($(0, 1) + (0.644, -0.438)$) -- ($(0, 1) + (0.663, -0.486)$) --
        ($(0, 1) + (0.670, -0.519)$) -- ($(0, 1) + (0.668, -0.546)$) --
        ($(0, 1) + (0.658, -0.590)$) -- ($(0, 1) + (0.648, -0.612)$) --
        ($(0, 1) + (0.636, -0.648)$) -- ($(0, 1) + (0.633, -0.666)$) --
        ($(0, 1) + (0.617, -0.677)$) -- ($(0, 1) + (0.596, -0.700)$) --
        ($(0, 1) + (0.535, -0.708)$) -- ($(0, 1) + (0.500, -0.709)$) --
        ($(0, 1) + (0.457, -0.717)$) -- ($(0, 1) + (0.412, -0.708)$) --
        ($(0, 1) + (0.372, -0.702)$) -- ($(0, 1) + (0.336, -0.695)$) --
        ($(0, 1) + (0.291, -0.679)$) -- ($(0, 1) + (0.268, -0.652)$) --
        ($(0, 1) + (0.241, -0.622)$);
    \node[draw,circle] (u) at ($(0, 1) + (0.5, -0.388)$)[label={$u$}]{};
 \node[draw,circle] (u2)  at ($(0, 1) + (0.658,
 -0.582)$)[label=right:{$u_2$}]{};
\node[draw,circle] (v) at ($(0, 1) + (0.5, -0.71)$)[label=below:{$v$}]{};
\node[draw,circle] (u1) at ($(0, 1) + (0.23, -0.582)$)[label=left:{$u_1$}]{};
\draw [in=-165, out=165, looseness=5.00](u) to (v);
\node[] at (0.5,0.5) {$f$};
\end{tikzpicture}
\end{document}

![Enter image description here

Holding a learning attitude, I‘d like to learn more concise tikz code which can draw the graph I want. For those irregular curves in the original graph, I don't know if there is any software that can assist in generating them.

1
  • 1
    What do you mean by "it seems that no good and concise" (seems to be incomprehensible/logically inconsistent)? Please respond by editing your question, not here in comments (without "Edit:", "Update:", or similar - the question should appear as if it was written right now). Jul 5, 2021 at 18:02

2 Answers 2

17

That's a very painful way of drawing something like that. Unfortunately, the random steps command is not very easy to use in this context so maybe drawing this step by step with rounded corners could be a solution:

graph with random paths

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}
    \tikzset
        {
        dot/.style={circle,draw,thick,fill=white,inner sep=2pt},
        rdm/.style={thick,dotted,rounded corners=5pt},
        }
    \begin{tikzpicture}[node distance=2cm]
        \draw[cyan] (-2,-2) grid (4,2);
        \node[dot,label=below left:$z_1$] (z1) {};
        \node[dot,above right = of z1,label=above right:$u$] (u) {};
        \node[dot,below right = of z1,label=below right:$v$] (v) {};
        \node[dot,above right = of v,label=below right:$z_2$] (z2) {};
        
        \draw[thick] (u) to[out=160,in=190,looseness=3] node[midway,left] {$e$} (v) ;
        
        \def\a{0.35}
        \draw[rdm] (z1) --++ (.5*\a,\a) --++ (\a,0) --++ (0,1.5*\a) node[left] {$P_1$} --++ (\a,0) --++ (0,\a) -- (u);
        \draw[rdm] (z1) --++ (0,-\a) --++ (\a,0) --++ (0,-1.5*\a) --++ (\a,0) --++ (0,-\a) -- (v);
        \draw[rdm] (v) --++ (\a,0.5*\a) --++ (\a,0) --++ (0,\a) --++ (\a,0) --++ (0,\a) --++ (\a,0) -- (z2);
        \draw[rdm] (u) --++ (.75*\a,0) --++ (0,-\a) --++ (1.75*\a,0) --++ (0,-\a) --++ (1.5*\a,0) node[right] {$P_2$} -- (z2);
        \path (u) -- (v) node[midway] {$f$};
    \end{tikzpicture}
\end{document}

This is obviously strongly customizable.

3
  • Thank you very much, indeed this is very painful. Originally I intended to use GeoGebra, but I do not know how to draw those irregular curves .
    – licheng
    Jul 5, 2021 at 12:06
  • 1
    One solution would be to draw them in a software like Illustratior and get the SVG file (Bezier curves).
    – SebGlav
    Jul 5, 2021 at 15:01
  • 1
    Inkscape with TikZ export might be helpful to get this part of the image.
    – usr1234567
    Jul 6, 2021 at 11:37
20

I start by apologize for giving a MetaPost/MetaFun+ConTeXt answer, even though you were asking for a TikZ one. This is a method I have used before to draw some random smooth curves. I can probably be used also for TikZ, but I do not know how.

The idea is to randomize and smoothen a path. Look at the following picture:

a circle that is first randomized and then smoothed

Here we have started with a circle (defined by circularpath(10), which gives a circle with 40 points), drawn in blue. We then randomize it by using randomized (the light pink curve). This curve is not smooth. We get a smooth curve by applying curved to the result (the dark pink curve). The pink curves are built by the same points (shown in dark red), but with different control points.

For your example, the full code could look like this (this is the code I used, it is compiled with context). Note that since it uses randomization, your result might look a bit different.

\startMPpage[offset=3bp]
u:=1cm;
pickup pencircle scaled 1.5bp;

path blob,handle;
pair blobtop, blobleft, blobbottom, blobright;

blob = curved(circularpath(10) scaled 6u randomized 0.5u);

blobtop := (ulcorner blob -- urcorner blob) intersectionpoint blob;
blobleft := (ulcorner blob -- llcorner blob) intersectionpoint blob;
blobbottom := (llcorner blob -- lrcorner blob) intersectionpoint blob;
blobright := (lrcorner blob -- urcorner blob) intersectionpoint blob;

handle = blobtop{dir 160} .. {dir 25}blobbottom;

fill blob withcolor 0.95white;
draw blob dashed withdots scaled 0.75bp;
draw handle;

for i = blobtop,blobleft,blobbottom,blobright:
  unfill fullcircle scaled 8bp shifted i;
  draw fullcircle scaled 8bp shifted i;
endfor;


label.rt("$e$", point 0.5 of handle);
label("$f$", center blob);
label.lft("$P_1$", point 33 of blob);
label.rt("$P_2$", point 9 of blob);
labeloffset := 6bp;
label.urt("$u$",blobtop);
label.lft("$z_1$",blobleft);
label.lrt("$v$",blobbottom);
label.rt("$z_2$",blobright);
\stopMPpage

The resulting picture

4
  • 6
    No need to apologise - alternative solutions are always interesting and helpful. Sometimes OP can use them and sometimes others can use them. Jul 5, 2021 at 22:48
  • 2
    And the result is far nicer than mine, so +1 for yours.
    – SebGlav
    Jul 6, 2021 at 7:57
  • 1
    Thanks for that, @hpekristiansen. I like that the community here is welcoming and open-minded.
    – mickep
    Jul 6, 2021 at 15:34
  • 2
    @SebGlav Thanks! I prefer your answer (+1) since it solves the problem and uses the right tool.
    – mickep
    Jul 6, 2021 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .