如何绘制具有不同线条颜色和圆角的多边形?

如何绘制具有不同线条颜色和圆角的多边形?

我想画一个线条颜色不同,角也圆润的多边形,但是多边形的角不是圆的!

\documentclass[12pt]{report}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line width=5pt]
\coordinate (A) at (0,0);
\coordinate (B) at (2,0);
\coordinate (C) at (3,2);
\coordinate (D) at (1,3);
\coordinate (E) at (-1,2);
\draw[pink] (A) -- (B);
\draw[blue] (B) -- (C);
\draw[red] (C) -- (D);
\draw [green] (D) -- (E);
\draw (E) -- (A);
\end{tikzpicture}
\end{document}

答案1

这不会给您圆角,但我认为它确实会给您带来更好的效果。本质上,这个想法是使用剪辑来剪掉参差不齐的部分,然后使用三角形帽(长度等于线宽的一半除以 tan 54)来整理五边形内的连接处。

\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[line width=5pt]
  \foreach \i in {0,...,4}  \coordinate (p\i) at ({18+\i*72}:1);
  \clip (p0) -- (p1) -- (p2) -- (p3) -- (p4) -- cycle;
  \pgfmathsetmacro\xyz{.5*\pgflinewidth/tan(54)}
  \foreach \i [count=\j from 0, evaluate=\j as \k using {int(mod(\j+1,5))}] in {magenta,blue,green,orange,red} \draw [\i, {Triangle Cap[length=\xyz pt]}-{Triangle Cap[length=\xyz pt]}] (p\j) -- (p\k);
\end{tikzpicture}
\end{document}

彩色五边形

如果你愿意,你可以添加[rounded corners]\clip,但我认为结果看起来并不好,因为你只能在线的边缘。

例如,添加[rounded corners=.5*\pgflinewidth]产品

外圆角

对于真正的舍入,填充多边形的扇区可能比绘制其边更容易。例如,

\begin{tikzpicture}[line width=10pt]
  \foreach \i in {0,...,4}  \path ({18+\i*72}:1) coordinate (p\i) ++({18+\i*72:-.5*\pgflinewidth}) coordinate (q\i);
  \clip [rounded corners=.5*\pgflinewidth] (p0) -- (p1) -- (p2) -- (p3) -- (p4) -- cycle (q0) -- (q4) -- (q3) -- (q2) -- (q1) -- cycle;
  \pgfmathsetmacro\xyz{.5*\pgflinewidth/tan(54)}
  \foreach \i [count=\j from 0, evaluate=\j as \k using {int(mod(\j+1,5))}] in {magenta,blue,green,orange,red} \path [fill, \i] (p\j) -- (p\k) -- (0,0) -- cycle;
\end{tikzpicture}

生产

圆润的内角和外角

在我看来,这个版本不如非圆角版本那么整洁,但我可以想象,对于某些专业用途来说,这个版本可能更好。为了获得最佳效果,您可能需要稍微调整一下边角的圆度 - 这里可能存在权衡。

pic将它们转换成用于绘制任何正多边形(不仅仅是五边形)并不难。

例如,下面给出的定义可以写成

\begin{tikzpicture}
  \pic at (0,0) {rainbow polygon={sides=6,colours={blue,blue!80!cyan,blue!60!cyan,blue!40!cyan,blue!20!cyan,cyan}}};
  \pic at (2,0) {rainbow polygon={rounded corners=outer,sides=12}};
  \pic at (0,2) {rainbow polygon};
  \pic at (2,2) {rainbow polygon={rounded corners=both,sides=7,colours={red,orange,yellow,green,blue,purple,magenta},line width=8pt}};
\end{tikzpicture}

生产

彩虹多边形

采用pic rainbow polygon单个可选参数。如果指定,则应提供自定义选项。在这里使用自己的键才有意义pic,尽管它也可以接受其他 TikZ 键。

colours={<comma separated list of colours>}
line width=<dimension>
sides=<sensible integer>
rounded corners=<none|inner|outer|both>
size=<dimension>

完整代码[使用风险自负!]:

