Hexフィールド





Siv3DApp.cpp
# include <Siv3D.hpp>

void Main()
{
  unsigned int mapwidth, mapheight ;
  double cellsize ;
  double cellint ;
  double padding ;

  GUI gui(GUISkin::Default()) ;
  gui.addSlider(L"Height", { 1, 100, 10}) ;

  while (System::Update()) {
    mapheight = gui.slider(L"Height").value ;
    cellsize  = Window::Height() / (0.5 + 1.5 * mapheight) ;
    cellint   = cellsize * sqrt(3) ;
    mapwidth  = (Window::Width() - 0.5 * cellint) / cellint ;
    padding   = (Window::Width() - (cellint * (0.5 + mapwidth + 0.5 * (mapheight == 1)))) / 2 ;

    Polygon hexcell = Geometry::CreateNgon(6, cellsize, 0, {0, 0}) ;
    Vec2 hexpos ;

    for (unsigned int y = 0; y < mapheight; ++y) for (unsigned int x = 0; x < mapwidth; ++x) {
      hexpos = {cellint * (x + 0.5 * (y%2 == 0) + 0.5) + padding, cellsize * (1.5 * y + 1)} ;
      (hexcell + hexpos).draw(Palette::Orange) ;
      (hexcell + hexpos).drawFrame(2.0, Palette::Red) ;
    }
  }
}