5
$\begingroup$

I have a notebook that generates a hexagonal grid graph and its vertex connectivity, coordinates, and numbers as an input for an FEA program in matlab. I simply want to add members to this shape (or any other shapes) after the graph has been generated. Here is an example of my chain of hexagons. enter image description here

Now, after the graph has been generated using this code:

HexagonalGridGraph2[{wide1_Integer?Positive, wide2_Integer?Positive, 
   wide3_Integer?Positive}, opts : OptionsPattern[Graph]] := 
 Module[{cells, edges, vertices}, cells =
   Flatten[
    Table[CirclePoints[{Sqrt[3] (1 j + k - 2) + Sqrt[3] (1 j + l - 2),
         3 k - 2 - 3 l}, {2, \[Pi]/2}, 6]*5/2, {j, wide1}, {k, 
      wide2}, {l, wide3}], 2];
  
  edges = Union[Sort /@ Flatten[Partition[#, 2, 1, 1] & /@ cells, 1]];
  
  vertices = Union[Flatten[edges, 1]];
  
  IndexGraph[
   Graph[UndirectedEdge @@@ edges, opts, 
    VertexCoordinates -> Thread[vertices -> vertices]]]
  ]
hc = HexagonalGridGraph2[{6, 1, 1}, VertexLabels -> "Index", 
  EdgeLabels -> "Index"]

I want to add in a little element on either side of this like so:

enter image description here

Ideally I would have the amount of unit shapes in a row and then add a vertex between nodes 25 and 26 as well as 2 and 5 with an edge proceeding outward from those nodes at a specified length.

Thank you!

$\endgroup$

1 Answer 1

7
$\begingroup$

You can add new vertices/edges using EdgeAdd, and use GraphEmbedding[hc] to compute the desired vertex coordinates for new vertices:

newverts = VertexCount[hc] + Range[4];

coords = {{-5, 0} + #, #, #2, {5, 0} + #2} & @@ 
   (Mean @ GraphEmbedding[hc][[#]] & /@ {{2, 5}, {25, 26}});

newedges = UndirectedEdge @@@ Partition[newverts, 2];

EdgeAdd[hc, newedges, 
 VertexCoordinates -> Join[GraphEmbedding[hc], coords], 
 EdgeStyle -> {Alternatives @@ newedges -> Directive[Thick, Red]}, 
 ImageSize -> 800]

enter image description here

If you want to add the red lines as annotation without changing the graph hc you use the option Epilog to add any desired graphics primitives:

Graph[hc, 
 Epilog -> 
  {Red, 
   Line[{#, Offset[{50, 0}, #]}] & @ Mean @ GraphEmbedding[hc][[{25, 26}]], 
   Green, 
   Line[{#, Offset[{-50, 0}, #]}] & @ Mean @ GraphEmbedding[hc][[{2, 5}]]},
 ImageSize -> 600, ImagePadding -> {{50, 50}, {3, 3}}]

enter image description here

You can replace Offset[{-50, 0}, #] (Offset[{50, 0}, #]) with Scaled[{-.1, 0},#] (respectively, Scaled[{.1, 0},#]) to get a similar result.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.