18

I am trying to draw a special and contradictive plot : A circle with random distortions which have to remain constant regardless of how many times I build the document.

An example of what I am trying to achieve can be seen below

enter image description here

This figure was produced with the following code

\documentclass{standalone}
\usepackage{tikz}   
\usetikzlibrary{decorations.pathmorphing}


\begin{document}
    \begin{tikzpicture}
        % axes
        \draw[ultra thin] (-2.5, 0) -- (2.5, 0);
        \draw[ultra thin] (0, -2.5) -- (0, 2.5);
        % undistorted sphere
        \draw[ultra thin, dashed] (0, 0) circle (2.25cm);
        \draw[ultra thin, dashed, ->] (0:0) -- (125:2.25);
        \node at (-1, 1) {$R$};
        % distrorted sphere
        \draw[very thick] plot[domain=0:350,smooth cycle ] (\x:2+rnd*0.5);
        \draw[very thick, ->] (0:0) -- (45:2.1);
        \draw[ultra thin, ->] (1, 0) arc (0:45:1);
        \node at (1.2, 0.4) {$\theta$};
        \node at (0.7, 1.25) {$R(\theta)$};
    \end{tikzpicture}
\end{document}

The problem with the code is that whenever I make a change to it and rebuild the document, the "distorted" circle will have a different shape, since I am using the rnd function. The reason I would like to have a constant-random shape is because I would like to denote its radius at an angle θ, therefore it has to touch the outline of the circle.

Any idea on how to achieve such a thing?

2 Answers 2

14

In answer by user JPG, the vector R don't intersect with randomize curve. For achieving the intersecting of two path, we have to use the intersections library. Adding to JPG's answer:

\documentclass{standalone}
\usepackage{tikz}   
\usetikzlibrary{decorations.pathmorphing,intersections}

    \begin{document}
       \begin{tikzpicture}[>=latex]
            \pgfmathsetseed{1} % choose a number which give a good shape to your circle
            % axes
            \draw[ultra thin] (-2.5, 0) -- (2.5, 0);
            \draw[ultra thin] (0, -2.5) -- (0, 2.5);
            % undistorted sphere
            \draw[ultra thin, dashed] (0, 0) circle (2.25cm);
            \draw[ultra thin, dashed, ->] (0:0) -- (125:2.25);
            \node at (-1, 1) {$R$};
            % distrorted sphere
            \path[draw,very thick, name path=curve] plot[domain=0:350,smooth cycle ] (\x:2+rnd*0.5);
            \path[very thick, name path=line] (0:0) -- (45:2.5);
            \draw [name intersections={of=curve and  line, by=x}];
            \draw [very thick, ->] (0,0)--(x);
            \draw[ultra thin, ->] (1, 0) arc (0:45:1);
            \node at (1.2, 0.4) {$\theta$};
            \node at (0.7, 1.25) {$R(\theta)$};
        \end{tikzpicture}
    \end{document}

enter image description here

4
  • With this elegant solution, we don't need the fixed random seed from the first answer anymore.
    – AlexG
    Commented Feb 12, 2019 at 10:10
  • @Thanos, your welcome. Also, You may thick the JPG's answer.
    – user31034
    Commented Feb 12, 2019 at 11:27
  • 2
    @ferahfeza : I know, but I believe your answer is more complete!
    – Thanos
    Commented Feb 12, 2019 at 11:29
  • how to shift to (-2,0) ? Commented Feb 10, 2021 at 22:20
15

Use \pgfmathsetseed to initialize the seed for random number generation:

\documentclass{standalone}
\usepackage{tikz}   
\usetikzlibrary{decorations.pathmorphing}


\begin{document}
   \begin{tikzpicture}
        \pgfmathsetseed{1} % choose a number which give a good shape to your circle
        % axes
        \draw[ultra thin] (-2.5, 0) -- (2.5, 0);
        \draw[ultra thin] (0, -2.5) -- (0, 2.5);
        % undistorted sphere
        \draw[ultra thin, dashed] (0, 0) circle (2.25cm);
        \draw[ultra thin, dashed, ->] (0:0) -- (125:2.25);
        \node at (-1, 1) {$R$};
        % distrorted sphere
        \draw[very thick] plot[domain=0:350,smooth cycle ] (\x:2+rnd*0.5);
        \draw[very thick, ->] (0:0) -- (45:2.1);
        \draw[ultra thin, ->] (1, 0) arc (0:45:1);
        \node at (1.2, 0.4) {$\theta$};
        \node at (0.7, 1.25) {$R(\theta)$};
    \end{tikzpicture}
\end{document}

EDIT: if you want the length of the arrow to be computed automatically

Then you can generate the length of the arrow before the plot, and use this length for the exact angle you want. Since you do not know on which angles the plot will be computed, you should start the plot at the angle you want to draw your arrow:

\documentclass{standalone}
\usepackage{tikz}   
\usetikzlibrary{decorations.pathmorphing}


\begin{document}
    \begin{tikzpicture}
        \pgfmathsetseed{1}
        % axes
        \draw[ultra thin] (-2.5, 0) -- (2.5, 0);
        \draw[ultra thin] (0, -2.5) -- (0, 2.5);
        % undistorted sphere
        \draw[ultra thin, dashed] (0, 0) circle (2.25cm);
        \draw[ultra thin, dashed, ->] (0:0) -- (125:2.25);
        \node at (-1, 1) {$R$};
        % distrorted sphere
        \pgfmathsetmacro\RR{2+rnd*0.5}
        \draw[very thick] plot[domain=45:395,smooth cycle ] (\x:{ifthenelse(\x==45,\RR,2+rnd*0.5)});
        \draw[very thick, ->] (0:0) -- (45:\RR);
        \draw[ultra thin, ->] (1, 0) arc (0:45:1);
        \node at (1.2, 0.4) {$\theta$};
        \node at (0.7, 1.25) {$R(\theta)$};
    \end{tikzpicture}
\end{document}

enter image description here

4
  • While we get a reproducible (not changing) random cyclic path on very compilation, it requires the thick arrow denoting the current radius to be adjusted manually.
    – AlexG
    Commented Feb 12, 2019 at 10:13
  • Thank you very much for your help! I wish I could select you answer. Keep up!!!
    – Thanos
    Commented Feb 12, 2019 at 11:21
  • 1
    @AlexG I updated my answer to have the arrow length adjusted automatically.
    – JPG
    Commented Feb 12, 2019 at 13:26
  • 1
    You could also do \draw[very thick] plot[domain=55:405,smooth cycle ] (\x:{2+rnd*0.5}); \pgfgetlastxy{\myx}{\myy} \draw[very thick, ->] (0:0) -- (\myx,\myy); in order to get rid of the ifthenelse. (The reason why I am suggesting that is that the comparison may fail in a plot.)
    – user121799
    Commented Feb 12, 2019 at 15:18

You must log in to answer this question.

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