News for hackers. Stuff that matters.
Re:Hexagonal Grids (2013) by amitp on Monday February 18, 2019 @02:04AM (#19189050)
A surprise to see my page on HN! For those of you curious about the tech:

Diagrams are in SVG. Canvas would be faster but SVG is easier for me to work with, especially for attaching mouse events to each hexagon. It also automatically scales to high dpi displays. With SVG and HTML accessed the same way, I can use the same code for updating text/samples as I do for updating diagrams. This includes interpolating values as noted by anchpop.

I don't use a build step for the JS code. It's just <script> tags, like it's 1999! You can View Source and see them all. I realize it'd be better if I minified bundled etc. but I'm lazy and this was easier. The page is implemented differently from a "single page app". For that, you might want something like JSX so that you can stay on the JS side, and output HTML+CSS as needed. For this kind of page, I mostly write HTML+CSS, and I tap into JS when needed using Vue.js. For example, I want the text "w = 2 * size" to be next to the rest of the text for that paragraph, instead of in a separate JS file:

       <p>
          Next we want to put several hexagons together.
          In the {{layout}} orientation, a hexagon 
          has width
          <code v-if="layout === 'flat'">w = 2 * size</code>
          <code v-else="">w = sqrt(3) * size</code>
          …
        </p>
I do use a build step for the HTML+SVG. Although the page works without this step (see pre-index.html), the user experience is a little bit better if I generate it on the server. I use cheesy pre-rendering for this: "$(CHROME) --headless --disable-gpu --dump-dom". It means more bytes :( but having all the HTML/SVG load at once is better for linking to a section, hitting the back button, etc. You don't lose the scroll position. Bonus: the page loads if you have Javascript turned off!

The static SVG on the server doesn't include the interactive parts. I use InteractionObserver to replace the static diagram with an interactive one as soon as you scroll to that section of the page.

I said I don't have a JS build step, but that's not quite true. The core hexagon algorithms were originally implemented in Haxe, compiled to Javascript. At the time (2013), I was curious about Haxe as an alternative to Javascript. Because I was either bored or crazy or both, in 2015 I implemented a code generator that used Haxe macros to parse the algorithms in Haxe-ish syntax[1] and then generate C++, Javascript, Python, and other output. The Javascript output from that is what now powers the page. Well, it's even more convoluted than that. The Haxe code generates Typescript[2], which I turn into Javascript[3]. I don't know what I was thinking! Well, I do — I wanted my readers to be able to use my unit-tested hexagon algorithms even if they were using a different programming language.

I wrote [4] describing the process of making an interactive page like this with D3.js. Check out [5] for a collection of interactive explanations from other people.

It's unlikely that you will print the page, but if you do, it will show endnotes for all the links so that you can see the URLs.[6]

[1] https://www.redblobgames.com/grids/hexagons/codegen/Codegen.... [2] https://www.redblobgames.com/grids/hexagons/codegen/output/l... [3] https://www.redblobgames.com/grids/hexagons/codegen/output/l... [4] https://www.redblobgames.com/making-of/line-drawing/ [5] https://explorabl.es/ [6] https://simblob.blogspot.com/2018/12/printing-my-pages.html

Re:Hexagonal Grids (2013) by sharpercoder on Monday February 18, 2019 @04:30AM (#19189538)
Your page is not only awesome in creating my Catan clone, but also very useful when communicating on it with others. You have set the gold standard or the defacto standard.

If you're interested in my implementation: https://github.com/generateui/jsettlers-web/blob/master/src/... https://github.com/generateui/jsettlers-web/blob/master/src/... https://github.com/generateui/jsettlers-web/blob/master/src/... https://github.com/generateui/jsettlers-web/blob/master/src/...

A hex has a coord. A coord has 6 nodes and 6 edges. An Edge can be created from 2 coords or 2 nodes. A node can be created from 3 edges or 3 coords. The implementation is immutable and memoized.

Re:Hexagonal Grids (2013) by maxnoe on Monday February 18, 2019 @01:44PM (#19193064)
Hey,

I'm an astrophysicist in high energy gamma ray astronomy.

Or telescope sensors have hexagonal pixels.

You website is awesome and helped a lot writing the coordinate trafos and other stuff. We always recommended it to everyone involved.

One switch I always wished it had was to switch to a cartesian coordinate system where x points right, y points up and negative coordinates are allowed.

Re:Hexagonal Grids (2013) by amitp on Tuesday February 19, 2019 @12:01AM (#19196720)
Cool! Glad the page helped! On the implementation page the code supports both y-up and y-down, as well as negative coordinates. One of these days I should update the diagrams on the main page to support this too.
Re:Hexagonal Grids (2013) by yellowapple on Monday February 18, 2019 @05:14PM (#19194971)
What drove the decision to go with hexagonal pixels? Sounds pretty crazy and cool.
Re:Hexagonal Grids (2013) by maxnoe on Tuesday February 19, 2019 @02:04AM (#19197192)
Historically, the "pixels" are mostly round photomultiplier tubes. So the natural way to place those is a hexagonal grid.

     o o
    o o o
     o o
   
The pixels are very sensitive, we can register single photons with a time resolution of half a nano second.

Quite recently, some telescopes started using silicon photo multipliers, which are mostly square by design.

But: The first telescope to use those also used light concentrators to increase the detector area and these have hexagonal entrance windows, so we also have hexagonal pixels.

Physicists like symmetries, and light concentrators are more effective when round than square, hexagonal is a good middle ground.

Re:Hexagonal Grids (2013) by yellowapple on Tuesday February 19, 2019 @12:38PM (#19200907)
Thanks for explaining! Makes sense.
Re:Hexagonal Grids (2013) by timvdalen on Monday February 18, 2019 @03:20AM (#19189265)
Thanks a lot for your website (an in particular this page and the hexagonal terrain generator page). I use it for small side projects quite often and I've actually re-implemented the terrain generator in C++ for a school project[1][2], with a 3D renderer for generated worlds[3].

[1]: https://github.com/H/2IV06-report [2]: https://github.com/Heightened/2IV06-map-generator [3]: https://github.com/Heightened/2IV06-map-viewer

Re:Hexagonal Grids (2013) by Longwelwind on Monday February 18, 2019 @05:56AM (#19189876)
Your guide is the first thing I open in my browser every time I need to code an algorithm in a hexagonal space.

Thanks a lot for the amazing work!

Re:Hexagonal Grids (2013) by rishav_sharan on Monday February 18, 2019 @02:48AM (#19189168)
Hey Amit

A big thanks for your site. As someone who fiddles around with small hobbyist game projects, your site is the defacto bible for me.

I was wondering if you plan on writing something on game AI. specially where the AI has infinite permutations of actions that they can take. How do these game AIs actually formulate a list of possible actions and how do they prioritise them?

Re:Hexagonal Grids (2013) by amitp on Tuesday February 19, 2019 @12:03AM (#19196731)
Thanks! I've found that my best pages are topics from real projects I've worked on, and I haven't worked on many game AI systems. I might have pointers to papers or web sites for you; feel free to email me at [email protected]
Re:Hexagonal Grids (2013) by appleflaxen on Monday February 18, 2019 @06:18AM (#19189964)
> . I use cheesy pre-rendering for this: "$(CHROME) --headless --disable-gpu --dump-dom". It means more bytes :( but having all the HTML/SVG load at once is better for linking to a section, hitting the back button, etc. You don't lose the scroll position.

this is an excellent tip! thanks for the awesome technique.

Re:Hexagonal Grids (2013) by ISV_Damocles on Monday February 18, 2019 @10:49AM (#19191478)
Thank you so much for your wonderful documentation. The H3 team at Uber used it so much for so many things. :)
Re:Hexagonal Grids (2013) by amitp on Tuesday February 19, 2019 @12:02AM (#19196727)
Wow, awesome to hear it!
Re:Hexagonal Grids (2013) by invalidusernam3 on Monday February 18, 2019 @04:34AM (#19189555)
You have no idea how helpful this page has been for me in the development of my hex game. Chuck a donate link on there, I would love to buy you a few beers to say thank you
Re:Hexagonal Grids (2013) by johnmarinelli on Monday February 18, 2019 @06:12AM (#19189937)
I used your site to implement A* as a learning exercise, and I learned a lot from it. Thanks!
Re:Hexagonal Grids (2013) by timvisee on Monday February 18, 2019 @06:13AM (#19189944)
Have used the page maby times. It's brilliant, and perfectly explains. Thanks a lot!
Re:Hexagonal Grids (2013) by syspec on Monday February 18, 2019 @02:50AM (#19189172)
Love all the content on your site, keep writing more new things please!
Re:Hexagonal Grids (2013) by amitp on Tuesday February 19, 2019 @12:05AM (#19196737)
Thanks! I'm keeping a list of ideas here: https://trello.com/b/mjOSMtsi/2019 but I'm not working on much at the moment. I needed a break after mapgen4 (2017-2018).
Re:Hexagonal Grids (2013) by phoe-krk on Monday February 18, 2019 @04:53AM (#19189634)
It's not the first time it is here.

https://news.ycombinator.com/item?id=19188932