如何与QUOT;扁平化"或QUOT;指数"三维阵列中的一维数组? [英] How to "flatten" or "index" 3D-array in 1D array?

查看:243
本文介绍了如何与QUOT;扁平化"或QUOT;指数"三维阵列中的一维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想变平的三维数组转换成一维数组的块系统在我的游戏。它是一个三维块游戏和基本上欲块系统是几乎相同的Minecraft的系统(由任何措施然而,这不是的Minecraft克隆)。在我的previous 2D游戏我曾访问过的扁平阵列算法如下:

I am trying to flatten 3D array into 1D array for "chunk" system in my game. It's a 3D-block game and basically I want the chunk system to be almost identical to Minecraft's system (however, this isn't Minecraft clone by any measure). In my previous 2D-games I have accessed the flattened array with following algorithm:

Tiles[x + y * WIDTH]

然而,这显然不与3D工作,因为它缺少Z轴。我不知道如何实现这种算法在三维空间。宽度,高度和深度都是常数(和宽度是一样大的高度)。

However, this obviously doesn't work with 3D since it's missing the Z-axis. I have no idea how to implement this sort of algorithm in 3D-space. Width, height and depth are all constants (and width is just as large as height).

难道仅仅是 X + Y *宽+ Z *深度?我是pretty的数学不好,我刚开始3D编程,所以我pretty的丢失:|

Is it just x + y*WIDTH + Z*DEPTH ? I am pretty bad with math and I am just beginning 3D-programming so I am pretty lost :|

PS。这样做的原因是,我循环,并从中得到的东西通过索引不少。我知道,一维数组比多维数组(的原因,我不能记住:P)更快。尽管这可能不是必要的,我要相同的良好性能越好:)

PS. The reason for this is that I am looping and getting stuff by index from it quite a lot. I know that 1D arrays are faster than multi-dimensional arrays (for reasons I cant remember :P ). Even though this may not be necessary, I want as good performance as possible :)

推荐答案

该算法大多是相同的。如果你有一个三维数组原始[高度,宽度,深度] ,那么你可以把它变成平[高*宽*深]

The algorithm is mostly the same. If you have a 3D array Original[HEIGHT, WIDTH, DEPTH] then you could turn it into Flat[HEIGHT * WIDTH * DEPTH] by

Flat[x + WIDTH * (y + DEPTH * z)] = Original[x, y, z]

顺便说一句,你应该$阵列在.NET中多维数组的对$ PFER阵列。的性能差异是显著

As an aside, you should prefer arrays of arrays over multi-dimensional arrays in .NET. The performance differences are significant

这篇关于如何与QUOT;扁平化"或QUOT;指数"三维阵列中的一维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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