Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Intersection of two straight lines in 3D space

Subject: Intersection of two straight lines in 3D space

From: James

Date: 11 Mar, 2009 14:56:02

Message: 1 of 6

Hello,

I've got two lines in 3D space, each represented by two points (of x,y,z coords) such as:

A1=[0 0 0];
A2=[5 5 5];

B1=[0 5 5];
B2=[5 0 0];

I would like to be able to calculate the (x,y,z) coordinates at which they intersect. With my own beginners Matlab knowledge and feeble grasp of maths, I have been able to calculate something similar in 2D (by calculating line equations in the form y=mx+c and then solving) but I am stuck at trying to do this in 3D.

Any help would be greatly appreciated, this is a real road block in my work and it is slowly driving me potty!

Subject: Intersection of two straight lines in 3D space

From: Alan Weiss

Date: 11 Mar, 2009 15:33:37

Message: 2 of 6

[2.5 2.5 2.5] (you can do this one in your head)

Represent each line as A1 + t*(A2 - A1) and B1 + s*(B2 - B1).
t = s = 2.5 gets the answer

In general, there is no solution to 2 lines in 3-space, and you could
instead find the point where the sum of squares is minimized. Represent
the sum in terms of t and s as above, differentiate, set to zero, voila!

Alan Weiss
MATLAB mathematical toolbox documentation

James wrote:
> Hello,
>
> I've got two lines in 3D space, each represented by two points (of x,y,z coords) such as:
>
> A1=[0 0 0];
> A2=[5 5 5];
>
> B1=[0 5 5];
> B2=[5 0 0];
>
> I would like to be able to calculate the (x,y,z) coordinates at which they intersect. With my own beginners Matlab knowledge and feeble grasp of maths, I have been able to calculate something similar in 2D (by calculating line equations in the form y=mx+c and then solving) but I am stuck at trying to do this in 3D.
>
> Any help would be greatly appreciated, this is a real road block in my work and it is slowly driving me potty!

Subject: Intersection of two straight lines in 3D space

From: Simon Preston

Date: 11 Mar, 2009 15:35:02

Message: 3 of 6

James,

You don't need Matlab for this. Write your lines in vector form as:
L1 = [0 0 0] + a*[5 5 5]
L2 = [0 5 5] + b*[-5 5 5]
then look for a and b such that each component of L1 and L2 is equal.

Best wishes, S

"James " <mmjpjb2@lboro.ac.uk> wrote in message <gp8je2$80b$1@fred.mathworks.com>...
> Hello,
>
> I've got two lines in 3D space, each represented by two points (of x,y,z coords) such as:
>
> A1=[0 0 0];
> A2=[5 5 5];
>
> B1=[0 5 5];
> B2=[5 0 0];
>
> I would like to be able to calculate the (x,y,z) coordinates at which they intersect. With my own beginners Matlab knowledge and feeble grasp of maths, I have been able to calculate something similar in 2D (by calculating line equations in the form y=mx+c and then solving) but I am stuck at trying to do this in 3D.
>
> Any help would be greatly appreciated, this is a real road block in my work and it is slowly driving me potty!

Subject: Intersection of two straight lines in 3D space

From: John D'Errico

Date: 11 Mar, 2009 17:09:02

Message: 4 of 6

"James " <mmjpjb2@lboro.ac.uk> wrote in message <gp8je2$80b$1@fred.mathworks.com>...
> Hello,
>
> I've got two lines in 3D space, each represented by two points (of x,y,z coords) such as:
>
> A1=[0 0 0];
> A2=[5 5 5];
>
> B1=[0 5 5];
> B2=[5 0 0];
>
> I would like to be able to calculate the (x,y,z) coordinates at which they intersect. With my own beginners Matlab knowledge and feeble grasp of maths, I have been able to calculate something similar in 2D (by calculating line equations in the form y=mx+c and then solving) but I am stuck at trying to do this in 3D.
>
> Any help would be greatly appreciated, this is a real road block in my work and it is slowly driving me potty!

James,

As has been stated, there will generally be no
solution, since two lines will probably not intersect.

Do you wish to find the point of nearest approach?

The line defined by A1 and A2 is (as a function of a
parameter t)

  A(t) = A1*(1-t) + A2*t = A1 + (A2 - A1)*t

Likewise,

  B(s) = B1 + (B2 - B1)*s

The solution to this is best obtained by linear algebra.
Just minimize the difference A(t) - B(s), taken in each
dimension. Since the A1, A2, B1, B2 vectors are all row
vectors, do this:

  ts = [A2(:) - A1(:),-(B2(:)-B1(:))]\(B1(:) - A1(:))
ts =
          0.5
          0.5

The closest point of approach is at

A1 + (A2 - A1)*ts(1)
ans =
          2.5 2.5 2.5

