我正在尝试生成一个新命令,用于创建“旋转器”来向学生讲授概率。
我已经能够编写代码来生成多边形旋转器,每个三角形部分都带有阴影。
我想要做的是传递作为参数给出的数字(见图),这些数字位于每个三角形部分的中间 - 它们不一定是 1,2,3 等。我希望我可以node {#\i}
在每个循环中做类似的事情,但这没有用。
\documentclass[12pt,a4paper]{article}
\usepackage[left=10mm, top=15mm, right=10mm, bottom=10mm]{geometry}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\newcommand{\polygon}[9]{%#1 number of sides, #2--numbers to go in spinner
\begin{tikzpicture}[line width =0.7mm]
\draw (0,0) coordinate (X) ;
\pgfmathsetmacro{\angle}{360/#1}
\foreach \i in {0,1,2,...,#1}{
\draw(X)--++(\angle*\i:2.5) coordinate (A\i);
}
\foreach \i in {0,1,2,...,\fpeval{#1-1}}{
\pgfmathsetmacro{\k}{100*\i/#1}
\pgfmathtruncatemacro{\j}{\i+1}
\filldraw[red!\k] (X)--(A\i)--(A\j)--cycle;
\draw (A\i)--(A\j);
}
\foreach \i in {0,1,2,...,#1}{
\draw(X)--(A\i);
}
\foreach \i in {1,2,...,#1}{
\pgfmathsetmacro{\l}{\angle*(\i+0.5)}
\path (X)--++(\l:1) coordinate (B\i);
}
\filldraw (X) circle [radius=3pt];
\end{tikzpicture} }%
\begin{document}
\polygon{4}{1}{2}{3}{6}{}{}{}{} \hspace{2cm}
\polygon{5}{1}{2}{3}{6}{}{}{}{}
\end{document}
答案1
我建议制作一个只包含两个参数的宏,其中第二个参数包含一个以逗号分隔的数字列表。然后,您可以使用它\foreach [count=\i] \txt in {#2} ...
来制作节点。
我还改变了边框的绘制和填充方式,以改善顶点的外观。
\documentclass[12pt,a4paper]{article}
\usepackage[left=10mm, top=15mm, right=10mm, bottom=10mm]{geometry}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\newcommand{\polygon}[2]{%#1 number of sides, #2--numbers to go in spinner
\begin{tikzpicture}[line width =0.7mm]
\draw (0,0) coordinate (X) ;
\pgfmathsetmacro{\angle}{360/#1}
\foreach \i in {0,1,2,...,#1}{
\draw(X)--++(\angle*\i:2.5) coordinate (A\i);
}
\foreach \i in {0,...,\fpeval{#1-1}}{
\pgfmathsetmacro{\k}{100*\i/#1}
\pgfmathtruncatemacro{\j}{\i+1}
% add line join=bevel to avoid bits of fill sticking outside the frame
\filldraw[red!\k,line join=bevel] (X)--(A\i)--(A\j)--cycle;
}
% draw border as single path to improve joins
\draw (A0) foreach \i in {1,...,\fpeval{#1-1}}{ -- (A\i) } -- cycle;
\foreach \i in {0,...,#1}{
\draw(X)--(A\i);
}
\foreach [count=\i] \txt in {#2}
{
\path (X)-- ++({(\i+0.5)*\angle}:1) node (B\i) {\txt};
}
\filldraw (X) circle [radius=3pt];
\end{tikzpicture} }%
\begin{document}
\polygon{4}{1,2,3,6}\hspace{2cm}
\polygon{5}{1,2,3,7,6}
\end{document}