I have a formula, which gets coordinates from user_id (see below).
I give User_id, and I get back coordinates.
But now I need to get User_id from coordinates.
For example:
function get_user_id_from_coordinates(x, y) {
// if x = 1, y = 0, returns 4 (as in this image)
}
Please help me with that formula :) The first one i got also form this forum.
// if user_id = 64
function get_coordinates_from_user_id(user_id) {
// Math.sqrt(user_id) = 8
// 8-1 = 7
// 7 / 2 = 3.5
// Math.ceil(3.5) = 4
var k = Math.ceil( (Math.sqrt(user_id) - 1) / 2 );
// t = 2 * 4 + 1 = 9
var t = 2 * k + 1;
// m = 81
var m = t * t;
// t = 9 - 1 = 8
t = t - 1;
// if 64 >= 81 - 8. If 64 >= 73
if (user_id >= m - t) {
// return [ -4, 4 - (81 - 64)]. return [ -4, -13 ]
return [ -k, k - (m - user_id) ]
}
// else m = 81 - 8 = 73
else m = m - t;
// if 64 >= 73 - 8. If 64 >= 65
if (user_id >= m - t) {
// return [ -4 + (73 - 64), -4]. return [ 5, -4 ]
return [ -k + (m - user_id), -k]
}
// else m = 73 - 8 = 65
else m = m - t;
// if 64 >= 65 - 8. If 64 >= 57
if (user_id >= m - t) {
// return [ 4, -4 + (65 - 64) ]. return [ 4, -3 ] <== passes
return [ k, -k + (m - user_id) ]
};
// return [ 4 - (65 - 64 - 8), 4 ]. return [ 11, 4 ]
else return [ k - (m - user_id - t), k ];
}