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 ?
3 Answers
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
- easily set the number of points on the circle;
- set the circle radius;
- decide the position of the picture in terms of coordinates
- 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:
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:
num vertex
;circle radius
;shift angle
;at pos
(new): provides the coordinates in which the node is positioned;
type of drawing:
circumference with points
;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.
-
2Beautiful. Just one question: wouldn't
\foreach \y in {\x,...,\vertices}{
be sufficient? Dec 27, 2012 at 15:53 -
Thank you very much for your answer. I have just two more questions connected to your code. When I try to insert the number of vertices to be 2, I get the following error: ! Package PGF Math Error: You asked me to calculate `1/0.0', but I cannot divid e any number by zero. What should I do in order to plot a circle with 2 points ? And secondly is there any way to label the points created ? Dec 27, 2012 at 16:03
-
@user23707 : Hmm, I think TikZ may not be quite up to making a two-sided polygon... Dec 27, 2012 at 16:19
-
1@user23707: Brent made the point; indeed, in my solution I used the polygon shape (it helps cause all nodes are labelled), thus when you set up
num vertex=2
an error arises. For what concern the labels: do you need arbitrary labels of a solution like My polygon graph ends up “wonky” and I don't know why is good? Dec 27, 2012 at 16:35 -
1@Brent.Longborough: yes,
\foreach \y in {\x,...,\vertices}
is sufficient. Thanks for the remark:)
Dec 27, 2012 at 16:37
run with xelatex
\documentclass{article}
\usepackage{pst-poly}
\providecommand{\PstPolygonNode}{%
\psdots[dotscale=2](1;\INode)
\multido{\iA=0+1}{\INode}{%
\multido{\iB=\iA+1}{\numexpr\INode-\iA+1\relax}{%
\psline[linecolor=blue!50](1;\iA)(1;\iB)}}}
\begin{document}
\psset{unit=2,linewidth=0.2pt}
\PstPolygon[PolyNbSides=4] \qquad \PstPolygon[PolyNbSides=8]
\bigskip
\PstPolygon[PolyNbSides=12]\qquad \PstPolygon[PolyNbSides=20]
\end{document}
if you want only the connections on the circle use
\providecommand{\PstPolygonNode}{%
\psdots[dotscale=2](1;\INode)
\multido{\iA=0+1,\iB=1+1}{\numexpr\INode+1}{%
\psline[linecolor=blue!50](1;\iA)(1;\iB)}}
-
-
see my edited answer.– user2478Sep 27, 2015 at 6:06
You can set the dimensions in the Geogebra to PGF export
window before pressing the Generate PGF/TikZ
button. Set xMin
, xMax
, yMin
, and yMax
(the way you want) to set a selection area which includes the graphics you created in GeoGebra. (In this case, it is your circle.)