B1 + (B2 - B1)*ts(2)
ans =
          2.5 2.5 2.5

See that in this case you chose lines that did in
fact intersect, but it works on any pair of lines
in any number of dimensions.

John

Subject: Intersection of two straight lines in 3D space

From: Roger Stafford

Date: 12 Mar, 2009 00:16:33

Message: 5 of 6

"James " <mmjpjb2@lboro.ac.uk> wrote in message <gp8je2$80b$1@fred.mathworks.com>...
> .......
> I've got two lines in 3D space, each represented by two points (of x,y,z coords) ...
> ......
> I would like to be able to calculate the (x,y,z) coordinates at which they intersect.
> ......

  As Alan and John have said, your three-dimensional lines may not intersect. In general you can find the two points, A0 and B0, on the respective lines A1A2 and B1B2 which are closest together and determine if they coincide or else see how far apart they lie.

  The following is a specific formula using matlab for determining these closest points A0 and B0 in terms of vector cross products and dot products. Assume that all four points are represented by three-element column vectors or by three-element row vectors. Do the following:

 nA = dot(cross(B2-B1,A1-B1),cross(A2-A1,B2-B1));
 nB = dot(cross(A2-A1,A1-B1),cross(A2-A1,B2-B1));
 d = dot(cross(A2-A1,B2-B1),cross(A2-A1,B2-B1));
 A0 = A1 + (nA/d)*(A2-A1);
 B0 = B1 + (nB/d)*(B2-B1);

  As Alan mentioned, it is evident by its nature that A0 = A1+t*(A2-A1) with t = nA/d a scalar, must fall on the line A1A2, and similarly for B0 = B1+s*(B2-B1) with scalar s = nB/d. You can easily verify that vector A0-B0 is orthogonal to both A2-A1 and B2-B1, which establishes that A0 and B0 are the closest possible pair of points on the two lines. This can be done by evaluating dot(A0-B0,A2-A1) and dot(A0-B0,B2-B1) for various random four-point sets and showing that each dot product is zero, allowing of course for very small round-off errors.

  The one case where the above code becomes indeterminate occurs when the two lines are parallel, in which case there are infinitely many possible pairs for A0 and B0. An arbitrary plane orthogonal to the lines will cut them in a valid pair A0 and B0. For such a case you will get a zero divided by zero situation in the above code which produces a NaN.

Roger Stafford

Subject: Intersection of two straight lines in 3D space

From: James

Date: 19 Mar, 2009 16:48:00

Message: 6 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gp9k91$ri8$1@fred.mathworks.com>...
> "James " <mmjpjb2@lboro.ac.uk> wrote in message <gp8je2$80b$1@fred.mathworks.com>...
> > .......
> > I've got two lines in 3D space, each represented by two points (of x,y,z coords) ...
> > ......
> > I would like to be able to calculate the (x,y,z) coordinates at which they intersect.
> > ......
>
> As Alan and John have said, your three-dimensional lines may not intersect. In general you can find the two points, A0 and B0, on the respective lines A1A2 and B1B2 which are closest together and determine if they coincide or else see how far apart they lie.
>
> The following is a specific formula using matlab for determining these closest points A0 and B0 in terms of vector cross products and dot products. Assume that all four points are represented by three-element column vectors or by three-element row vectors. Do the following:
>
> nA = dot(cross(B2-B1,A1-B1),cross(A2-A1,B2-B1));
> nB = dot(cross(A2-A1,A1-B1),cross(A2-A1,B2-B1));
> d = dot(cross(A2-A1,B2-B1),cross(A2-A1,B2-B1));
> A0 = A1 + (nA/d)*(A2-A1);
> B0 = B1 + (nB/d)*(B2-B1);
>
> As Alan mentioned, it is evident by its nature that A0 = A1+t*(A2-A1) with t = nA/d a scalar, must fall on the line A1A2, and similarly for B0 = B1+s*(B2-B1) with scalar s = nB/d. You can easily verify that vector A0-B0 is orthogonal to both A2-A1 and B2-B1, which establishes that A0 and B0 are the closest possible pair of points on the two lines. This can be done by evaluating dot(A0-B0,A2-A1) and dot(A0-B0,B2-B1) for various random four-point sets and showing that each dot product is zero, allowing of course for very small round-off errors.
>
> The one case where the above code becomes indeterminate occurs when the two lines are parallel, in which case there are infinitely many possible pairs for A0 and B0. An arbitrary plane orthogonal to the lines will cut them in a valid pair A0 and B0. For such a case you will get a zero divided by zero situation in the above code which produces a NaN.
>
> Roger Stafford


Alan, Simon, John, Roger,

Many thanks for your prompt and detailed replies. Between the four of you, you have really helped me out. Cheers!

James

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us