4

I would like to realize beautiful rounded polygons enclosing some nodes.

If we use \draw[rounded corners] the resulting polygon does not include our vertices.

\documentclass[border=5]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

%Coordinates of the vertices
\foreach \i/\r in {0/1,1/0.8,2/1.2,3/1,4/0.9,5/1.1}
    \coordinate (c\i) at (60*\i:\r) {};

%The vertices
\foreach \i in {0,...,5} \node[fill=black,circle,inner sep=1pt] at (c\i) {};

%The Polygon
\draw[rounded corners=5] (c0)--(c1)--(c2)--(c3)--(c4)--(c5)--cycle;

\end{tikzpicture}
\end{document}

Some nodes and a rounded polygon that fails to entail them

We can shift the center points of the polygon like this, but for each node the direction of shifting changes and had to be calculated.

A larger polygon containing now the nodes

I know of the two approaches in this very much related Question but both answers have their drawbacks. Symbol 1s answer actually uses filldraw so we can not have multiple polygons intersect. My answer is just ridiculously slow for compiling.

So my answer is: What is a nice way of drawing a polygon around some nodes?

  • I accepted the answer but sadly for polygons with both flat and wide angels the 'rounded corners' approach is quite ugly. I will never find my desired solution – bitt.j May 7 '18 at 15:02
3

I do not think it is necessary to reinvent this since this has already a very nice answer here.

\documentclass[tikz]{standalone}
\makeatletter
\usetikzlibrary{decorations,backgrounds}
\def\pgfdecoratedcontourdistance{0pt}
\pgfset{
  decoration/contour distance/.code=%
    \pgfmathsetlengthmacro\pgfdecoratedcontourdistance{#1}}
\pgfdeclaredecoration{contour lineto closed}{start}{%
  \state{start}[
    next state=draw,
    width=0pt,
    persistent precomputation=\let\pgf@decorate@firstsegmentangle\pgfdecoratedangle]{%
    \pgfpathmoveto{\pgfpointlineattime{.5}
      {\pgfqpoint{0pt}{\pgfdecoratedcontourdistance}}
      {\pgfqpoint{\pgfdecoratedinputsegmentlength}{\pgfdecoratedcontourdistance}}}%
  }%
  \state{draw}[next state=draw, width=\pgfdecoratedinputsegmentlength]{%
    \ifpgf@decorate@is@closepath@%
      \pgfmathsetmacro\pgfdecoratedangletonextinputsegment{%
        -\pgfdecoratedangle+\pgf@decorate@firstsegmentangle}%
    \fi
    \pgfmathsetlengthmacro\pgf@decoration@contour@shorten{%
      -\pgfdecoratedcontourdistance*cot(-\pgfdecoratedangletonextinputsegment/2+90)}%
    \pgfpathlineto
      {\pgfpoint{\pgfdecoratedinputsegmentlength+\pgf@decoration@contour@shorten}
      {\pgfdecoratedcontourdistance}}%
    \ifpgf@decorate@is@closepath@%
      \pgfpathclose
    \fi
  }%
  \state{final}{}%
}
\makeatother
\tikzset{
  contour/.style={
    decoration={
      name=contour lineto closed,
      contour distance=#1
    },
    decorate}}
\begin{document}
\begin{tikzpicture}
\foreach \i/\r in {0/1,1/0.8,2/1.2,3/1,4/0.9,5/1.1}
    \coordinate (c\i) at (60*\i:\r);
\foreach \i in {0,...,5} \node[fill=black,circle,inner sep=1pt] at (c\i) {};

\draw[preaction={contour=-5pt,rounded corners=5,draw}] (c0)--(c1)--(c2)--(c3)--(c4)--(c5)--cycle;   
\end{tikzpicture}
\end{document}

enter image description here

\draw[draw=none,preaction={contour=-5pt,rounded corners=5,draw}] (c0)--(c1)--(c2)--(c3)--(c4)--(c5)--cycle; 

enter image description here

4

You can calculate the barycenter of all vertices (in the code is the node (b)), and then scale the polygon relatively to its barycenter (in the code using scale around={1.3:(b)} stored in a style s).

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{calc}
\begin{document}
  \begin{tikzpicture}
    %Coordinates of the vertices
    \coordinate(b); % <-- will contain the barycenter of the vertices
    \foreach[count=\n from 0] \i/\r in {0/1,1/0.8,2/1.2,3/1,4/0.9,5/1.1}
        \path (60*\i:\r) coordinate (c\i) ($(c\i)!\n/(\n+1)!(b)$) coordinate(b);

    %The vertices
    \foreach \i in {0,...,5} \node[fill=black,circle,inner sep=1pt] at (c\i) {};

    %The rounded polygon
    \draw[rounded corners=5,s/.style={scale around={1.3:(b)}}]
      ([s]c0) foreach \i in{1,...,5}{--([s]c\i)}--cycle;

  \end{tikzpicture}
\end{document}

enter image description here

  • Are you sure that this works in general, i.e. also for an L-shaped polygon, say? Or is your answer tailor-made for the polygon of the above question? – user121799 Apr 13 '18 at 15:01
  • 1
    @marmot it "works" only for convex polygons with not very strange distribution of the vertices. – Kpym Apr 13 '18 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.