0

I recently started using LaTeX/tikz and was wondering if anyone would be willing to help code (using tikz) a diagram similar to the attached. Note that the pentagon should not necessarily be regular and I would prefer the vertices not to be labelled. Many thanks. enter image description here

wrb98
  • 876

4 Answers4

5

If you need truly random then you probably accept that they can be in any weird position on the circle. Here is one way

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0,0) circle [radius=1cm];
\foreach\x in{1,...,5}\coordinate (penta-\x) at ({random(360)}:1cm);
\foreach \x in {1,...,4}{
  \foreach\y in {\x,...,5}{
  \draw (penta-\x) -- (penta-\y);
  }
}
\end{tikzpicture}
\end{document}

enter image description here

percusse
  • 157,807
  • ,@percusse random(360) seems to have the same seed in consecutive runs, how can we assure a different number in each run? – AboAmmar Aug 04 '15 at 18:51
  • 1
    @AboAmmar It's not the same seed but it's fixed to the date hour and the minute. Have a look at this one make it change more often http://tex.stackexchange.com/questions/144621/have-a-new-seed-in-pgfmath-more-often – percusse Aug 04 '15 at 18:57
  • 1
    Ready for code golfing ? \tikz\draw circle(1)(360*rnd:1)coordinate(1)foreach~in{2,...,5}{--(360*rnd:1)coordinate(~)}foreach~in{1,3,5,2,4,1}{--(~)}; – Kpym Aug 04 '15 at 20:39
  • @percusse in a strange way the copy/paste from the comment add an invisible character after {--(360*rnd:1)coordinate(~)}‌ and before ​foreach~in{1,3,5,2,4,1}{--(~)}; :( If you delete it, it works. – Kpym Aug 05 '15 at 16:11
  • @percusse Here is the answer why SX insert U+200C U+200B ! – Kpym Aug 05 '15 at 16:15
  • @Kpym Got it, thanks. \tikz\def\z{(360*rnd:1)coordinate}\draw circle(1)\z(1)foreach~in{2,...,5}{--\z(~)}foreach~in{1,3,5,2,4,1}{--(~)}; – percusse Aug 05 '15 at 16:31
  • @percusse one less \tikz\def\z{(360*rnd:1)coordinate(}\draw circle(1)\z1)foreach~in{2,...,5}{--\z~)}foreach~in{1,3,5,2,4,1}{--(~)}; ;) – Kpym Aug 05 '15 at 16:45
  • 1
    @Kpym a few less\tikz\draw circle(1)foreach~in{1,...,5}{(360*rnd:1)coordinate(~)}foreach~in{1,...,5,1,3,5,2,4,1}{--(~)}; – percusse Aug 08 '15 at 09:53
  • @percusse not only the shortest, but also the best. You should put this one in your answer ! – Kpym Aug 08 '15 at 14:34
3

I drew this exact, but you can set any random five angles if you wish.

enter image description here

\documentclass[tikz,border=2pt]{standalone}
\begin{document}

\begin{tikzpicture}[line cap=butt,outer sep=0pt]
\node[minimum size=3cm,circle,draw] (cirle) {};

\foreach \p[count=\i] in {0,72,144,216,288}
\coordinate(n-\i) at (cirle.\p);

\foreach \x in {1,...,4}{%
  \foreach \y in {2,...,5}{%
  \ifnum\y>\x\draw(n-\x)--(n-\y);\else\fi%
}}
\end{tikzpicture}

\end{document}
AboAmmar
  • 46,352
  • 4
  • 58
  • 127
2

For comparison here's an attempt in Metapost done with a single statement, albeit one featuring a loop-within-a-loop and the occasionally useful hide construct. I admit that this one is fairly obscure.

enter image description here

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);
draw for i=0 upto 4:
  hide(z[i] = point 8/5 i + 1/4 normaldeviate of fullcircle scaled 144;
  for j=i downto 0: draw z[i] -- z[j]; endfor) z[i] .. 
endfor cycle withcolor .67 red;
endfig;
end.
Thruston
  • 42,268
1

You can use the TikZ library shape, which defines various node shapes. I use the regular polygon shape, which draw a regular polygon. The number of sides is set by regular polygon sides to 5.

This library create also a series of anchor in node shapes, such as corner i where i is a integer. I use these anchors to draw the inner star.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}
\node(a)[draw,regular polygon,regular polygon sides=5,minimum size=2cm]{};
\draw circle (1cm);
\draw(a.corner 1) foreach\anchor in {3,5,2,4} { -- (a.corner \anchor)} -- cycle;
\end{tikzpicture}
\end{document}
skevin93
  • 516
  • To draw a complete regular graph it is probably simpler to use graphs,graphs.standard library with \graph[nodes={circle,fill,inner sep=1pt},empty nodes]{subgraph K_n[n=5,clockwise,radius=2cm]}; – Kpym Aug 04 '15 at 21:14