所以我有一种形式的网格,我正在尝试获取X和Y。
有没有公式可以将例如12变成2,2或14变成2,3
还有这种网格的名称吗?
static int getX(int z)
{
int count = 0;
int res = 0;
int curr = 0;
for(int temp = z; temp > 0; temp >>= 1)
{
if(count % 2 ==0)
{
res += ((temp & 1) << curr);
curr++;
}
count++;
}
return res;
}
static int getY(int z)
{
int count = 0;
int res = 0;
int curr = 0;
for(int temp = z; temp > 0; temp >>= 1)
{
if(count % 2 ==1)
{
res += ((temp & 1) << curr);
curr++;
}
count++;
}
return res;
}
答案 0 :(得分:0)
正如Sneftel所观察到的,这看起来像一个Z-order curve。这样,您可以通过交织二进制表示来转换坐标。所以你的例子是
0 1 0 x=2 0 1 0 x=2
0 1 0 y=2 0 1 1 y=3
001100 p=12 001110 p=14
因此要从单元格编号 p 中获取 x 和 y 坐标,请分配 p 的位交替为 x 和 y 。使用基本算术运算很难表达这种位算术,并且我不知道对此没有公认的公式符号,但是这个想法很简单。