Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
2  
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
add comment

migrated from stackoverflow.com Mar 7 '11 at 4:00

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

5 Answers

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:

circles and polygons again


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.

circles and polygons

share|improve this answer
add comment

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}
share|improve this answer
    
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
add comment

Rewritten answer — I had missed point about inscribed circle

In Metapost:

  \documentclass{article}
  \usepackage{luamplib}
  \begin{document}
  \begin{mplibcode}
  beginfig(1)
    pair vert[];
    n:=5; radius=3cm;
    for i=0 upto n: 
        vert[i] = radius * dir(360*i/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:

enter image description here

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.

share|improve this answer
add comment

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}

enter image description here

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.