8
\$\begingroup\$

I'm trying to figure out the optimal width and height (in pixels) to start building hex tiles for game development. My preference is for "flat topped" hex grids, but the math is similar for both.

I am looking for an "optimal" tile size that allows both the width and the height of the tile to be a rounded pixel number, based on the fact that height = sqrt(3)/2 * width.

My math skills being virtually nonexistent, I just ran a brute force script that ran through widths from 1 to 1024 and did not come up with a single value for w where h was an integer. Is this really the case? How does anyone create pixel-perfect hex tiles if there's no even width & height size that can accommodate a perfect hex aspect ratio?

\$\endgroup\$
  • 7
    \$\begingroup\$ This is not important to gameplay. It is a form of procrastination. If it very important to you, look for the closest match instead of an actual fit. \$\endgroup\$ – wolfdawn Oct 19 '14 at 6:25
  • 1
    \$\begingroup\$ You said "pixel", right? So you're talking about programming? Internally, you would work with ints to say which cell you're in (there should be online resources about hex grids), and the drawing of the lines will be done by the computer. (Think: You can't draw a circle, either.) \$\endgroup\$ – leewz Oct 19 '14 at 20:24
  • 1
    \$\begingroup\$ If you're a curious type then by all means read this where it says "Proof by infinite descent". Just Ctrl + f to find it. \$\endgroup\$ – wolfdawn Oct 20 '14 at 10:27
  • 1
    \$\begingroup\$ @Zehelvion haha and NOW I know what you mean by "procrastination" - I just spend the last 2 hours shaving the irrational numbers yak, and NOT creating a hex-tile based game. \$\endgroup\$ – Tom Auger Oct 20 '14 at 15:49
  • 1
    \$\begingroup\$ That must be quite a yak, since its fleece go on and on when represented decimally and never repeat the same pattern (really) . I didn't remember that reference from Ren & Stimpy; it's good to know. :) \$\endgroup\$ – wolfdawn Oct 21 '14 at 3:40
16
\$\begingroup\$

No. √3 is an irrational number, and by definition an irrational number can not be used as a ratio between two natural numbers (integers) such as pixel counts.

However, there is no rule that says you have to use ideal hexagons in your game tiles. If you approximate it closely and avoid any miscalculations that may result, which you should be able to do with integer math anyway, you can get a good-looking product while working with easy numbers behind the scenes (if you can call 100 and 173 easy to work with).

\$\endgroup\$
  • \$\begingroup\$ Nice, but √3 is the irrational number sqrt(3)=1.7320508075688772 is (say) a double, and can certainly be expressed as a ratio of integers (138907099/80198051). \$\endgroup\$ – Sean D Oct 19 '14 at 23:10
  • \$\begingroup\$ @SeanD Any number represented as a double or a float is a rational number. I don't see where you are going with this? \$\endgroup\$ – wolfdawn Oct 20 '14 at 10:30
  • \$\begingroup\$ NaNs are doubles, but they aren't rational. The answer claims "sqrt(3) is an irrational number" which is false in the context of programming, I was trying to draw a distinction between computer numbers and the real numbers. \$\endgroup\$ – Sean D Oct 20 '14 at 10:47
  • \$\begingroup\$ @SeanD Good point, computers store a close rational approximation of irrational numbers. In fact, for most rational numbers, computers store a close rational approximation also. So you could have a "perfect" hexagon in terms of the limited computer precision. We can only store 2^(numOfBits) of possible numbers within memory and there is an infinite amount of rational numbers between 0 .. 1, let alone irrational numbers of which there is greater infinite amount. \$\endgroup\$ – wolfdawn Oct 20 '14 at 10:48
  • 2
    \$\begingroup\$ Thanks for locating the √ character for me; I'll incorporate it into my answer so we don't need to argue about floating point precision. \$\endgroup\$ – Seth Battin Oct 20 '14 at 13:49
4
\$\begingroup\$

Just in case anyone is interested:

Lets assume sqrt(3) is rational:

  1. Therefore, there must be two integral numbers a and b such that a/b = sqrt(3)
  2. We assume these numbers are coprime, if they have a common factor, we divide by it producing a coprime pair, a and b
  3. We know that (a/b)^2 = 3 and therefore a^2 = 3 * b^2.
  4. 3 * b^2 is devisible by 3 as b^2 is integral and therefore a^2 is also devisible by 3.
  5. There are not integral numbers square is devisible by 3, but they are not. so it follows that a itself is devisible by 3. Lets define k = a/3.
  6. a^2 = (3k)^2 = 3 * b^2 => 9 * k^2 = 3 * b^2 = > 3 * k^2 = b^2 which means that b is also devisible by 3.
  7. This contradicts the base assumption that they are coprime integers.

Credits to wikipedie for refreshing my memory.

\$\endgroup\$
  • \$\begingroup\$ Show off! ;-) +1 for refreshing my memoey \$\endgroup\$ – Pieter Geerkens Oct 21 '14 at 3:28
  • \$\begingroup\$ @PieterGeerkens :) thanks, I managed to remember half of it (from Calculus 1) but then found it was explained real well well in wiki. \$\endgroup\$ – wolfdawn Oct 21 '14 at 3:30
1
\$\begingroup\$

Lots of complex answers here. If you are looking for a 'Close enough' answer, try 7x8. Not a perfect hexagon, but close enough that most people will not notice the difference.

\$\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.