5
$\begingroup$

I am trying to put a series of disks at the center of each hexagon, but I could center only 1. How do I center all of the disks at the center of each hexagon?

Here is the code I am using.

h[x_, y_] := Polygon[Table[{Cos[2 Pi k/6] + x, Sin[2 Pi k/6] + y}, {k, 6}]]
p1 = Graphics[{EdgeForm[Opacity[.7]], White, Table[h[3 i + 3 ((-1)^j + 1)/4, Sqrt[3]/2 j], {i, 0, 1}, {j, 0,3}]}];
Show[p1, Table[Graphics[Disk[{i, j}, {0.25, 0.25}]], {i, 0, 2, 1.5}, {j, 0, 2, 1.5}]]

Here is my result:

enter image description here

$\endgroup$
1
  • $\begingroup$ I do this might seem trite and still, wouldn't the solution have been more obvious if you'd thought about "How to plot a disk in the center of each hexagon?" $\endgroup$ Jun 7, 2020 at 23:33

3 Answers 3

9
$\begingroup$

Just reuse the expression for the x and y components that you used when centering the hexagons:

Table[
 Graphics[Disk[{3 i + 3 ((-1)^j + 1)/4, Sqrt[3]/2 j}, {0.25, 0.25}]],
 {i, 0, 1},
 {j, 0, 3}
 ]

This gives you:

enter image description here

$\endgroup$
0
5
$\begingroup$
Clear["Global`*"]

You can use CirclePoints to define the Polygon

h[x_, y_] := Polygon[{x, y} + # & /@ CirclePoints[6]]

EDIT: As pointed out in the comment by J.M., using RegularPolygon this can be simplified to

h[x_, y_] := RegularPolygon[{x, y}, 1, 6]

The common center points are

ctrs = Table[
    {3 i + 3 ((-1)^j + 1)/4, Sqrt[3]/2 j},
    {i, 0, 1}, {j, 0, 3}] //
   Flatten[#, 1] &;

Map Polygon and Disk onto the centers.

Graphics[{
  EdgeForm[Opacity[.7]], {
     White, h @@ #,
     Black, Disk[#, 0.25]} & /@
   ctrs}]

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ Of course, your h[x, y] is the same as RegularPolygon[{x, y}, 1, 6]. $\endgroup$ Jun 6, 2020 at 15:52
3
$\begingroup$

1. You can post-process your p1 to add disks at RegionCentroid of polygons:

p1 /. p_Polygon:> {p, FaceForm[Black], Disk[RegionCentroid @ p, {1, 1} / 4]}

enter image description here

2. Alternatively, you can create a single disk/hexagon pair centered at {0, 0}

prims = {Black, Scale[Disk[], {1, 1}/4], 
   EdgeForm[Gray], FaceForm[], RegularPolygon[{0, 0}, 1, 6]};

and Translate it using centers:

centers = {3 # + 3 (1 + (-1)^#2)/4, Sqrt[3] #2/2} & @@@ 
   Tuples[{{0, 1}, Range[0, 3]}];


Graphics[Translate[prims, #] & /@ centers]

enter image description here

$\endgroup$

Your Answer

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

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