[Tex/LaTex] How to generate n points on a circumference and connect all of them while having constraints on the image size

circlestikz-pgf

I have to generate a circle that has n points on the circumference that have to be connected with all possible cords. I drew the required picture using Geogebra and tried exporting it as a normal png. However the quality of the png file was not good enough, thus I decided to export the image as an eps file. If I include the eps in my file the picture is displayed, but as I resize it, the lines become either too wide or, the indices of the points become so small that the picture is not useful. So I tried to export the image in terms of PGF/TikZ code. But I had the same problem. I cannot predict how big the image will be, thus after exporting I have to resize it. So my question is what should I do ? Is there an easier way to draw such circle? What can I do about the image size, so I will not need to resize the image every time ?

Best Answer

Just with the help of TikZ it is perfectly possible to do such a job. Here is indeed a possible solution that allows you to

  1. easily set the number of points on the circle;
  2. set the circle radius;
  3. decide the position of the picture in terms of coordinates
  4. eventually position labels.

The first three things are did by one command: \drawconnectvertices while to add labels one should use \drawconnectverticeslabelled.

Here is the code (revised thanks to Brent suggestion in the comments):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.geometric} % required for the polygon shape

\def\drawconnectvertices[num vertex=#1, circle radius=#2] at (#3);{%
\pgfmathtruncatemacro\vertices{#1}
\pgfmathsetmacro\circleradius{#2}
\pgfmathsetmacro\halfcircleradius{\circleradius/2}
\draw[blue] (#3) circle (\halfcircleradius cm) node[regular polygon, regular polygon sides=\vertices, minimum size=\circleradius cm, draw=none, name={vertex set}] {};
\foreach \x in {1,...,\vertices}{
    \node[draw,circle, inner sep=1pt,blue, fill=blue] at (vertex set.corner \x) {};
}

\foreach \x in {1,...,\vertices}{
    \foreach \y in {\x,...,\vertices}{
            \draw[ultra thin, red] (vertex set.corner \x)--(vertex set.corner \y);
    }
}
}

\def\drawconnectverticeslabelled[num vertex=#1, circle radius=#2, shift angle=#3] at (#4);{%
\pgfmathtruncatemacro\vertices{#1}
\pgfmathsetmacro\circleradius{#2}
\pgfmathsetmacro\halfcircleradius{\circleradius/2}
\draw[blue] (#4) circle (\halfcircleradius cm) node[regular polygon, regular polygon sides=\vertices, minimum size=\circleradius cm, draw=none, name={vertex set}] {};
\foreach \x in {1,...,\vertices}{
    \node[draw,circle, inner sep=1pt,blue, fill=blue] at (vertex set.corner \x) {};
    \pgfmathparse{#3-360*(\x-1)/ \vertices}
    \node at ($(vertex set)+(\pgfmathresult:\halfcircleradius)$)[label={[font=\small]\pgfmathresult:$\x$}]{};
}

\foreach \x in {1,...,\vertices}{
    \foreach \y in {\x,...,\vertices}{
            \draw[ultra thin, red] (vertex set.corner \x)--(vertex set.corner \y);
    }
}
}

\begin{document}
\begin{tikzpicture}
\drawconnectverticeslabelled[num vertex=6, circle radius=3, shift angle=0] at (0,0.75);
\drawconnectverticeslabelled[num vertex=4, circle radius=2, shift angle=45] at (4,0.75);
\drawconnectvertices[num vertex=8, circle radius=3] at (8,0.75);
\drawconnectverticeslabelled[num vertex=15, circle radius=6, shift angle=90] at (4,-5);
\drawconnectvertices[num vertex=30, circle radius=8] at (4,-13.5);
\end{tikzpicture}
\end{document}

The output:

enter image description here

One remark on \drawconnectverticeslabelled: as it is possible to see from the example, the order of the labels could be changed according to the shift angle.


After having seen TeX Parameter Processing imitating key-value pairs, here is a complete TikZ-based solution.

I've defined \drawconnectvertices as an alias of \node and now the parameters and the type of drawing are set up with pgfkeys:

  • parameters:

    1. num vertex;
    2. circle radius;
    3. shift angle;
    4. at pos (new): provides the coordinates in which the node is positioned;
  • type of drawing:

    1. circumference with points;
    2. circumference with points labelled.

Notice that the parameters should always be declared before the type of drawing.

The code:

\documentclass{article}
\usepackage[height=22cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.geometric} % required for the polygon shape

\pgfkeys{/tikz/.cd,
     num vertex/.initial=6,
     num vertex/.get=\vertices,
     num vertex/.store in=\vertices,
     circle radius/.initial=3,
     circle radius/.get=\circleradius,
     circle radius/.store in=\circleradius,
     shift angle/.initial=0,
     shift angle/.get=\shiftangle,
     shift angle/.store in=\shiftangle, 
     at pos/.initial={(0,0)},
     at pos/.get=\position,
     at pos/.store in=\position,
}

% that's just an alias for \node
\makeatletter
\def\drawconnectvertices{\tikz@path@overlay{node}}
\makeatother

\pgfkeys{/tikz/circumference with points/.code={
        \pgfmathsetmacro\halfcircleradius{\circleradius/2}
        \draw[blue] \position circle (\halfcircleradius cm) node[regular polygon, regular polygon sides=\vertices, minimum size=\circleradius cm, draw=none, name={vertex set}] {};
        \foreach \x in {1,...,\vertices}{
            \node[draw,circle, inner sep=1pt,blue, fill=blue] at (vertex set.corner \x) {};
        }

        \foreach \x in {1,...,\vertices}{
            \foreach \y in {\x,...,\vertices}{
                    \draw[ultra thin, red] (vertex set.corner \x)--(vertex set.corner \y);
            }
        }
    }
}   

\pgfkeys{/tikz/circumference with points labelled/.code={
        \pgfmathsetmacro\halfcircleradius{\circleradius/2}
        \draw[blue] \position circle (\halfcircleradius cm) node[regular polygon, regular polygon sides=\vertices, minimum size=\circleradius cm, draw=none, name={vertex set}] {};
        \foreach \x in {1,...,\vertices}{
            \node[draw,circle, inner sep=1pt,blue, fill=blue] at (vertex set.corner \x) {};
            \pgfmathparse{\shiftangle-360*(\x-1)/ \vertices}
            \node at ($(vertex set)+(\pgfmathresult:\halfcircleradius)$)[label={[font=\small]\pgfmathresult:$\x$}]{};
        }

        \foreach \x in {1,...,\vertices}{
            \foreach \y in {\x,...,\vertices}{
                    \draw[ultra thin, red] (vertex set.corner \x)--(vertex set.corner \y);
            }
        }
    }
}   

\begin{document}
\begin{tikzpicture}
\drawconnectvertices[at pos={(0,0.75)}, circumference with points labelled] {};
\drawconnectvertices[num vertex=4, 
    circle radius=2,
    at pos={(4,0.75)},
    shift angle=45,circumference with points labelled] {};
\drawconnectvertices[num vertex=8,
    at pos={(8,0.75)},
    circumference with points] {};
\drawconnectvertices[num vertex=15,
    circle radius=6, 
    shift angle=90,
    at pos={(4,-5)},
    circumference with points labelled] {};
\drawconnectvertices[num vertex=30,
    circle radius=8,
    at pos={(4,-13.5)}, 
    circumference with points] {};
\end{tikzpicture}
\end{document}

provides the same result displayed above.

Related Question