我正试图在线平面相交算法中实现。根据Wikipedia,我需要在飞机上有三个非共线点来做到这一点。如何在飞机上获得三个非共线点? - C++
因此,我试着在C++中实现this algorithm。有些东西肯定是错的,因为我可以选择任何x和y坐标并且它们适合在飞机中是没有意义的。如果飞机是垂直的并沿着x轴呢?那么y = 1的点就不在飞机上了。
我意识到这个问题已经在StackOverflow上发布了很多,并且我看到很多解决方案,其中平面由3个点定义。但我只有一个正常的位置。在我梳理我的非共线点测向仪之前,我无法测试我的线平面相交算法。
现在的问题是,我被normal.z分,而当normal.z为0
我与这架飞机的测试显然是行不通的:平面* P =新的Plane(Color(),Vec3d(0.0,0.0,0.0),Vec3d(0.0,1.0,0.0)); //第二个参数:位置,第三个参数:正常
当前的代码给出了这样的不正确的答案:
{0 , 0 , 0} // alright, this is the original
{12.8377 , 17.2728 , -inf} // obviously this is not a non-colinear point on the given plane
这里是我的代码:
std::vector<Vec3d>* Plane::getThreeNonColinearPoints() {
std::vector<Vec3d>* v = new std::vector<Vec3d>();
v->push_back(Vec3d(position.x, position.y, position.z)); // original position can serve as one of the three non-colinear points.
srandom(time(NULL));
double rx, ry, rz, start;
rx = Plane::fRand(10.0, 20.0);
ry = Plane::fRand(10.0, 20.0);
// Formula from here: http://en.wikipedia.org/wiki/Plane_(geometry)#Definition_with_a_point_and_a_normal_vector
// nx(x-x0) + ny(y-y0) + nz(z-z0) = 0
// |-----------------| <- this is "start"
//I'll try to insert position as x0,y0,z0 and normal as nx,ny,nz, and solve the equation
start = normal.x * (rx - position.x) + normal.y * (ry - position.y);
// nz(z-z0) = -start
start = -start;
// (z-z0) = start/nz
start /= normal.z; // division by zero
// z = start+z0
start += position.z;
rz = start;
v->push_back(Vec3d(rx, ry, rz));
// TODO one more point
return v;
}
我意识到,我可能会尝试解决这完全错了。如果是这样,请链接这个的具体实现。我确信它必须存在,当我看到这么多的线平面交叉点实现时。
在此先感谢。
实际上,您可以用方程式'ax + by + cz == d'来描述任何平面。任何具有'b == 0.0'的平面都将平行于y轴(如您所描述的那样),因为在这种情况下,'y'的值不会影响(in)的平等。 – comingstorm