2

I am working on Graph Theory and I have to draw paths that seem quite random such as the ones on the 1st image. I have created the paths P,Q on the 2nd image using the code below.

However, it seems really frustrating having to add more points to achieve more definition and having to change "ins" and "outs" of paths to "generate randomness".

What I'm really asking for is a more practical way to generate paths such as P,Q, that could then, of course, be impelemnted to produce something like the graph on the 1st image.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}[line cap=round]

% Path P \draw[thick] (-5,2) to[out=20, in=170] ++(0.5,0.1) to[out=-2, in=130] ++(0.5,+0) to[out=-40, in=168] ++(0.5,-0.07)
to[out=-10, in=170] ++(0.5,+0.05)
to[out=-10, in=120] ++(0.5,0) to[out=300, in=170] ++(0.5,+0.05)
to[out=-20, in=120] ++(0.5,+0.05) coordinate (u)
to[out=-20, in=170] ++(0.5,0) to[out=-10, in=170] ++(0.5,+0.05) coordinate (P)
;

\node[shift={(0.2,0)}] at (P) {$\mathrm{P}$};

% Path Q \draw[thick] (-5,0) to[out=300, in=160] ++(0.5,-0.1) to[out=-10, in=170] ++(0.5,0)
to[out=-2, in=175] ++(0.5,+0.1) coordinate (y) to[out=-4, in=178] ++(0.5,-0.1) to[out=-2, in=210] ++(0.5,+0) to[out=30, in=190] ++(0.5,0.05) coordinate (v) to[out=+10, in=130] ++(0.5,+0.05)
to[out=-30, in=160] ++(0.5,0) to[out=-20, in=220] ++(0.5,+0.05) coordinate (Q) ; \node[shift={(0.2,0)}] at (Q) {$\mathrm{Q}$};

\end{tikzpicture} \end{document}

1 Answers1

2

Here's a way to do it.

  • \usetikzlibrary{decorations.pathmorphing}
  • defining decoration, with some overwrite for Q (rounded corners)
  • use relative polar coordinates to set end points
  • storing coordinates of end points
  • placing nodes with labels there

However, tikz sometimes needs a lot of resources when varying parameters for segment length and rounded corners.

result


\documentclass[10pt,border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}

\begin{tikzpicture} % see 50.3.1 Decorations Producing Straight Line Paths [decoration={random steps,segment length=3mm}] % paths \draw [decorate] (0,0) -- (10:5) coordinate (P); \draw [decorate, rounded corners=1mm] (0,-1) -- ++(-5:5) coordinate (Q); % labels \node at (P) [xshift=5mm] {P}; \node at (Q) [xshift=5mm] {Q}; \end{tikzpicture}

\end{document}

MS-SPO
  • 11,519