How to draw line connecting points on circle

A pretty easy solution is to use Graph because there you can

  • create edges
  • position the vertices on a circle
  • label all vertices
  • put a circle on it

A somewhat condensed form of this idea is the following

drawMe[l_List] :=
 With[{ed = Range[Length[l]], dphi = 2 Pi/(Length[l])},
  Graph[ed, (UndirectedEdge @@@ 
        Subsets[Flatten[#], {2}]) & /@ (Position[l, #] & /@ 
       Union[l]) // Flatten, 
   VertexLabels -> (Rule @@@ Transpose[{ed, l}]),
   VertexCoordinates -> 
    Table[{Cos[phi], Sin[phi]}, {phi, 0, 2 Pi - dphi, dphi}], 
   Epilog -> {Circle[]}, ImagePadding -> 15]]

drawMe[{1, 2, 3, 4, 5, 1, 2, 3, 5, 4}]

Mathematica graphics

Btw, you can create nice things with this

drawMe@Table[Mod[i, 8], {i, 2^6}]

Mathematica graphics


This is how I would do it:

coords[list_] := 
  With[{offset = 2 Pi/Length[list]}, Array[{Cos[# offset], Sin[# offset]} &, Length[list]]]
lines[list_] := Line[coords[list][[#]] & /@ Partition[Ordering[list], 2]]
labels[labels_, scaling_] := MapThread[Text, {labels, scaling coords[labels]}]

b = {1, 2, 3, 4, 5, 1, 2, 3, 5, 4};
Graphics[{
  Circle[],
  lines[b],
  labels[b, 1.1]
  }]

gfx

It assumes there are two of each label. It would have to be adjusted to work for other cases. I think the labeling function will work for your case as well. What it does is take the coordinates of the vertices and multiply them by some constant, to put them further away from the middle.


list = {1, 2, 3, 4, 5, 1, 2, 3, 5, 4};

Using RelationGraph

RelationGraph[UnsameQ[##] && SameQ @@ list[[{##}]] &, Range@Length@list, 
  VertexCoordinates -> CirclePoints[Length@list], 
  VertexLabels -> (MapIndexed[#2[[1]] -> Placed[#, Center] &, list]), 
  GraphStyle -> "SmallNetwork", Prolog -> {Red, Circle[]}]

enter image description here

Using AdjacencyGraph

am = Outer[Boole[Equal@##] &, list, list] - IdentityMatrix[Length@list];
AdjacencyGraph[am, VertexCoordinates -> CirclePoints[Length @ list], 
 VertexLabels -> (MapIndexed[#2[[1]] -> Placed[#, Center] &, list]), 
 GraphStyle -> "SmallNetwork", Prolog -> {Red, Circle[]}]

enter image description here

Note: If you replace CirclePoints[Length @ list]with

Table[{Cos[2 Pi k /Length[list]], Sin[2 Pi k /Length[list]]}, {k, Length@list}]

the second approach also works in versions before 10.1,

Tags:

Graphics