I’m working on isometric projections at the day job this morning.  I had the world position of an entity (wx, wy) and had to figure out the grid coordinates (gx, gy) of said entity on an isometric grid which had individual tiles tw wide and th high.  I also already have the formulas at hand for the inverse problem of converting from grid coordinates (gx, gy) to world coordinates (wx, wy).  I’m sure this math exists in a dozen places online, but actually spending a couple minutes doing out the work always helps if it doesn’t quite work right and you find yourself trying to figure out why in the debugger.  So here’s what is written on the notepad on my desk:

Known Formulas
wx = (gx – gy) * tw
wy = (gx + gy) * th

Solve for gx
wx = (gx – gy) * tw
(gx – gy) = wx / tw
gx = (wx / tw) + gy

Solve for gy
wy = (gx + gy) * th
gx + gy = wy / th
gy = (wy / th) – gx

Re-solve for gx excluding gy
gx = (wx / tw) + (wy / th) – gx
gx * 2 = (wx / tw) + (wy / th)
gx = ((wx / tw) + (wy / th)) / 2

Re-solve for gy excluding gx
gy = (wy / th) – (wx / tw) + gy
gy * 2 = (wy / th) – (wx / tw)
gy = ((wy / th) – (wx / tw)) / 2

I’m sure it marks me as a huge nerd, but I find it so satisfying when I get to do this kind of thing as part of my regular employment.  Sweet, sweet justification for all those math classes.