# Drawing a regular polygon encompassed by a circle

I would like to replicate the sort of diagrams shown below - a regular polygon in dashed lines encompassed by a solid lined unit circle with vectors going from the origin to each labelled vertex in an xy-plane. How would one go about doing this in TikZ? ## 5 Answers

With TikZ

\def\deg{120}                   % for triangle
\def\p{3}                       % vertices
\begin{tikzpicture}[scale=2]
\draw [<->] (-1.5,0)--(1.5,0);
\draw [<->] (0,-1.5)--(0,1.5);
\draw (0,0) circle (1);

\foreach \t/\x in {0/0*\deg,1/1*\deg, 2/2*\deg}
{\draw[thick,->] (0:0)--(\x:1) node [] (\t) at (\x:1){};
\node[anchor=center] at (\x:1.2) {$w^{\t}_\p$};}

\foreach \from/\to in {0/1,1/2,2/0}
{\draw [thin, dashed] (\from) -- (\to);}

\end{tikzpicture}

\def\deg{40}                    % for convex hull, phase angle
\def\p{9}                       % vertices
\begin{tikzpicture}[scale=2]
\draw [<->] (-1.5,0)--(1.5,0);
\draw [<->] (0,-1.5)--(0,1.5);
\draw (0,0) circle (1);
\foreach \t/\x in {0/0*\deg, 1/1*\deg, 2/2*\deg, 3/3*\deg, 4/4*\deg, 5/5*\deg,
6/6*\deg, 7/7*\deg, 8/8*\deg}
%\foreach \t[evaluate=\t as \x using int(\t*\deg)] in {0,1,...,8} % suggested by Claudio Fiandrino
{\draw[thick,->] (0:0)--(\x:1) node[] (\t) at (\x:1) {};
\node[anchor=center] at (\x:1.2) {$w^{\t}_\p$};}
\foreach \from/\to in {0/1, 1/2, 2/3, 3/4, 4/5, 5/6, 6/7, 7/8, 8/0}
{\draw [thin, dashed] (\from) -- (\to);}
\end{tikzpicture}


This is the result • Nice :) What about using \foreach \t[evaluate=\t as \x using int(\t*\deg)] in {0,1,...,8} to simplify the loop code? – Claudio Fiandrino Oct 4 '13 at 10:38
• @ClaudioFiandrino -- Yes, indeed. That simplifies the loop. Did not think of using "evaluate ... as ...". Thank you for the suggestion. Appreciated. – Jesse Oct 4 '13 at 11:06
• How would I change the starting degree? For example, how would I shift the first diagram by 60 degrees? – Alex Oct 4 '13 at 14:54
• @AlexMardikian -- Replace \deg by \deg+60, since my design starts from degree 0 due to \t=0. Or, better yet, define a new variable \def\shift{60} and applies \deg+\shift. – Jesse Oct 4 '13 at 15:17

Just another way with TikZ.

It is possible to use more styles and have a more flexible setup (you cannot use a or c more than once without drawing it only once). It is possible to draw ellipses by using the x radius and y radius key.

I don’t like the the implementation via \nodeRot (could just as well be a key). But that’s the easiest way to allow rotations of the polygon with the rotate key and correctly placed labeling of the corners without rotated nodes and without using PGF tricks.

## Code

