//使用二次B样条曲线生成离散点 vector<Point>& Route::bSpline() { double a0,a1,a2; double dt,t1,t2; Point tmp_p; //使首位相连 Point *newp = new Point[num_points+2]; for (int i=0; i<num_points; i++) { newp[i].setXY(p[i].x, p[i].y); } newp[num_points].setXY(p[0].x, p[0].y); newp[num_points+1].setXY(p[1].x, p[1].y); dt=1.0/k; tmp_p.setXY((newp[0].x+newp[1].x)/2,(newp[0].y+newp[1].y)/2); //曲线起始点; positions.push_back(tmp_p); //将结果输出到文件 ofstream outlog("positons.txt"); outlog<<"x= "<<tmp_p.x<<" y= "<<tmp_p.y<<"\n"; for(int i=1;i<num_points+1;i++) { for(int j=0;j<=k;j++) { t1=j*dt; t2=t1*t1; a0=(t2-2*t1+1)/2.0; a1=(2*t1-2*t2+1)/2.0; a2=t2/2.0; double t_x,t_y; t_x=a0*newp[i-1].x+a1*newp[i].x+a2*newp[i+1].x; t_y=a0*newp[i-1].y+a1*newp[i].y+a2*newp[i+1].y; tmp_p.setXY(t_x,t_y); outlog<<"x= "<<tmp_p.x<<" y= "<<tmp_p.y<<"\n"; positions.push_back(tmp_p); } } // positions.pop_back(); outlog.close(); delete [] newp; return positions; }