Here is a link to a graphing calculator to help you visualize what I'm talking about (I made it): https://www.desmos.com/calculator/ccxnopqqkr

Slide Hq and Hr to change the coordinates of the Red Hexagon (Axial coordinates, not Cartesian).

My question is, what formula would convert that black point (or any point in space) to the axial coordinates of the hexagon it falls into? Note, these are not for regular hexagons. If it falls directly on the border between two hexagons, I need it to consistently push (round) in the same direction every time, so that it I can be sure which hex a point will convert to.

The reason I need this is for a game I'm making (A simple board game). It's in an isometric view with a hexagon game grid. The reason the hexagon on that link is scaled down vertically is to match the hexagons in the game. It has the exact same coordinates (including the origin of the hexagon tile being the top-left).

  • $\begingroup$ Scale to make the hexagons regular and then apply your favorite Cartesian-to-hex-coordinate mapping from the many written up on the Internet. $\endgroup$ – amd Jan 2 '19 at 5:05

A formula will not be able to deal directly with the edge cases. To convert from $(X,Y)$ to $(H_q,H_r)$ you will need an algorithm. To begin with, imagine vertical lines draw through the top left points of the hexagons. This divides the grid into columns, and you need to know which column the point is in. This is simply $\lfloor\frac{x}{18}\rfloor$. We'll call this value $a$. We then divide that column into cells using the horizontal lines of the hexagons in that column so that each cell is an $18\times14$ rectangle with its top left corner concurrent with that of a hexagon, then find a value $b$ for the cell containing the given point.

When $a$ is even we calculate $b=\lfloor\frac{-y}{14}\rfloor$, and when $a$ is odd $b=\lfloor\frac{-y-7}{14}\rfloor$.

Using these values we find $H_r=b-\lfloor\frac{a}{2}\rfloor$ and $H_q=H_r+a$. Also use these values to find the $(x,y)$ values for the top left corner $P$ of the cell.

When $a$ is even $P_{x,y}=(18a,-14b)$, when $a$ is odd $P_{x,y}=(18a,-14b-7)$.

Now check the location of the given point within the cell to see if it falls in one of the corner regions.

First let $T_x=X-P_x-11$. If this value $T_x\le0$ then the point is in the main region and our values for $H_q$ and $H_r$ are correct. Otherwise, let $T_y=P_y-Y$.

Finally, if $T_y<7$ and $T_x>T_y$ we need subtract $1$ from $H_r$, or if $T_y>7$ and $T_x>14-T_y$ we need to add $1$ to $H_q$.

C code reference (tested and confirmed):

  • $\begingroup$ Hi, I tried implementing that as an algorithm but it seems to return the wrong result. I set my mouse coordinates to (-16,-41) (My map is offset to position (64,64) so this is correctly on-screen, but that shouldn't matter as I subtract 64 from the mouse's x and y positions). At (-16,-41) for X,Y, it returns an Hq of 1 and an Hr of 2, when the expected values should be Hq = 2 and Hr = 3. $\endgroup$ – user91670 Jan 3 '19 at 14:46
  • $\begingroup$ @user91670 After further inspection, I have edited the answer to correct two errors. For odd $a$, $b=\lfloor\frac{-y-7}{14}\rfloor$ and for all $a$, $H_r=b-\lfloor\frac{a}{2}\rfloor$. $\endgroup$ – Daniel Mathias Jan 3 '19 at 16:46
  • $\begingroup$ Similar correction for $P_y$ when $a$ is odd: $P_y=-14b-7$ $\endgroup$ – Daniel Mathias Jan 3 '19 at 16:54
  • $\begingroup$ @user91670 My apologies for not writing and testing code in the first place. The previously mentioned changes have been made, as well as the condition $T_x>T_y$ when $T_y<7$. See changes in post. $\endgroup$ – Daniel Mathias Jan 3 '19 at 22:08
  • $\begingroup$ The edit fixed all of the issues. Thank you very much! $\endgroup$ – user91670 Jan 3 '19 at 23:20

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.