How to draw a closed smooth curve from a list of nodes?

Karl is a list of randomly generated nodes by \curvepnodes using parametric points in RPN (I don't know how to use rand in algebraic expression). The number of elements of the list is \Karlnodecount + 1 or plotpoints.

In the documentation, I only see \psnline to connect the list with a straight line segment between two consecutive nodes. To close the path, I invoke \closepath inside \pscustom.

• Unfortunately, the closing line segment does not look smooth.
• Instead of using a straight line segment, I want to use a curved one. Using \psparametricplot[plotstyle=curve] does help a bit but the closing segment is still problematic.

\documentclass[pstricks]{standalone}
\usepackage{pst-plot,pst-node}
\psset
{
fillstyle=solid,
fillcolor=gray,
linearc=2pt,
}
\begin{document}
\begin{pspicture}(-3,-3)(3,3)
\curvepnodes[plotpoints=40]{0}{360}{/R rand 1001 mod 1000 div 1.5 add def R t cos mul  R t sin mul}{Karl}
\pscustom
{
\psnline(0,\Karlnodecount){Karl}
\closepath
}
\end{pspicture}
\end{document}


I also tried using \multido and \curveto but \curveto needs 3 points per invocation (two of them are control points of a bezier curve).

How to draw a closed smooth curve from a list of nodes?

-
Does it have to be pstricks? I'm sure you can do this with tikz. –  Anke Apr 9 '13 at 12:22
@Anke: The bounties are offered for a single correct answer in PSTricks. I am so sorry. Solutions in TikZ are welcome but there is no bounty for them. –  cyanide-based food Apr 9 '13 at 12:25
It's ok, I'm no bounty hunter ;) If it was just about getting the desired result, I could have helped. But if this is just about proving a point or whatever, I'm not going to bother trying to help with tikz. –  Anke Apr 9 '13 at 12:32

\documentclass{article}
\usepackage{pst-node,multido}
\SpecialCoor
\pstVerb{1234 srand}
\def\PlotImage#1{% #1: no of points
\pstVerb{ /Step 360 #1 div def } \def\randompath{}
\multido{\i=0+1}{#1}{%
\xdef\randompath{\randompath(! Rand 2 mul 1 sub 2.5 add \i\space Step mul PtoC  )}}%
\begin{pspicture}[showgrid=false](-3,-3)(3,3)
\psset{fillstyle=solid,fillcolor=black!20}
\expandafter\psccurve\randompath
\psset{linecolor=red,opacity=0.4,fillcolor=blue!40}
\expandafter\psccurve\randompath
\end{pspicture}}

\begin{document}

\PlotImage{36}  \PlotImage{142}

\end{document}


-
When the number of points is, say, 142, the result looks strange. –  g.kov Apr 9 '13 at 17:03
@g.kov: \curvepnodes makes life easier because we don't need to calculate the angle step. –  cyanide-based food Apr 9 '13 at 17:31
@g.kov: see my edit, the no of plotpoints is now no problem. –  Herbert Apr 9 '13 at 17:49
@Herbert: Yes, now it's perfect. –  g.kov Apr 9 '13 at 19:11
1 sub 2.5 add? –  cyanide-based food Apr 9 '13 at 22:51

Sorry, I don't know PSTricks, but for comparison, this is the Metapost equivalent of Jake's TikZ solution:

\starttext

\startMPpage[offset=3mm]

path p;
p := for i = 0 step 10 until 350 : (1 randomized 1)*dir(i) .. endfor cycle;
draw p scaled 1cm;

\stopMPpage

\stoptext

-
Ha, wow, Metapost is succinct! +1 –  Jake Apr 9 '13 at 13:09
@Jake: Yes, after all, metapost follows the syntax of metafont, a language designed by Knuth: succinct, precise, and weird (in the same way as TeX is weird). –  Aditya Apr 9 '13 at 13:34

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{hobby}
\begin{document}
\pgfmathsetseed{2}
\edef\randompath{}
\foreach \theta in {0,10,...,350} {
\pgfmathsetmacro\r{rnd+1}
\xdef\randompath{\randompath (\theta:\r) ..}
}
\begin{tikzpicture}[use Hobby shortcut]
\expandafter\draw\randompath cycle;
\end{tikzpicture}
\end{document}

-
It's not PSTricks, but it's a wonderful example of how elegant PGF/TikZ can be. +1. –  Sean Allred Apr 9 '13 at 12:36
There is a special bounty of 250 for your \xdef technique. I will be given at the last in your answer in another question (not this question because the fifth bounty cannot be 250). –  cyanide-based food Apr 9 '13 at 13:38
That reminds me ... I must upload the latest version of hobby to CTAN. Then it becomes a smidgeon easier: \draw[use Hobby shortcut] ([closed]0:rnd+2) \foreach \ang in {0,10,...,350} { .. (\ang:rnd+2) }; –  Loop Space Apr 9 '13 at 19:53
I get Use of \tikz@curveto@double doesn't match its definition.. I have a recent version of hobby. The problem seems to come from cycle –  Alain Matthes Apr 17 '13 at 5:46
@AlainMatthes Didn't spot that comment. I messed around with the internals of the package and broke the cycle syntax (I think irreparably). See tex.stackexchange.com/a/121310/86 for more details. –  Loop Space Jul 9 '13 at 7:59

The bullet proof solution.

\documentclass[pstricks]{standalone}

\usepackage{pst-node,pst-plot}
\psset{fillstyle=solid,fillcolor=gray}

\def\points{}
\pstVerb{realtime srand}
\def\N{25}

\begin{document}
\begin{pspicture}(-3,-3)(3,3)
\curvepnodes[plotpoints=\N]{0}{360}{rand 16 mod 15 div 1.5 add t PtoC}{P}
\multido{\i=0+1}{\Pnodecount}{\xdef\points{\points (P\i)}}
\expandafter\psccurve\points
\end{pspicture}
\end{document}

-
Use \psnccurve to save more keystrokes! –  cyanide-based food Nov 12 '13 at 20:39