\documentclass[tikz]{standalone}
\tikzset{declare function={angleForPoly(\i,\n,\d)=360/\n*\i+\d;
x_radius              =\pgfkeysvalueof{/tikz/x radius};
y_radius              =\pgfkeysvalueof{/tikz/y radius};},
d/.style={circle,fill,outer sep=1pt,inner sep=+0pt,minimum size=+3pt,#1},
c/.style={insert path={(C) edge[#1,to path={circle[]}] ()}},
a/.style={insert path={(C)+(left:x_radius+.5cm) edge[#1,<->] +(right:x_radius+.5cm)
(C)+(  up:y_radius+.5cm) edge[#1,<->] +( down:y_radius+.5cm)}}}
\def\nodeRot{0}
\newcommand*\poly[]{%
\path (0,0) coordinate (C) [rotate/.append code={\def\nodeRot{##1}},#1]
++ ({angleForPoly(0,#2,0)}:x_radius and y_radius) coordinate[d] (c)
\foreach \cnt[count=\Cnt from 0] in {1,...,#2} {
(c) [late options={alias=c'}] edge [<-, thick] (C)
coordinate[d] (c) at ({angleForPoly(\cnt,#2,0)}:x_radius and y_radius)
(c') edge[dashed] (c)
\ifnum\Cnt>0 node[anchor={angleForPoly(\Cnt,#2,180+\nodeRot)},circle]
{$\omega\ifnum\Cnt>1^{\Cnt}\fi_{#2}$} \fi
};}
\begin{document}
\begin{tikzpicture}[radius=2cm,>=latex]
\matrix {
\poly[a,c]{3} & \poly[a=red,c,rotate=30]{4} & \poly {5} \\
\poly[c]  {6} & \poly                   {7} & \poly {8} \\
\poly     {9} & \poly                  {10} & \poly{11} \\
};
\end{tikzpicture}
\end{document}


## Output with PSTricks:

\documentclass{article}
\usepackage{pstricks-add}
\usepackage[margin=5mm]{geometry}
\pagestyle{empty}\parindent=0pt
\begin{document}

\psset{unit=2}
\multido{\iA=3+1}{20}{%
\begin{pspicture}(-1.3,-1.3)(1.3,1.3)
\degrees[\iA]\pscircle{1}% set unit for a circle to 3,4,5,... instead of 360
\psaxes[labels=none,ticks=none](0,0)(-1.1,-1.1)(1.1,1.1)
\multido{\iB=0+1,\iC=1+1}{\iA}{%
\psline[linestyle=dashed,showpoints](1;\iB)(1;\iC)
\psline[arrowscale=2,linewidth=1pt]{->}(1;\iB)
\rput(1.15;\iB){$\omega_{\iA}^{\iC}$}}
\end{pspicture} }

\end{document} • We already have \psnline, \psncurve, \psnccurve but \psnpolygon is missing. – kiss my armpit Oct 4 '13 at 17:04

Just for fun with PSTricks.

\documentclass[pstricks,border=20pt]{standalone}
\usepackage{pst-node,pst-plot}

\makeatletter

\def\Atom#1{%
\begin{pspicture}(-2,-2)(2,2)
\psaxes[labels=none,ticks=none]{->}(0,0)(-2,-2)(2,2)[$x$,0][$y$,90]
\pscircle[dimen=medusa]{1.75}
\degrees[#1]
\curvepnodes[plotpoints=\numexpr#1+1]{0}{#1}{1.75 t .5 add \pst@angleunit PtoC}{P}
\psnline[linecolor=lightgray,linestyle=dashed](0,\Pnodecount){P}
\multido{\i@=0+1}{\Pnodecount}{\psline[linecolor=blue]{->}(P\i@)\uput[\i@](P\i@){\scriptsize$\omega_{#1}^{\the\numexpr\i@+1}$}}
\end{pspicture}}

\makeatother

\begin{document}
\multido{\i=3+1}{10}{\Atom{\i}}
\end{document} • It is better to append @ for internal looping counters (e.g., \i@) to avoid interfering other external counters. It is difficult to debug! – kiss my armpit Oct 4 '13 at 19:45

Just for fun (some years later :-): Qrrbrbirlbel's diagram, but this time with MetaPost in a LuaLaTeX program:

\documentclass{standalone}
\usepackage{luamplib}
\mplibsetformat{metafun}
\mplibtextextlabel{enable}
\everymplib{verbatimtex \leavevmode etex;

vardef circled_polygon(expr u, n) =
clearxy; save xmin, xmax, ymin, ymax, angl, polygon; path polygon;
xmax := 1.2u; xmax = ymax = -xmin = -ymin; angl := 360/n; z0 = u*right;
polygon = z0 for i = 1 upto n-1: hide(z[i]= z[i-1] rotated angl) -- z[i] endfor -- cycle;
draw polygon dashed evenly; draw fullcircle scaled (2u);
draw (xmin, 0) -- (xmax, 0); draw (0, ymin) -- (0, ymax);
drawoptions(withpen pencircle scaled 1bp);
drawarrow origin -- z0; label.urt("$1$", z0);
drawarrow origin -- z1; freelabel("$\omega_{" & decimal n & "}$", z1, origin);
for i = 2 upto n-1:
drawarrow origin -- z[i];
freelabel("$\omega_{" & decimal n & "}^{" & decimal i & "}$", z[i], origin);
endfor;
drawoptions();
enddef;

beginfig(0);}

\everyendmplib{setbounds currentpicture to boundingbox currentpicture enlarged 3bp;
endfig;}

\begin{document}
\begin{mplibcode}
u = 2cm;
for i= 1 upto 3:
for j = 1 upto 3:
draw image(circled_polygon(u, 3i+j-1)) shifted ((2u+1.5cm)*(j-1, -i+1));
endfor;
endfor;
\end{mplibcode}
\end{document} 