我想在3維空間中創建立方體素節點的26個相鄰節點。輸入是節點的x,y,z位置和立方體的大小。我正在嘗試使用for循環,但尚未管理。我在編程方面很新手,請幫助我。在3d空間中創建相鄰節點的循環
2
A
回答
8
for (int dz = z - 1; dz <= z + 1; ++dz)
{
for (int dy = y - 1; dy <= y + 1; ++dy)
{
for (int dx = x - 1; dx <= x + 1; ++dx)
{
// all 27
if ((dx != x) || (dy != y) || (dz != z))
{
// just the 26 neighbors
}
}
}
}
1
這是一個沒有嵌套循環的變體。我試過使用類似於your question的表示法。
// g++ 26neighbours.cpp -o 26neighbours && 26neighbours
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
namespace {
struct Pos {
double x, y, z;
template<class Char, class Traits>
friend std::basic_ostream<Char, Traits>&
operator<< (std::basic_ostream<Char, Traits>& out, const Pos& p)
{
return out << p.x << " " << p.y << " " << p.z;
}
explicit Pos(double x_ = 0, double y_ = 0, double z_ = 0)
: x(x_), y(y_), z(z_)
{
}
};
template <class OutputPosIterator, class InputPosIterator, class Number>
void translate(OutputPosIterator first, OutputPosIterator last,
InputPosIterator delta, Number factor)
{
for (; first != last; ++first, ++delta) {
first->x += delta->x * factor;
first->y += delta->y * factor;
first->z += delta->z * factor;
}
}
}
int main(int argc, char *argv[])
{
const Pos delta[] = {
// ruby -e"(-1..1).each{|i| (-1..1).each{|j| (-1..1).each{|k| printf(\"Pos(%2d,%2d,%2d),\n\", i, j, k) if (i!=0 || j!=0 || k!=0)}}}"
Pos(-1,-1,-1),
Pos(-1,-1, 0),
Pos(-1,-1, 1),
Pos(-1, 0,-1),
Pos(-1, 0, 0),
Pos(-1, 0, 1),
Pos(-1, 1,-1),
Pos(-1, 1, 0),
Pos(-1, 1, 1),
Pos(0,-1,-1),
Pos(0,-1, 0),
Pos(0,-1, 1),
Pos(0, 0,-1),
Pos(0, 0, 1),
Pos(0, 1,-1),
Pos(0, 1, 0),
Pos(0, 1, 1),
Pos(1,-1,-1),
Pos(1,-1, 0),
Pos(1,-1, 1),
Pos(1, 0,-1),
Pos(1, 0, 0),
Pos(1, 0, 1),
Pos(1, 1,-1),
Pos(1, 1, 0),
Pos(1, 1, 1),
};
const int N = sizeof(delta)/sizeof(*delta);
// find neighbours of somePos
double cube_size = 0.5;
Pos somePos(0.5, 0.5, 0.5);
std::vector<Pos> neighbours(N, somePos);
translate(neighbours.begin(), neighbours.end(), delta, cube_size);
// print neighbours
std::copy(neighbours.begin(), neighbours.end(),
std::ostream_iterator<Pos>(std::cout, "\n"));
std::cout << std::endl;
return 0;
}
0
for(int i = 0; i < 27, i++)
{
if(i == 13) continue;
int dx = i%3 -1;
int dy = (i/3)%3 -1;
int dz = i/9 - 1;
process(x+dx,y+dy,z+dz);
}
相關問題
- 1. Libgdx在3D中創建天空/環境
- 2. AS3-在3D空間中創建一個可循環的隨機路徑
- 3. 使用循環創建xml節點,coldfusion
- 4. 在鄰接矩陣中形成一個循環的節點(Matlab)
- 5. 如何在gnuplot中創建3d相空間圖?
- 6. 使用名稱空間創建節點
- 7. 數基於相鄰節點
- 8. 在* ngFor循環中創建空白行
- 9. 查找樹中的相鄰節點
- 10. 如何使用php在while循環中創建子xml節點
- 11. 在neo4j循環中創建關係和合並節點
- 12. 循環相同的密鑰JSON節點
- 13. Ruby循環創建空行
- 14. VBA - 刪除相鄰行中循環
- 15. 通過Neo4j中的節點循環並創建關係
- 16. 如何在JavaScript中創建XML命名空間節點?
- 17. 在Python/R中創建節點邊緣三角形鄰接圖
- 18. scenekit節點的旋轉和3D空間中的移動
- 19. 如何在3D空間中插入點?
- 20. 探索圖上的相鄰節點
- 21. 交換LinkedList的相鄰節點
- 22. 與合併相鄰節點的幫助
- 23. RadTreeView節點循環
- 24. 循環遍歷由HtmlAgilityPack創建的節點
- 25. 在PowerShell中創建循環
- 26. 在循環中創建ezPlots
- 27. 在循環中創建json
- 28. 3D中半空間集合的頂點
- 29. 3D空間中的曲線擬合點
- 30. 基於相鄰節點計算節點值的圖算法
你可能需要做一些邊界邊緣的情況下檢查,但你絕對回答了這個問題。 +1 – 2008-11-28 23:35:03
修正錯字:++ z,y,x應分別爲++ dz,dy,dx。 – strager 2008-11-29 00:29:01