help----求助!!!!!!!!!
我写了个程序,现在知道那地方有问题,但是不理解为什么会出错。具体如下:void Delaunay::buildSuperTriangle()
{
if (pnt.size() < 3) return; // nothing to handle
// Determine the bounding box.
std::vector<PointF>::iterator pnt_it = pnt.begin();
double xMin=0.0;
double yMin=0.0;
double xMax=0.0;
double yMax=0.0;
for (; pnt_it != pnt.end(); pnt_it++)
{
double x= (*pnt_it)[0];
double y= (*pnt_it)[1];
if(x<xMin)
xMin=x;
if(x>xMax)
xMax=x;
if(y<yMin)
yMin=y;
if(y>yMax)
yMax=y;
}
double dx = xMax - xMin;
double dy = yMax - yMin;
// Make the bounding box slightly bigger, just to feel safe.
double ddx = dx * 0.01;
double ddy = dy * 0.01;
xMin -= ddx;
xMax += ddx;
dx += 2 * ddx;
yMin -= ddy;
yMax += ddy;
dy += 2 * ddy;
// Create a 'super triangle', encompassing all the vertices. We choose an equilateral triangle with horizontal base.
// We could have made the 'super triangle' simply very big. However, the algorithm is quite sensitive to
// rounding errors, so it's better to make the 'super triangle' just big enough, like we do here.
vertex vSuper[3];
vSuper[0] = vertex(xMin - dy * sqrt3 / 3.0, yMin); // Simple highschool edge, believe me.
vSuper[1] = vertex(xMax + dy * sqrt3 / 3.0, yMin);
vSuper[2] = vertex((xMin + xMax) * 0.5, yMax + dx * sqrt3 * 0.5);
triangle tri(vSuper);
superTri=tri;
// std::cout<<"OK!!!!!!!!"<<"center0="<<superTri.getCenter()[0]<<"\t"<<"center1="<<superTri.getCenter()[1]<<std::endl;
// std::cout<<"OK!!!!!!!!"<<"supTri00="<<((superTri.vtex(0))->point())[0]<<"\t"<<"supTri01="<<((superTri.vtex(0))->point())[1]<<std::endl;
// std::cout<<"OK!!!!!!!!"<<"supTri10="<<((superTri.vtex(1))->point())[0]<<"\t"<<"supTri11="<<((superTri.vtex(1))->point())[1]<<std::endl;
// std::cout<<"OK!!!!!!!!"<<"supTri20="<<((superTri.vtex(2))->point())[0]<<"\t"<<"supTri21="<<((superTri.vtex(2))->point())[1]<<std::endl;
}
这个函数是建立一个大的三角形,得到的这个三角形包含我考虑的所有的点。
但是我调用这个函数以后,在另一个函数里面输出的时候,得到的是错误的结果,
void Delaunay::Triangulate()
{
std::cout<<"Begin to generate the Delaunay triangle mesh!\n";
tri.clear();
buildSuperTriangle();
// std::cout<<"OK!!!!!!!!"<<"center0="<<superTri.getCenter()[0]<<"\t"<<"center1="<<superTri.getCenter()[1]<<std::endl;
std::cout<<"OK!!!!!!!!" << "supTri00=" << (superTri.vtex(0))->GetX() << "\t"<<"supTri01=" << (superTri.vtex(0))->GetY()<<std::endl;
std::cout<<"OK!!!!!!!!" << "supTri10=" << (superTri.vtex(1))->GetX() << "\t"<<"supTri11=" << (superTri.vtex(1))->GetY()<<std::endl;
std::cout<<"OK!!!!!!!!" << "supTri20=" << (superTri.vtex(2))->GetX() << "\t"<<"supTri21=" << (superTri.vtex(2))->GetY()<<std::endl;
tri.push_back(superTri);
}
输出的这个三角形的三个顶点的坐标只有第一个是正确的,后面两个错误,不知道为什么会出现这种情况,烦请高手解答一下。
程序的所有代码见附件!谢谢!
624.tar.gz
(256.75 KB)