0

我想在 C++ 中创建一个 3d 立方体网格,但我有一个问题:一旦指定了起点(xs,ys,zs),终点(xe,ye,ze)和沿三个轴的元素数量..我想确定每个立方体的八个角。将原点设置在 (0,0,0) 和 i,j,k 沿三个轴移动的索引我知道:

1°角在(i,j,k) 2°角在(i+1,j,k) 3°角在(i,j+1,k)

等等..

我不知道如何获取这三倍的价值并在所有元素的三个“for循环”中识别一个点......请帮助我:)

4

1 回答 1

2

首先定义一个简单的struct point3D:

typedef struct {

  float x;
  float y;
  float z;
} point3D;

我写这个来生成网格:

//Compute edge sizes 
float x_size = xe - xs;
float y_size = ye - ys;
float z_size = ze - zs;

//Compute steps
float x_step = x_size/n_step;
float y_step = y_size/n_step;
float z_step = z_size/n_step;

//Points per edge (including ending point)
int n = n_step + 1;

//Alloc grid (you can use malloc if you prefer)
point3D grid[n*n*n];

for (int i = 0; i < n; i++) { //move on x axis

  for (int j = 0; j < n; j++) { //move on y axis

    for (int k = 0; k < n; k++) { //move on z axis

      point3D p;
      p.x = xs + x_step * i;
      p.y = ys + y_step * j;
      p.z = zs + z_step * k;
      grid[i+n*j+n*n*k] = point3D;
    } 
  }
}

要获取 8 个角点,请使用:

point3D corner = grid[n_step*x + n*n_step*y + n*n*n_step*z];

和:

   (x, y, z)
1: (0, 0, 0)
2: (0, 0, 1)
3: (0, 1, 0)
4: (0, 1, 1)
5: (1, 0, 0)
6: (1, 0, 1)
7: (1, 1, 0)
8: (1, 1, 1)
于 2013-07-11T11:09:13.043 回答