通过 X 和 Y 获取索引

Get Index by X and Y

有一种方法可以根据索引得到 X 和 Y,比如[​​=11=]

x = index % columns;
y = index / columns;

我怎样才能反转它并获得基于 X 和 Y 的索引?

假设我们在这里处理整数,如果您有以下代码:

x = index % columns; // get the remainder of dividing index by columns 
y = index / columns; // get the result of dividing index by columnsn

那么反过来就是:

index = y * columns + x;