交线:矢量函数中的错误

发布于 2024-10-30 05:51:10 字数 1895 浏览 1 评论 0原文

我想计算由 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);

但是,我收到错误消息,无法运行该程序。请提供任何帮助。

i want to compute intersection line, given by 2 planes. my main program gives me, plane parameters a,b,-1,d (my equation is ax+by-z+d=0). so, my function for computing equation of intersection line is as;

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;
}

in the main program, i call this function as:

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

but, i got error message and cannot run the program. any help please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ㄟ。诗瑗 2024-11-06 05:51:10

从根本上来说,您的问题是您在分配 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; 

Fundementally, your problem is that you are assigning to locations in the point vector without first allocating them. Additionally, pointout serves no purpose, and you have a syntax error. Here are my recommendations:

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

This line appears twice. Delete the line the first time it appears, and replace the second instance with:

vector <double> point(3);

Notice the (3). Without it, point is an empty vector, i.e. it has no doubles in it at all. With it, it has 3 doubles.

The pointout object serves no purpose. Replace these two lines:

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

with this line:

return point; 
你的笑 2024-11-06 05:51:10

我建议使用调试器。您有一些越界访问权限。例如,您可以在点向量为空时访问它。

I suggest using a debugger. You have some out of bounds access. For example, you access the point vector while it's empty.

初熏 2024-11-06 05:51:10

更改

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

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

Change

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

to

vector <double> point(3); // You need to init the size of this!
vector <double> pointout;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文