圆角多边形

圆角多边形

我想要实现包围一些节点的漂亮的圆角多边形。

如果我们使用的\draw[rounded corners]结果多边形不包括我们的顶点。

\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}

一些节点和无法包含它们的圆角多边形

我们可以像这样移动多边形的中心点,但是对于每个节点,移动的方向都会发生变化,并且必须进行计算。

现在包含节点的更大多边形

我知道两种方法这个非常相关的问题但这两个答案都有缺点。符号 1 的答案实际上使用,filldraw所以我们不能让多个多边形相交。我的答案编译起来慢得离谱。

我的答案是:在某些节点周围绘制多边形的好方法是什么?

答案1

我认为没有必要重新发明这个,因为这已经有一个很好的答案了这里

\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}

在此处输入图片描述

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

在此处输入图片描述

答案2

您可以计算所有顶点的重心(在代码中为节点(b)),然后相对于其重心缩放多边形(在代码中使用scale around={1.3:(b)}存储在样式中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}

在此处输入图片描述

相关内容