因此,我尝试在C++中实现此算法。然而,有些地方肯定出了问题,因为选择任意的x和y坐标并使其适合于平面是没有意义的。如果平面是垂直于x轴的,则y=1的点将不在平面上。
我意识到这个问题已经在StackOverflow上发布了很多次,并且我看到了很多解决方案,其中平面由3个点定义。但是我只有一个法向量和一个位置。在解决我的非共线点查找器之前,我无法测试我的线面交叉算法。
目前的问题是,我正在除以normal.z,当normal.z为0时,这显然行不通。
我正在使用这个平面进行测试:Plane* p = new 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
的值无法影响不等式。 - comingstorm