1
$\begingroup$

I'm new to mathematika. I need to access the hexagon by the index of the two-dimensional array that I set. This hexagon needs to be painted over, I was able to find how to set the grid only with a one-dimensional array, please help. I need to either make a two-dimensional array out of this one-dimensional array or even find another function to create a grid with the ability to access the desired hexagon

hexes = Keys@
   ResourceFunction["HextileBins"][
    Flatten[Table[{x, y}, {x, 0, 15}, {y, 0, 15}], 1], 2];
Graphics[{Red, Polygon[hexes[[80, 1]]], EdgeForm[Blue], 
  FaceForm[None], hexes}]
Polygon[hexes[[80, 1]]] 

that's just where the problem is that the array is one-dimensional

$\endgroup$

2 Answers 2

1
$\begingroup$
Clear["Global`*"]

$Version

(* "12.3.1 for Mac OS X x86 (64-bit) (June 19, 2021)" *)

hexes = Keys@
   ResourceFunction["HextileBins"][
    Flatten[Table[{x, y}, {x, 0, 15}, {y, 0, 15}], 1], 2];

Manipulate[
 Graphics[{
   Red, Polygon[hexes[[10*(column - 1) + row, 1]]],
   EdgeForm[Blue], FaceForm[None], hexes},
  ImageSize -> Small],
 {{row, 1}, Range[10, 1, -1],
  ControlType -> SetterBar,
  Appearance -> "Vertical",
  ControlPlacement -> Left},
 Row[{
   Spacer[70],
   Control[{{column, 1}, Range[8],
     ControlType -> SetterBar}]}]]

enter image description here

$\endgroup$
0
$\begingroup$

If I understand correctly, you'd like to be able to enter some 2D integer indices and return the Polygon on a hexagonal grid? Using HextileBins to do this is interesting (and in-fact there's a similar example of making hexagonal grids under Applications in the Resource Function's documentation).

Your code above can be massaged into achieving this, for example something like this:

xBins=8;
yBins=8;
xBinsPadded=Floor[2 Sqrt[3]/3 xBins];

hexes=Join@@@(Transpose/@Partition[GatherBy[
SortBy[Keys[ResourceFunction["HextileBins"][Flatten[Table[{x,y},{x,xBinsPadded},{y,yBins}],1],2Sqrt[3]/3]],Mean@*PolygonCoordinates],
First@*Mean@*PolygonCoordinates],2]);
Graphics[{Red,hexes[[7]],Green,hexes[[7,3]],EdgeForm[Blue],FaceForm[None],hexes}]

enter image description here

However, the main problem is that the hexagonal grid does not live on the cartesian grid, but rather uses hexagonal lattice vectors.

This is perhaps a more 'rigorous' way of achieving what you want:

  • First, define the hexagonal vectors
hexagonalLatticeVectors = {{1/2, Sqrt[3]/2}, {1/2, -Sqrt[3]/2}};
  • Then, we write a simple function to return a Polygon at a particular Cartesian location
hexagonAt[{x_, y_}] := Polygon[CirclePoints[{x, y}, {Sqrt[3]/3, \[Pi]/6}, 6]]
  • We can then loop over integer linear combinations of these lattice vectors to obtain a 2D hexagonal grid (note these are indexed using the hexagonal lattice vectors)
newHexes=Table[hexagonAt[{i,j}.hexagonalLatticeVectors],{i,8},{j,8}];
Graphics[{Red,newHexes[[7]],Green,newHexes[[7,2]],EdgeForm[Blue],FaceForm[None],Flatten[newHexes]}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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