# Inner and outer circles of a polygon in LaTeX

How do I draw a circle and its surrounding polygons, for example an octagon, in LaTeX?

-

## migrated from stackoverflow.comMar 7 '11 at 4:00

This question came from our site for professional and enthusiast programmers.

You might want to look at the answers to the following question: Is there an easy way to TeX geometric pictures? – Alan Munn Mar 7 '11 at 5:08

How shall I draw thee? Let me count the ways!
I can draw thee with TikZ and PSTricks and pictures

... but there my scansion leaves me and I resume normal operating procedures.

Edited: My original method was using node shapes, but that was simultaneously posted by Debilski above so here's another TikZ method. (I'll leave my original pictures in, though). This method is a bit more like TikZ's "circle" and "rectangle" commands (though not quite) in that it looks a bit as though there is a generic polygon shape, though in reality it is just a regular LaTeX command that expands to the correct drawing instructions. The arguments to the command are the number of sides and the inner radius.

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\polygon}[2]{%
let \n{len} = {2*#2*tan(360/(2*#1))} in
++(0,-#2) ++(\n{len}/2,0) \foreach \x in {1,...,#1} { -- ++(\x*360/#1:\n{len})}}

\begin{document}

\begin{tikzpicture}
\draw[red,dashed] (0,0) circle (1);
\draw (0,0) \polygon{8}{1};
\draw[red,dashed] (3,0) circle (1);
\draw (3,0) \polygon{4}{1};
\draw[red,dashed] (6,0) circle (1);
\draw (6,0) \polygon{7}{1};
\end{tikzpicture}

\end{document}


Result:

The following is from my original answer, as noted above that was the same as that given by Debilski, but as that answer didn't include pictures I'm leaving these here.

-

There is an example in the TikZ/PGF manual:

\documentclass{minimal}
\usepackage{tikz}
\usepgflibrary{shapes}
\begin{document}
\begin{tikzpicture}
\draw[dashed] (10,0) circle (1cm);
\node[regular polygon, regular polygon sides=8, draw, inner sep=0.707cm] at (10,0) {};
\end{tikzpicture}
\end{document}

-
Also in the same manual on page 474, the through library makes it very easy to draw a circumcircle using the circle through option. That way you don't need to specify a radius rather leave it to tikz to calculate what the radius is. This is particularly helpful when scaling the figure. – percusse Mar 7 '11 at 13:16

In Metapost:

  \documentclass{article}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
beginfig(1)
pair vert[];
for i=0 upto n:
endfor;
draw for i=0 upto n-1: vert[i] -- endfor cycle withcolor red;
% polygon
draw for i=0 upto n-1: vert[i] .. endfor cycle withcolor green;
% outer circle
draw for i=0 upto n-1: 1/2*(vert[i]+vert[i+1]) .. endfor cycle withcolor blue;
% inner circle, using midpoints.
endfig;
\end{mplibcode}
\end{document}


which makes use of mplib, only available in Luatex, so you have to compile this using lualatex. It gives:

The nice thing about this way of doing things is that you calculate the interior circle using midpoints of lines, rather than with magic numbers or trigonometric identies, giving code that is more obviously correct.

-

Another way, if you want to use the vertices :

\documentclass[a4paper]{article}
\usepackage{tikz}

% #1 : number of sides
% #2 : radius of the circumscribed circle
% #3 : Prefix of vertices
% #4 : angle for a rotation around the center
% The macro defines only the vertices

\newcommand\RegPolygon[4]{%
\foreach \n in {1,...,#1}{%
+ (#4+\n*360/#1:#2) coordinate (#3\n)}}

% Draw the polygon
% #1 : options
% #2 : number of sides
% #3 : the prefix

\newcommand\DrawPolygon[3][]{%
\def\s{1}
\draw[#1] (#3\s)
\foreach \s  in  {2,...,#2}{--(#3\s)}--cycle;
}

% macro to draw the inscribed circle
% #1 : number of sides
% #2 : : radius of the circumscribed circle

\newcommand\DrawCircleIns[2]{%
circle ({#2*cos(180/#1)})
}

\begin{document}

\begin{tikzpicture}
\path (1,1)  \RegPolygon{5}{3}{S}{18};
\DrawPolygon[red,thick]{5}{S}
\draw (1,1) circle (3);
\path (1,-6)  \RegPolygon{3}{3}{B}{30};
\DrawPolygon[red,thick]{3}{B}
\draw (1,-6) circle (3);
\draw (1,1) \DrawCircleIns{5}{3};
\end{tikzpicture}

\end{document}


The vertices of the pentagon are names S1,S2, etc. and the vertices of the triangle are B1,B2 and B3

-
How can I label vertices? – minthao_2011 Aug 7 '15 at 1:43

You might have better luck on the version of this site devoted to tex: http://tex.stackexchange.com

nonetheless, you should try using the PSTricks package and drawing whatever shape you need point by point or arc by arc.

For example, here is how you can draw a triangle and a circle:

\begin{pspicture}(6,6)
%% Triangle in red
\psline[linecolor=red](1,1)(5,1)(1,4)(1,1)

%% Circle in blue with radius 1:
\pscircle[linecolor=blue](3,2.5){1}
\end{pspicture}


For other n-sided polygons, use something similar to the triangle above, plotting a point for each vertex in the shape.

-

An easy pstricks solution, to have a circle with an inscribed and a circumscribed pentagons. It uses the pst-poly package:

\documentclass[12pt, x11names, border=1pt]{standalone}

\usepackage{pst-poly}
\usepackage{auto-pst-pdf}
\usepackage{fp}%
\def\R{1.09}
\begin{document}
\psset{unit = 2cm, dimen =middle}
\begin{pspicture}
\psset{ linewidth = 0.6pt, dimen =inner}
\rput{-22.5}{\PstOctogon[PolyName =A, linecolor =DarkOliveGreen3]}
\pscircle[linecolor=IndianRed3](A0){1}
\psdot[dotstyle = o, dotsize=1.5pt,fillstyle = solid, fillcolor=IndianRed3](A0)
\makeatletter\psset{unit=\R, linewidth =0.6pt, linecolor=Gold1}\makeatother%
\rput(A0){\PstOctogon[PolyName=B,]}
\psset{linestyle=dashed, linewidth=0.3pt}
\psline[ linecolor=DarkOliveGreen3 ](B8)(A0)(B1)\psline[ linecolor= Gold1](A0)(A1)
\end{pspicture}

\end{document}


-