如何将1d索引转换为3d索引? [英] How do I convert a 1d index into a 3d index?

查看:84
本文介绍了如何将1d索引转换为3d索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了这个问题:转换1d数组索引到3d数组索引?,但是无论我如何计算索引,我的测试结果都不同。

I looked at this question: Convert a 1d array index to a 3d array index? but my result differs when I test it no matter how I calculate the index in the first place.

我正在使用以下将尺寸(宽度,高度,深度)的3d(x,y,z)转换为1d:

I am using the following to convert 3d (x,y,z) in dimensions (WIDTH, HEIGHT, DEPTH) to 1d:

i=x+WIDTH(y+HEIGHT*z)

然后从索引i转换回(x, y,z):

Then to convert back from index i to (x,y,z):

z=i/(HEIGHT*WIDTH)
y=(i/WIDTH)%HEIGHT
x=i%(WIDTH)

当我在两种方法之间进行测试时,我得到了错误的提示结果。我的方法有明显的问题吗?正确的解释是什么?

When I test this between both methods I get the wrong results. Is there something obviously wrong with my method? What is the correct explanation?

推荐答案

以下是适用于这些值的javascript版本:

here's a javascript version that works with these values:

WIDTH = 10;
HEIGHT = 20;

x = 2;
y = 4;
z = 5;

=> i = 1042

代码:

i = x + WIDTH * (y + HEIGHT * z);


z = Math.round(i / (WIDTH * HEIGHT));
y = Math.round((i - z * WIDTH * HEIGHT) / WIDTH);
x = i - WIDTH * (y + HEIGHT * z);

这篇关于如何将1d索引转换为3d索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