如何在 C++ 中查找 3D space 中的相邻点

How to find adjacent points in a 3D space in c++

我有几个三维点space,我想写一个条件来判断两个点是否相邻:它们在整数格中是否只相隔一个单位?

我有一个名为 Point 的结构,它包含 x、y 和 z 坐标。然后在主函数中,我设置点 a、b、c、d、e 的值并将它们放入一个向量中。然后在 for 循环中我想检查两个点是否相邻。目前我只是在检查它们是否在同一个轴上,但我不知道如何进行。

struct Point{
   int x;
   int y;
   int z;
};

int main(){
    // just a couple of points
    struct Point a;
    struct Point b;
    struct Point c;
    struct Point d;
    struct Point e;

    vector<Point> pointVector;

    a.x = 0 ; a.y = 0; a.z = 0;
    b.x = 0; b.y = 0; b.z = -1;
    c.x = 1; c.y = 0; c.z = -1;
    d.x = 1; d.y = -1; d.z = -1;
    e.x = 2; e.y = -1; e.z = -1;

    pointVector.push_back(a);
    pointVector.push_back(b);
    pointVector.push_back(c);
    pointVector.push_back(d);
    pointVector.push_back(e);

    for(int i = 0; i < 5; i++){
       if(pointVector[i].x == pointVector[i+1].x ||  // how to set the condition to check if two points are adjacent?
          pointVector[i].y == pointVector[i+1].y || 
          pointVector[i].z == pointVector[i+1].z  
       ) cout << "adjacent" << endl;
       else
         cout << "not adjacent" << endl;


    }



    return 0;
}

我所说的相邻是指这张照片中的内容:

非常简短:

for each pair of points:
    if two of the three coordinates are equal AND
              the other coordinate differs by 1:
        then mark the pair as adjacent.

遍历点对非常简单:第一个点 a 遍历索引 0-(n-2);第二点 ba 的位置遍历索引,一直到最后,n-1.

给定整数坐标,检查邻接也很容易。

diff = abs(a.x - b.x) + 
       abs(a.y - b.y) + 
       abs(a.z - b.z)

diff = 1 当且仅当 这些点是相邻的。