Reputation: 1364
The line is:
Ax + By + Cz = D
Ex + Fy + Gz = H
I want any point (x,y,z) that satisfies those equations.
I've tried by choosing one coordinate to set to zero, then solving for the other two. This works correctly except:
1) I'm not sure of a reliable way to choose which coordinate to zero without causing numerical instability when some coefficients are zero or near zero.
2) It involves a lot of if statements which makes the code messy and hard to test all combinations of conditions.
Edit: I don't care which point it finds. It doesn't have to allow all of them to be found.
Upvotes: 0
Views: 1412
Reputation: 1808
i would like to complement jh314's solution:
you can also obtain a point by solving a more complex problem like:
Ax + By + Cz = D
Ex + Fy + Gz = H
(BG-CF)x+(-AG+CE)y+(AF-BE)z = 0
i think that would be numerically more stable
Upvotes: 3
Reputation: 27812
You have two planes, and the intersection is a line. A line is defined by a point and a vector.
To get the vector, you can do the cross product of the normal vectors of the plane.
Ax + By + Cz = D has normal vector <A,B,C>
Ex + Fy + Gz = H has normal vector <E,F,G>
The cross product is
<BG-CF,-AG+CE, AF-BE>
If cross product is <0,0,0>
, the planes are parallel, and no line exists.
Then find a point (a,b,c) in the intersection (by solving your original two equations):
Ax + By + Cz = D
Ex + Fy + Gz = H
To do so, you can suppose that z
is zero. Then check if (A*F-E*B) != 0
. If this is true, then evaluate x,y
:
x = (D*F-B*H)/(A*F-E*B)
y = (E*D-A*H)/(E*B-A*F)
Otherwise, check if (A*G-E*C) != 0
. If so, then you know
x = (D*G-C*H)/(A*G-E*C)
z = (E*D-A*H)/(E*C-A*G)
Otherwise, check if (B*G-C*F) != 0
. If so, then you know
y = (D*G-C*H)/(B*G-C*F)
z = (B*H-D*F)/(B*G-C*F)
Then you have a line!
x = a + (BG-CF)t
y = b + (CE-AG)t
z = c + (AF-BE)t
where t
is your parameter. For any t
you pick, (x,y,z) will be a point on your desired line.
Upvotes: 2