反转方法?

时间:2017-05-05 18:30:49

标签: java

所以我有这个方法......

public static Vector2 cellsToIso(float row, float col) {
    float halfTileWidth = tileWidth *0.5f;
    float halfTileHeight = tileHeight *0.5f;

    float x = (col * halfTileWidth) + (row * halfTileWidth);
    float y = (row * halfTileHeight) - (col * halfTileHeight);

    return  new Vector2(x,y);
}

我希望使用反向方法isoToCells(float x, float y)

我尝试了这个,但它对我没有意义

 public static Vector2 isoToCell(float x, float y) {
    float halfTileWidth = tileWidth * 0.5f;
    float halfTileHeight = tileHeight * 0.5f;

    float row = (y / halfTileWidth) - (x / halfTileWidth);
    float col = (x / halfTileHeight) + (y / halfTileHeight);

    return  new Vector2(row,col);
}

1 个答案:

答案 0 :(得分:2)

float x = (col * halfTileWidth) + (row * halfTileWidth);
float y = (row * halfTileHeight) - (col * halfTileHeight); 

通过这两个等式我们可以写

x/halfTileWidth = row + col;
y/halfTileHeight = row - col;

所谓rowcolumnxy而言,

row = (1.0/2) * (x/halfTileWidth + y/halfTileHeight);
column = (1.0/2) * (x/halfTileWidth - y/halfTileHeight);

在逆方法中替换它以获取rowcolumn

public static Vector2 isoToCell(float x, float y) {
    float halfTileWidth = tileWidth * 0.5f;
    float halfTileHeight = tileHeight * 0.5f;

    float row = (1.0/2) * (x/halfTileWidth + y/halfTileHeight);
    float col = (1.0/2) * (x/halfTileWidth - y/halfTileHeight);

    return  new Vector2(row,col);
}