3 ms·
The line noise argument is still funny to me, and I've been a happy Perl developer for years. Recently I wanted to draw an irregular circle and bumped into this
by bartvk 5y ago
The line noise argument is still funny to me, and I've been a happy Perl developer for years. Recently I wanted to draw an irregular circle and bumped into this Stack Overflow answer that expressed the answer in some LaTeX dialect -- boy I found that hard to parse.
\documentclass[tikz]{standalone}
\newcommand\irregularcircle[2]{% radius, irregularity
\pgfextra {\pgfmathsetmacro\len{(#1)+rand*(#2)}}
+(0:\len pt)
\foreach \a in {10,20,...,350}{
\pgfextra {\pgfmathsetmacro\len{(#1)+rand*(#2)}}
-- +(\a:\len pt)
} -- cycle
}
\begin{document}
\begin{tikzpicture}
\coordinate (c) at (0,0);
\coordinate (d) at (1,2);
\draw[blue,rounded corners=1mm] (c) \irregularcircle{1cm}{1mm};
\draw[red,rounded corners=1mm] (d) \irregularcircle{1cm}{1mm};
\end{tikzpicture}
\end{document}
Unfortunately I ran out of time and still have no idea what the final 3 lines of the irregularcircle command do, or why the \pgfmathsetmacro\len{} is repeated twice.
Source: https://tex.stackexchange.com/a/126179 https://tex.stackexchange.com/a/126179
- grayclhn 5y agoI like Perl, but if the best argument for its readability is that it’s better than using an archaic macro-only typesetting DSL for graphics…
- redis_mlc 5y agoWhich is false for more than half of the CPAN modules. You can pick a random module and the code is as readable as any other language - I find idiomatic python and rust much harder to read than perl, actually.
- pvaldes 5y ago> still have no idea what the final 3 lines of irregularcircle do, or why the \pgfmathsetmacro\len{} is repeated twice. Divide the circle in angles of 10 degrees. Each one will be a node for drawing the line. Then go to 10,20,30,40 degrees etc until reaching 350 degrees and assign a length for the radius in each node This length will be is the desired radius of the circle plus a small amount of irregularity that is randomly assigned but starting at a value provided as second argument. Finally we need to close the circle with -- cycle. The polygon is defined as a -- b -- cycle, or in this case: starting_node -- {iteration} -- closing_node. Iteration is a list of values for the radius each ten degrees. If I'm not wrong, we need to use the macro two times because the first time we use a starting value with angle = 0 and then we iterate over the whole circle. We need to assure that we start and end exactly at the same radius value (the radius provided as first argument) so the polygon line will close correctly. Therefore the first and last node can't have any irregularity.