将增量网格索引转换为 (x,y) 坐标

Convert incremental grid index to (x,y) coordinates

我正在拼命寻找给我 xy 的表达式,每个 i,以这种方式排序:

x = f(i) ??
y = f(i) ??

我在 GLSL 中编码,所以我可以使用基本的数学函数(模数、底数、平方根...)以及 if/else 语句。不过,我只能访问 i 变量。

我不知道 GLSL,但这是 Python 中的代码 3. 这应该很容易转换为其他语言。 (你可以在这里使用 floor 而不是 int。)这个版本使用 Python 到 return 两个输出值 xy 作为一个输入值 i。在 Python 3 中,代码顶部需要行 from math import sqrt

def f(i):
    base = int(sqrt(i))
    remains = i - base * base
    if remains <= base:
        x = remains
        y = base
    else:
        x = base
        y = 2 * base - remains
    return x, y