内格数字转换为它们相应的x,y坐标 [英] Converting numbers within grid to their corresponding x,y coordinates

查看:209
本文介绍了内格数字转换为它们相应的x,y坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下电网的数字(从1到36),我怎么能在网格内确定其坐标(X,Y)?

  | 0 1 2 3 4 5 6 7 8
------------------------------
0 | 1 2 3 4 5 6 7 8 9
1 | 10 11 12 13 14 15 16 17 18
2 | 19 20 21 22 23 24 25 26 27
3 | 28 29 30 31 32 33 34 35 36
 

即。我想获得如下:

  | 0 1 2 8
------------------------------
0 | (0,0)(1,0)(2,0)...(8,0)
1 | (0,1)(1,1)(2,1)...(8,1)
2 | (0,2)(1,2)(2,2)...(8,2)
3 | (0,3)(1,3)(2,3)...(8,3)
 

我曾尝试:

  X =编号%9-1;
Y =号/ 9;
 

其中适用于除那些在右边的最后一列的所有情况。

于是我想出了:

 如果(编号%9 == 0){
    X = 8;
    Y =号/ 9-1;
}
其他{
    X =编号%9-1;
    Y =号/ 9;
}
 

我的问题是,是否有这样做的更聪明的方式?

解决方案

  X =(数1)9%;
Y =(数1)/ 9;
 

Given the numbers in the following grid (from 1 to 36), how can I determine their coordinates (x,y) within the grid?

 |  0  1  2  3  4  5  6  7  8
------------------------------
0|  1  2  3  4  5  6  7  8  9
1| 10 11 12 13 14 15 16 17 18
2| 19 20 21 22 23 24 25 26 27
3| 28 29 30 31 32 33 34 35 36

i.e. what I want to obtain is the following:

 |     0     1     2        8
------------------------------
0| (0,0) (1,0) (2,0) ... (8,0)
1| (0,1) (1,1) (2,1) ... (8,1)
2| (0,2) (1,2) (2,2) ... (8,2)
3| (0,3) (1,3) (2,3) ... (8,3)

I have tried:

x = number%9-1;
y = number/9;

which works for all cases except the ones in the last column on the right.

So I came up with:

if (number%9==0) {
    x = 8;
    y = number/9-1;
}
else{
    x = number%9-1;
    y = number/9;
}

My question is, is there a smarter way of doing this?

解决方案

x = (number-1)%9;
y = (number-1)/9;

这篇关于内格数字转换为它们相应的x,y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