2

I am trying to generate a newcommand that will create "spinners" to teach probability to students.
I have been able to produce code to generate polygon spinners with each triangular section shaded.

What I would like to do is pass the numbers, given as parameters (see picture), that would sit in the middle of each triangular section - they are not necessarily 1,2,3 etc. I was hoping i could do something like node {#\i} in a for each loop but this didn't work.

This is the output I would be after

\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 Answer 1

4

I'd suggest making a macro with just two arguments, where the second argument has a comma separated list of numbers. Then you can use \foreach [count=\i] \txt in {#2} ... to make the nodes.

I also changed how the border is drawn and the filling, to improve the look of the vertices.

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .