I do believe this answer is correct, except for me, I didn't need the corrected.x --;
But I needed JavaScript for AfterEffects, so here, let me provide the JavaScript version.
function coordinatesForHexAtPoint(r, a, b)
{
// Get basic hex information - pseudocode
var radius = r;
// Estimate the most likely hex and round to nearest values
var x = 2.0/3.0*a/radius;
var z = (1.0/3.0*Math.sqrt(3.0)*b-1.0/3.0*a)/radius;
var y = -x-z;
var ix = Math.round((Math.floor(x-y)-Math.floor(z-x))/3.0);
var iy = Math.round((Math.floor(y-z)-Math.floor(x-y))/3.0);
var iz = Math.round((Math.floor(z-x)-Math.floor(y-z))/3.0);
// Adjust to flat coordinates on the offset numbering system
var corrected = hexToFlatCoordinates(ix, iy, iz);
//corrected.x--;
return axialToOffsetCoordinates(corrected);
}
function hexToFlatCoordinates(ix, iy, iz)
{
var coordinates = [];
coordinates.x = ix;
coordinates.y = iz;
return coordinates;
}
function axialToOffsetCoordinates(axial)
{
var offset = [];
offset.x = axial.x;
offset.y = axial.y + Math.ceil(axial.x/2.0);
return offset;
}
var point = coordinatesForHexAtPoint(100, 100, 100);
var x = point.x;
var y = point.y;
This code above gave me extremely accurate results.