gpt4 book ai didi

java - 反转方法?

转载 作者:行者123 更新时间:2023-12-02 02:51:15 25 4
gpt4 key购买 nike

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

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);
}

最佳答案

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);

用逆方法替换它即可恢复

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);
}

关于java - 反转方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43811648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com