In a triangulated isometric grid, what triangle is a given point in?

What you want to do is turn this into a grid as much as possible because grids are far easier to work with.

The first thing you do is work out what column it’s in. You say you store that so it should be easier by doing a simple integer division on the x coordinate by the column width offset by the box start. Easy.

After that you want to work out what triangle it’s in (obviously). How you partially turn this into a grid is you pretend that you have a stack of right angle triangles instead of a stack of isometric triangles.

The triangles have a length along the y axis (the side of the column). Divide that number in two and work out how many steps down you are. Based on the number of steps down and if the column is even or odd will tell you if you’re looking at:

|-_      |
|  -_    |
|    -_  |
|      -_|

or the reverse. At this point you only need to determine which side of the line it’s on to determine which right triangle it’s in, which also tells you which isometric triangle it’s in.

You have a couple of options for this.

  1. You could use something like Bresenham’s line algorithm to rasterize the hypotenuse and when you hit the column you’re in work out if you’re above or below that line;
  2. Because you only have two possible grids here (one being the reverse of the other so it’s really only one). You could store a array of row values, saying that for column 3, the hypotenuse is at offset 2, whereas for 6 it’s at 4 and so on.

You could even use (1) to generate (2) as a fast lookup.

The only other thing to consider is what happens if the mouse cursor is on an edge?

Leave a Comment