繁体   English   中英

交叉线:向量函数中的错误

[英]line of intersection: error in vector functions

我想计算由2个平面给出的交叉线。 我的主程序给我,平面参数a,b,-1,d(我的方程是ax + by-z + d = 0)。 所以,我的交线计算方程函数是:

vector<double> LineofIntersection(vector<double> plane1,vector<double> plane2) {
     double a1=plane1.at(0);    double a2=plane2.at(0);
     double b1=plane1.at(1);    double b2=plane2.at(1);
     double d1=plane1.at(2);    double d2=plane2.at(2);
     int c1=-1,c2=-1;
     double cros_x=fabs((b1*c2)-(b2*c1));
     double cros_y=fabs((a2*c1)-(a1*c2));
     double cros_z=fabs((a1*b2)-(a2*b1));
     vector <double> point; vector <double> pointout;

     int maxc;   // max coordinate
     if (cros_x > cros_y){
        if (cros_x > cros_z)
             maxc = 1;
        else maxc = 3;
        }
     else {
        if (cros_y > cros_z)
             maxc = 2;
        else maxc = 3;
        }
     //
     vector <double> point; vector <double> pointout;
     switch (maxc) {  // select max coordinate          
     case 1: // intersect with x=0                   
        point.at(0)=0;
        point.at(1)=(d2-d1)/(b2-b1);
        point.at(2)=(b1*d2-2*b1*d1+d1*b2)/(b2-b1);
        break;
     case 2: // intersect with y=0                   
        point.at(0)=(d2-d1)/(a1-a2);
        point.at(1)=0;
        point.at(2)=(a1*d2-a2*d1)/(a1-a2);
        break;
     case 3: // intersect with z=0                   
        point.at(0)=(b1*d2-b2*d1)/(a1*b2-a2*b1);
        point.at(1)=(a2*d1-a1*d2)/(a1*b2-a2*b1);
        point.at(2)=0;
        break;
     }   
     pointout.push_back(point.at(0));
     pointout.push_back(point.at(1));
     pointout.push_back(point.at(2));
     return pointout;
}

在主程序中,我将此函数称为:

vector<double> linep=LineofIntersection(plane1,plane2);
cout<<linep.at(1)<<" "<<linep.at(1)<<" "<<linep.at(2);

但是,我收到错误消息,无法运行该程序。 请帮忙。

我建议使用调试器。 你有一些超出界限的访问权限。 例如,您可以在空白时访问点矢量。

有趣的是,您的问题是您在没有先分配它们的情况下分配point矢量中的point 此外, pointout没有任何意义,你有一个语法错误。 以下是我的建议:

 vector <double> point; vector <double> pointout;

这一行出现两次。 第一次出现时删除该行,并将第二个实例替换为:

vector <double> point(3);

注意(3) 没有它, point是一个空向量,即它根本没有双精度数。 有了它,它有3个双打。

pointout对象没有任何意义。 替换这两行:

pointout.push_back(point.at(0));pointout.push_back(point.at(1));pointout.push_back(point.at(2));
return pointout;

用这一行:

return point; 

更改

vector <double> point; vector <double> pointout;

vector <double> point(3); // You need to init the size of this!
vector <double> pointout;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM