交线:矢量函数中的错误
我想计算由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从根本上来说,您的问题是您在分配
point
向量中的位置时没有先分配它们。此外,pointout
没有任何作用,并且您有一个语法错误。以下是我的建议:该行出现两次。删除第一次出现的行,并将第二个实例替换为:
注意
(3)
。没有它,point
就是一个空向量,即它根本没有双精度。有了它,它就有了3个双打。pointout
对象没有任何作用。将这两行: 替换为以下行:
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:This line appears twice. Delete the line the first time it appears, and replace the second instance with:
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:with this line:
我建议使用调试器。您有一些越界访问权限。例如,您可以在点向量为空时访问它。
I suggest using a debugger. You have some out of bounds access. For example, you access the point vector while it's empty.
更改
为
Change
to