假設我有以下 3D 離散化空間,其中樣本/節點的索引是順序的,如圖所示。

現在只考慮水平中間層。
我的目標是找到一個以編程方式和迭代規則/s,允許我在中間層上運行螺旋(如影像或類似的,它可以從任何方向開始),從節點 254 開始,如上所示圖片:

正如您在圖片中看到的,黃色十字表示要探索的節點。在第一圈,這些節點是連續的,而在第二圈,它們被 1 個節點隔開,依此類推。
我開始解決問題如下(偽代碼):
我考慮過 size(y) = y = 13
大小(z) = z = 3
第 1 圈:
- 254 – z * y = 215
- 254 – z * (y 1) = 212
- 254 – z = 251
- 254 z * (y - 1) = 290
- 254 z * y = 293
- 254 z * (y 1) = 296
- 254 z = 257
- 254 – z * (y – 1) = 218
- 第 2 圈:
- 254 – 3 * z * y = 137
- 254 – 3 * z * (y 2/3) = 131
- …
但我認為可能有一個更簡單、更通用的規則。
uj5u.com熱心網友回復:
每個方向都有恒定的索引增量:
const int dx = 39;
const int dy = 3;
const int dz = 1;
所以要制作螺旋線,您只需從起始索引開始并沿當前方向遞增,i-times然后旋轉 90 度并執行相同的操作……然后遞增i并執行此操作,直到達到所需的大小……
您還應該添加范圍檢查,這樣您的螺旋就不會超出您的陣列,因為這會搞砸。通過檢查實際x,y,z坐標。因此,要么并行計算它們,要么通過ix使用模算術推斷它們,例如(C )之類的東西:
const int dx = 39;
const int dy = 3;
const int dz = 1;
int cw[4]={-dx,-dy, dx, dy}; // CW rotation
int ix=254; // start point (center of spiral)
int dir=0; // direction cw[dir]
int n=5; // size
int i,j,k,x,y,z,a; // temp
for (k=0,i=1;i<=n;i =k,k^=1,dir ,dir&=3)
for (j=1;j<=i;j )
{
int a=ix-1;
z = a% 3; a/= 3; // 3 is z-resolution
y = a%13; a/=13; // 13 is y-resolution
x = a;
if ((x>=0)&&(x<13)&&(y>=0)&&(y<13)&&(z>=0)&&(z<3))
{
// here use point ix
// Form1->mm_log->Lines->Add(AnsiString().sprintf("%i (%i,%i,%i) %i",ix,x,y,z,i));
}
ix =cw[dir];
}
產生這個輸出
ix x,y,z i
254 (6,6,1) 1
215 (5,6,1) 1
212 (5,5,1) 2
251 (6,5,1) 2
290 (7,5,1) 2
293 (7,6,1) 2
296 (7,7,1) 3
257 (6,7,1) 3
218 (5,7,1) 3
179 (4,7,1) 3
176 (4,6,1) 3
173 (4,5,1) 3
170 (4,4,1) 4
209 (5,4,1) 4
248 (6,4,1) 4
287 (7,4,1) 4
326 (8,4,1) 4
329 (8,5,1) 4
332 (8,6,1) 4
335 (8,7,1) 4
338 (8,8,1) 5
299 (7,8,1) 5
260 (6,8,1) 5
221 (5,8,1) 5
182 (4,8,1) 5
143 (3,8,1) 5
140 (3,7,1) 5
137 (3,6,1) 5
134 (3,5,1) 5
131 (3,4,1) 5
如果您希望 CCW 螺旋反轉cw[]或代替dir 執行dir--
如果您想要更改螺釘寬度,那么您只需增加i實際寬度而不是僅增加 1。
uj5u.com熱心網友回復:
基于@Spektre 的回答,這段代碼對我有用:
const int x_res = 13;
const int y_res = 13;
const int z_res = 3;
const int dx = 39;
const int dy = 3;
const int dz = 1;
int cw[4]={-dx,-dy, dx, dy}; // CW rotation
int ix=254; // start point (center of spiral)
int dir=0; // direction cw[dir]
int n=30; // size
int i,j,k;
cout << ix << endl;
// first "lap" (consecutive nodes)
for (k=0,i=1;i<=2;i =k,k^=1,dir ,dir&=3)
for (j=1;j<=i;j )
{
ix =cw[dir];
cout << ix << endl;
}
i-=1;
int width = 2; //screw width
i =width;
int dist = 1; //nodes separation
int node_count = 0; //nodes counter
for (k=k,i=i;i<=n;i =k,k^=width,dir ,dir&=3)
{
if (dir==1)
{
dist =1;
}
for (j=1;j<=i;j )
{
ix =cw[dir];
node_count =1;
if ((0 < ix) && (ix <= x_res*y_res*z_res))
{
if (node_count == dist)
{
cout << ix << endl;
node_count = 0;
}
}
else return 0;
}
}
return 0;
使用此輸出:
254 215 212 251 290 293 296 257 218 179 140 134 128 206 284 362 368 374 380 302
224 146 68 59 50 83 200 317 434 443 452 461 386 269 152 35
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/321841.html
上一篇:A*搜索中水壺問題的啟發式函式
下一篇:使用構建插入和中值構建資料結構
