我在3D空间中有几个点,我想写一个条件来确定两个点是否相邻:它们是否只是整数点阵中的一个单位?
我有一个名为Point的结构,它包含x,y和z坐标。然后在main函数中,我设置点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;
}
答案 0 :(得分:2)
非常简短:
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);第二个点b
遍历从a
的位置到结尾的索引,n-1
。
给定整数坐标,检查邻接也很容易。
diff = abs(a.x - b.x) +
abs(a.y - b.y) +
abs(a.z - b.z)
diff
= 1 iff 这些点相邻。