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

1
  • 2
    Your question leaves all efforts to users here.
    – user31729
    Aug 4, 2015 at 18:24

4 Answers 4

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

9
  • ,@percusse random(360) seems to have the same seed in consecutive runs, how can we assure a different number in each run?
    – AboAmmar
    Aug 4, 2015 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 tex.stackexchange.com/questions/144621/…
    – percusse
    Aug 4, 2015 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 4, 2015 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 5, 2015 at 16:11
  • 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 8, 2015 at 9:53
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}
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.
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}
1
  • 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 4, 2015 at 21:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .