line of intersection: error in vector functions
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.
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;
I suggest using a debugger. You have some out of bounds access. For example, you access the point vector while it's empty.
Change
vector <double> point; vector <double> pointout;
to
vector <double> point(3); // You need to init the size of this!
vector <double> pointout;
精彩评论