Here is what I came up with
function posInGrid(x, y, length) { xFromColCenter = x % length - length / 2; yFromRowCenter = y % length - length / 2; col = (x - xFromColCenter) / length; row = (y - yFromRowCenter) / length; if (yFromRowCenter < xFromColCenter) { if (yFromRowCenter < (-xFromColCenter))--row; else++col; } else if (yFromRowCenter > xFromColCenter) { if (yFromRowCenter < (-xFromColCenter))--col; else++row; } return "Col:"+col+", Row:"+row+", xFC:"+xFromColCenter+", yFC:"+yFromRowCenter; }
X and Y are the coordinates in the image, and the length is the grid spacing.
Right now it is returning a string, just for testing .. the result should be a string and col, and these are the coordinates I selected: your tile 1 has the coordinates (1,0) tile 2 (3,0), tile 10 is (0 , 1), tile 11 is equal to (2,1). You can convert my coordinates to numbered tiles in a line or two.
And JSFiddle for testing http://jsfiddle.net/NHV3y/
Greetings.
EDIT: changed the return statement, had some variables that I used to debug left.
jcane86
source share