\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{arrows.meta}
\makeatletter
\newif\ifrainbowpolygon@rounded@outer
\newif\ifrainbowpolygon@rounded@inner
\newcounter{rainbowpolygon@colours}
\setcounter{rainbowpolygon@colours}{0}
\tikzset{%
  pics/rainbow polygon/.style={%
    code={%
      \tikzset{%
        rainbow polygon/.cd,
        defaults,
        #1
      }
      \ifrainbowpolygon@rounded@outer\tikzset{rainbow polygon/clip/.style={rounded corners=.5*\pgflinewidth}}\fi
      \begin{scope}[line width=\rainbowpolygon@linewidth]
        \foreach \i in {0,...,\rainbowpolygon@last}  \path ({90-\rainbowpolygon@eangle+\i*\rainbowpolygon@eangle}:\rainbowpolygon@size) coordinate (p\i) \ifrainbowpolygon@rounded@inner ++({90-\rainbowpolygon@eangle+\i*\rainbowpolygon@eangle}:-.5*\pgflinewidth) coordinate (q\i) \fi ;
         \clip [rainbow polygon/clip] (p0) \foreach \i in {1,...,\rainbowpolygon@last} { -- (p\i) } -- cycle \ifrainbowpolygon@rounded@inner (q0) \foreach \j in {\rainbowpolygon@last,...,1} { -- (q\j) } -- cycle \fi ;
         \ifrainbowpolygon@rounded@inner
           \foreach \i [evaluate=\i as \k using {int(mod(\i+1,\rainbowpolygon@sides))}, evaluate=\i as \j using {int(mod(\i,\therainbowpolygon@colours))}] in {0,...,\rainbowpolygon@last} \path [fill, col\j] (p\i) -- (p\k) -- (0,0) -- cycle;
         \else
           \pgfmathsetmacro\xyz{.5*\pgflinewidth/tan(.5*\rainbowpolygon@iangle)}
           \foreach \i [evaluate=\i as \k using {int(mod(\i+1,\rainbowpolygon@sides))}, evaluate=\i as \j using {int(mod(\i,\therainbowpolygon@colours))}] in {0,...,\rainbowpolygon@last} \draw [col\j, {Triangle Cap[length=\xyz pt]}-{Triangle Cap[length=\xyz pt]}] (p\i) -- (p\k);
         \fi
    \end{scope}
    }
  },
  rainbow polygon/.search also={/tikz},
  rainbow polygon/.cd,
  angles/.code={%
    \pgfmathsetmacro\rainbowpolygon@eangle{360/#1}%
    \pgfmathsetmacro\rainbowpolygon@iangle{180-\rainbowpolygon@eangle}%
  },
  last/.code={%
    \pgfmathsetmacro\rainbowpolygon@last{int(#1-1)}%
  },
  line width/.store in=\rainbowpolygon@linewidth,
  sides/.store in=\rainbowpolygon@sides,
  sides/.forward to=/tikz/rainbow polygon/angles,
  sides/.forward to=/tikz/rainbow polygon/last,
  size/.store in=\rainbowpolygon@size,
  rounded outer corners/.is if=rainbowpolygon@rounded@outer,
  rounded inner corners/.is if=rainbowpolygon@rounded@inner,
  rounded corners/.is choice,
  rounded corners/none/.style={/tikz/rainbow polygon/.cd, rounded outer corners=false, rounded inner corners=false},
  rounded corners/outer/.style={/tikz/rainbow polygon/rounded outer corners},
  rounded corners/inner/.style={/tikz/rainbow polygon/rounded inner corners},
  rounded corners/both/.style={/tikz/rainbow polygon/.cd, rounded outer corners, rounded inner corners},
  clip/.style={},
  colours/.code={%
    \setcounter{rainbowpolygon@colours}{0}%
    \edef\tempa{#1}%
    \foreach \i in \tempa {
      \edef\tempb{col\therainbowpolygon@colours}%
      \xglobal\colorlet{\tempb}{\i}%
      \stepcounter{rainbowpolygon@colours}%
    }%
  },
  colours/.default={magenta,blue,green,orange,red,gray},
  defaults/.style={%
    line width=5pt,
    sides=5,
    size=10mm,
    rounded corners=none,
    colours,
  },
}
\makeatother
\begin{document}
\begin{tikzpicture}
  \pic at (0,0) {rainbow polygon={sides=6,colours={blue,blue!80!cyan,blue!60!cyan,blue!40!cyan,blue!20!cyan,cyan}}};
  \pic at (2,0) {rainbow polygon={rounded corners=outer,sides=12}};
  \pic at (0,2) {rainbow polygon};
  \pic at (2,2) {rainbow polygon={rounded corners=both,sides=7,colours={red,orange,yellow,green,blue,purple,magenta},line width=8pt}};
\end{tikzpicture}

\begin{tikzpicture}[line width=5pt]
  \foreach \i in {0,...,4}  \coordinate (p\i) at ({18+\i*72}:1);
  \clip (p0) -- (p1) -- (p2) -- (p3) -- (p4) -- cycle;
  \pgfmathsetmacro\xyz{.5*\pgflinewidth/tan(54)}
  \foreach \i [count=\j from 0, evaluate=\j as \k using {int(mod(\j+1,5))}] in {magenta,blue,green,orange,red} \draw [\i, {Triangle Cap[length=\xyz pt]}-{Triangle Cap[length=\xyz pt]}] (p\j) -- (p\k);
\end{tikzpicture}
\begin{tikzpicture}[line width=5pt]
  \foreach \i in {0,...,4}  \path ({18+\i*72}:1) coordinate (p\i);
  \clip [rounded corners=.5*\pgflinewidth] (p0) -- (p1) -- (p2) -- (p3) -- (p4) -- cycle;
  \pgfmathsetmacro\xyz{.5*\pgflinewidth/tan(54)}
  \foreach \i [count=\j from 0, evaluate=\j as \k using {int(mod(\j+1,5))}] in {magenta,blue,green,orange,red} \draw [\i, {Triangle Cap[length=\xyz pt]}-{Triangle Cap[length=\xyz pt]}] (p\j) -- (p\k);
\end{tikzpicture}
\begin{tikzpicture}[line width=10pt]
  \foreach \i in {0,...,4}  \path ({18+\i*72}:1) coordinate (p\i) ++({18+\i*72:-.5*\pgflinewidth}) coordinate (q\i);
  \clip [rounded corners=.5*\pgflinewidth] (p0) -- (p1) -- (p2) -- (p3) -- (p4) -- cycle (q0) -- (q4) -- (q3) -- (q2) -- (q1) -- cycle;
  \pgfmathsetmacro\xyz{.5*\pgflinewidth/tan(54)}
  \foreach \i [count=\j from 0, evaluate=\j as \k using {int(mod(\j+1,5))}] in {magenta,blue,green,orange,red} \path [fill, \i] (p\j) -- (p\k) -- (0,0) -- cycle;
\end{tikzpicture}
\end{document}

相关内容