【发布时间】:2018-04-10 22:41:13
【问题描述】:
我在 3D 空间中有几个点,我想编写一个条件来确定两个点是否相邻:它们在整数格中是否仅相隔一个单位?
我有一个名为 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;
}
【问题讨论】:
-
我们需要您为此应用程序定义“相邻”。
-
@Prune 我添加了一张图片,其中“点”连接并且相邻的点直接相互连接
-
我想我明白了:你需要坐标在一个轴上正好相差 1。你能写出这个逻辑吗?
-
@Prune 完全正确。而且我不知道如何解决这种问题。编码不是我需要理解逻辑的问题。一旦我有了它,一切都很好:)
标签: c++ algorithm adjacency-list