# 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?

-
It is a straightforward task as TikZ provides a regular polygon shape (and you can customize the number of sides). On the manual there are some examples to draw a polygon inside a circle, but you can also have a look to Connect vertices of a regular poly by curved arrows –  Claudio Fiandrino Oct 4 '13 at 6:23
Start from here: texample.net/tikz/examples/regular-polygons –  alfC Oct 4 '13 at 6:26

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 Mardikian 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

with PSTricks:

\documentclass{article}
\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. –  Please don't touch Oct 4 '13 at 17:04

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;
d/.style={circle,fill,outer sep=1pt,inner sep=+0pt,minimum size=+3pt,#1},
c/.style={insert path={(C) edge[#1,to path={circle[]}] ()}},
\def\nodeRot{0}
\newcommand*\poly[2][]{%
\path (0,0) coordinate (C) [rotate/.append code={\def\nodeRot{##1}},#1]
\foreach \cnt[count=\Cnt from 0] in {1,...,#2} {
(c) [late options={alias=c'}] edge [<-, thick] (C)
(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}
\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

-

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! –  Please don't touch Oct 4 '13 at 19:45