1
$\begingroup$

I generated an array of circles/disks with an hexagonal packing. Now I want to give each circle/disk a color depending on the value of a second list.

Table 1: Coordinates of disks

1.) (x1/y1)
2.) (x2/y2)
3.) (x3/y3)
4.) (x4/y4)
...

Table 2: Colors
(Blue)
(Red)
(Green)
(Blue)
...


NumberRows = 10;
NumberColumns = 10;
Table1 = Table[{0.5 + i, 0.5 + (2*j*Sqrt[1 - 0.5^2])}, {i, 0, 
    NumberColumns - 1}, {j, 0, ((NumberRows - 2)/2)}];
Table2 = Table[{i, 0.5 + ((1 + 2*j)*Sqrt[1 - 0.5^2])}, {i, 0, 
    NumberColumns - 1}, {j, 0, ((NumberRows - 2)/2)}];

TableCombined = Join[Table1, Table2];
g3 = Flatten[TableCombined, 1];

points = Graphics[{Blue , Disk[#, 0.5] & /@ g3}];

Show[points, Frame -> True]
$\endgroup$
1
  • $\begingroup$ Try disks = MapThread[{#1, Disk[#2, 0.5]}&, {listOfColors, g3}]; Graphics [disks, Frame -> True]. Note that listOfColors should have the same length as g3. $\endgroup$
    – MarcoB
    Commented Jun 18, 2018 at 13:55

1 Answer 1

1
$\begingroup$
NumberRows = 10;
NumberColumns = 10;
Table1 = Table[{0.5 + i, 0.5 + (2*j*Sqrt[1 - 0.5^2])}, {i, 0, 
    NumberColumns - 1}, {j, 0, ((NumberRows - 2)/2)}];
Table2 = Table[{i, 0.5 + ((1 + 2*j)*Sqrt[1 - 0.5^2])}, {i, 0, 
    NumberColumns - 1}, {j, 0, ((NumberRows - 2)/2)}];

TableCombined = Join[Table1, Table2];
g3 = Flatten[TableCombined, 1];

g4 = Partition[Flatten@Riffle[g3, {Blue, Red, Green}, {1, -2, 2}], 3];

Show[Graphics[{First@#, Disk[Rest@#, 0.5]}] & /@ g4, Frame -> True]

enter image description here

Or as @MarcoB suggests

listOfColors = Take[Flatten@Table[{Blue, Red, Green}, 35], Length@g3];

Graphics[MapThread[{#1, Disk[#2, 0.5]} &, {listOfColors, g3}], 
 Frame -> True]
$\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.