gpt4 book ai didi

2d - 屏幕坐标到等距坐标

转载 作者:行者123 更新时间:2023-12-02 17:41:48 24 4
gpt4 key购买 nike

我正在努力将鼠标/屏幕坐标转换为等距图 block 索引。我已经尝试了所有可以在这里或在互联网上找到的公式,但它们似乎都不起作用,或者我遗漏了一些东西。

enter image description here

这是一张图片,原点在左上角,一个图 block 的尺寸是 128x64px。

如果有任何帮助,我将不胜感激。

最佳答案

基本上,您需要应用一个带有其他一些位的旋转矩阵。下面是一些用 AWK 编写的示例代码,应该很容易移植到任何其他语言:

END {
PI = 3.1415;
x = 878.0;
y = 158.0;

# Translate one origin to the other
x1 = x - 128*5;
# Stretch the height so that it's the same as the width in the isometric
# This makes the rotation easier
# Invert the sign because y is upwards in math but downwards in graphics
y1 = y * -2;

# Apply a counter-clockwise rotation of 45 degrees
xr = cos(PI/4)*x1 - sin(PI/4)*y1;
yr = sin(PI/4)*x1 + cos(PI/4)*y1;

# The side of each isometric tile (which is now a square after the stretch)
diag = 64 * sqrt(2);
# Calculate which tile the coordinate belongs to
x2 = int(xr / diag);
# Don't forget to invert the sign again
y2 = int(yr * -1 / diag);

# See the final result
print x2, y2;
}

我用几个不同的坐标对其进行了测试,结果似乎是正确的。

关于2d - 屏幕坐标到等距坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19717770/

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