# Finding intersection of 3D lines

I want to find the intersection of 3D line. I want to detect the 3D light source position by intersecting 3D lines.

lightsource = {0, 0, 250};

p3d1 = {100, 100, 100};
p3d2 = {100, 0, 100};
p3d3 = {0, 100, 100};

d1={500/3, 500/3, 0};
d2={500/3, 0, 0};
d3={0, 500/3, 0};
(*equation of 3 ray*)
ray1 = Flatten[p3d1 + (d1 - p3d1)*t1];
ray2 = Flatten[p3d2 + (d2 - p3d2)*s1];
ray3 = Flatten[p3d3 + (d3 - p3d3)*k1];


I solved the system equation like this AX=B.

eq = ray1 - ray2 - ray3
var = Variables[eq]
A = Coefficient[#, var] & /@ eq;
B = eq - A.var
X = PseudoInverse[A].B (*in this case we can use Inverse[A] because A is invertible.*)
ray1 /. {t1 -> -1}
{100/3, 100/3, 200}(*It is not right*) **What is the problem here?**


(Second method: intersecting two rays by two rays)

eq = ray2 - ray3;
eqp = eq[[1 ;; 2]];
var = Variables[eqp];
coefs = N[Coefficient[#, var] & /@ eqp];
verif =
Chop[eq /. Solve[eqp[[1]] == 0 && eqp[[2]] == 0, var]]
sol = Solve[eqp == {0, 0}, var];
cs1 = Flatten[ray2 /. sol] (*It is true for the 3 rays*).

I am using this graphics

Show[Graphics3D[Cuboid[{0, 0, 0}, {100, 100, 100}]],
Graphics3D[{Red, Thick, Line[{p3d1, d1}]}],
Graphics3D[{Green, Thick, Line[{p3d2, d2}]}],
Graphics3D[{Blue, Thick, Line[{p3d3, d3}]}]]


-
Generally lines do not intersect in 3D. So it is very hard to tell what exactly you are trying to do. Perhaps if you give a rigged example, wherein you indicate both the input and the desired output, it might be easier to follow this. –  Daniel Lichtblau Jan 13 '14 at 21:13
@DanielLichtblau, I think that my example is very clear. I suppose that I have a cube and I have a light source and using 3 rays (+ shadow) as mentioned in the figure, i want to find the center of the source light. –  developer2000 Jan 13 '14 at 21:17
you may check this link and also suggested links as comments mathematica.stackexchange.com/questions/24211/… –  s.s.o Jan 13 '14 at 21:19
You could find the two points, one on each line, that are closest to each other. (Use FindMinimum or geometry.) –  Michael E2 Jan 13 '14 at 21:52

If you minimize the sum of the squares of the distances from an arbitrary point {x, y, z} to a point on each of the lines, you will find a point that is, in some sense, closest to the source. If the given lines are exact and concurrent, then the solution will be the exact point source. If the given lines are approximate, then the solution will be approximate.

p3d1 = {100, 100, 100};
p3d2 = {100, 0, 100};
p3d3 = {0, 100, 100};

d1 = {500/3, 500/3, 0};
d2 = {500/3, 0, 0};
d3 = {0, 500/3, 0};

pts = {d1, d2, d3};
vecs = {p3d1, p3d2, p3d3} - pts;
n = Length[pts];
vars = Array[t, n];

{distsq, sol} = Minimize[
Total[({x, y, z} - Transpose[pts + vecs vars])^2, 2],
{x, y, z} ~Join~ vars]
(*
{0, {x -> 0, y -> 0, z -> 250, t[1] -> 5/2, t[2] -> 5/2, t[3] -> 5/2}}
*)

Graphics3D[{
{Red, Thick, Line[{p3d1, d1}]},
{Green, Thick, Line[{p3d2, d2}]},
{Blue, Thick, Line[{p3d3, d3}]},
{PointSize[Large], Orange, Point[{x, y, z} /. sol]}
}]


-
Here the distance distsq is the square of the real distance. –  developer2000 Jan 16 '14 at 21:41
@developer2000 Yes, as suggested in the opening sentence of the answer, as well as the name of the variable. (Of course, it's not used for anything. One could just use sol = Last@Minimize[..].) –  Michael E2 Jan 16 '14 at 21:54