public class Test {
private static final int xDimension = 4;
public static void main(String[] args) {
System.out.println(getPosition(1));
System.out.println(getPosition(3));
System.out.println(getPosition(4));
System.out.println(getPosition(5));
System.out.println(getPosition(12));
System.out.println(getPosition(32));
}
public static Vector2 getPosition(int n) {
int y = (n - 1) / xDimension;
int x = (n - 1) % xDimension;
return new Vector2(x, y);
}
public static class Vector2 {
int x, y;
public Vector2(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
}
1条答案
按热度按时间8i9zcol21#
如果您问的是“如何计算4x8Map中第12个对象的位置”,则算法如下:
(我假设向量2包含int,int作为x,y位置,0,0是原点,我们从1开始计算分幅):
输出: