从中心循环 XYZ

发布于 2024-12-02 02:57:29 字数 396 浏览 1 评论 0原文

我有一个三维数组,我想用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や莫失莫忘 2024-12-09 02:57:29

你可以使用 BFS 之类的东西
C 风格的伪代码,而不是 C#:

class Point{int x,y,z;};

queue<Point> q;
bool used[100][100][100]; //all false. true when add point to queue
Point vectors[]={(0,0,1),(0,0,-1),(0,1,0),(0,-1,0),(1,0,0),(-1,0,0)}
q.push(Point(50,50,50)); //start point
used[50][50][50]=true;
while(!q.empty()){
     Point cur=q.front();
     //use cur;
     q.pop();
     for(i=0;i<6;++i){
         if(!used[cur+vectors[i]] && (cur+vectors[i] is in our diapason)){
             q.push(cur+vectors[i]);
             used[cur+vectors[i]]=true;
         }
     }
}

它将按 Manhettan 距离填充区域。所以它会立方体,但角到顶部和底部

你也可以使用这个简单的解决方案(也是伪代码):

Point points[];
for (int x = 0; x < 10; x++)
    for (int y = 0; y < 10; y++)
        for (int z = 0; z < 10; z++)
            {
                 add to point array
            }

sort points[] by function r() (if r(a)<r(b) then a before b)

loop points here

r(a)=sqrt((a.x-5)^2+(a.y-5)^2+(a.z-5)^2) for ball
r(a)=max(abs(a.x-5),abs(a.y-5),abs(a.z)-5) for cube

You may use something like BFS
C-style pseudocode, not C#:

class Point{int x,y,z;};

queue<Point> q;
bool used[100][100][100]; //all false. true when add point to queue
Point vectors[]={(0,0,1),(0,0,-1),(0,1,0),(0,-1,0),(1,0,0),(-1,0,0)}
q.push(Point(50,50,50)); //start point
used[50][50][50]=true;
while(!q.empty()){
     Point cur=q.front();
     //use cur;
     q.pop();
     for(i=0;i<6;++i){
         if(!used[cur+vectors[i]] && (cur+vectors[i] is in our diapason)){
             q.push(cur+vectors[i]);
             used[cur+vectors[i]]=true;
         }
     }
}

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):

Point points[];
for (int x = 0; x < 10; x++)
    for (int y = 0; y < 10; y++)
        for (int z = 0; z < 10; z++)
            {
                 add to point array
            }

sort points[] by function r() (if r(a)<r(b) then a before b)

loop points here

r(a)=sqrt((a.x-5)^2+(a.y-5)^2+(a.z-5)^2) for ball
r(a)=max(abs(a.x-5),abs(a.y-5),abs(a.z)-5) for cube
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文