从中心循环 XYZ
我有一个三维数组,我想用 C# 中的值填充它。通过一次循环一个维度,它将从一个角开始,并按行工作,直到在对角结束。
标准循环:
for (int x = 0; x < 10; x++)
for (int y = 0; y < 10; y++)
for (int z = 0; z < 10; z++)
{
// fill value
}
我想循环相同的值,但从数组的中心开始,慢慢向外移动,这样,如果我在 3D 空间中查看正在填充的数组,它会慢慢成长为一个球(或立方体)从中间开始。
关于如何做到这一点有什么好的想法吗?填充立方体形式和填充球体(即距离中间最近的距离)的想法会很棒!
I have a three dimensional array which I want to fill in with values in C#. By looping one dimension at a time it will start at one corner, and work in rows until it finishes at the opposite corner.
Standard loop:
for (int x = 0; x < 10; x++)
for (int y = 0; y < 10; y++)
for (int z = 0; z < 10; z++)
{
// fill value
}
I want to loop the same values, but start at the center of the array, and slowly work my way outward, so that if I look at the array in 3D space as it's being filled, it will slowly grow as a ball (or cube) from the middle.
Any good ideas on how I can do this? Ideas for both filling a cube form, and filling as a sphere (ie. closest distance to middle) would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以使用 BFS 之类的东西
C 风格的伪代码,而不是 C#:
它将按 Manhettan 距离填充区域。所以它会立方体,但角到顶部和底部
你也可以使用这个简单的解决方案(也是伪代码):
You may use something like BFS
C-style pseudocode, not C#:
It will fill area by Manhettan distance. So it will cube, but corner to top and bottom
Also you may use this naive solution(pseudocode too):