Skip to main content

Another way of doing this (http://mathforum.org/library/drmath/view/51980.htmlLink) is to find the mutual perpendicular between the two lines using the cross product, converting this to a unit vector, and then using the dot product between that cross product, and any vector going between the two lines. Like this:

newMinDist[{p1_, p2_}, {q1_, q2_}] :=
 Module[
  {u, v, n, w},
  u = p2 - p1;
  v = q2 - q1;
  n = Normalize[Cross[u,v]];
  w = q1 - p1;
  Dot[w,n]
  ]

Again, u and v are vectors headed along the lines. The vector n is a unit vector in the direction of the cross product of u and v (the unit vector normal to both u and v). The w is any vector between the ps and the qs. You could swap any value of 1 or 2 here (for instance q2-p1). Then w.n gives the shortest distance between the lines.

Gives the same result, and Timing shows this method is better than an order of magnitude faster than the previous one.

Another way of doing this (http://mathforum.org/library/drmath/view/51980.html) is to find the mutual perpendicular between the two lines using the cross product, converting this to a unit vector, and then using the dot product between that cross product, and any vector going between the two lines. Like this:

newMinDist[{p1_, p2_}, {q1_, q2_}] :=
 Module[
  {u, v, n, w},
  u = p2 - p1;
  v = q2 - q1;
  n = Normalize[Cross[u,v]];
  w = q1 - p1;
  Dot[w,n]
  ]

Again, u and v are vectors headed along the lines. The vector n is a unit vector in the direction of the cross product of u and v (the unit vector normal to both u and v). The w is any vector between the ps and the qs. You could swap any value of 1 or 2 here (for instance q2-p1). Then w.n gives the shortest distance between the lines.

Gives the same result, and Timing shows this method is better than an order of magnitude faster than the previous one.

Another way of doing this (Link) is to find the mutual perpendicular between the two lines using the cross product, converting this to a unit vector, and then using the dot product between that cross product, and any vector going between the two lines. Like this:

newMinDist[{p1_, p2_}, {q1_, q2_}] :=
 Module[
  {u, v, n, w},
  u = p2 - p1;
  v = q2 - q1;
  n = Normalize[Cross[u,v]];
  w = q1 - p1;
  Dot[w,n]
  ]

Again, u and v are vectors headed along the lines. The vector n is a unit vector in the direction of the cross product of u and v (the unit vector normal to both u and v). The w is any vector between the ps and the qs. You could swap any value of 1 or 2 here (for instance q2-p1). Then w.n gives the shortest distance between the lines.

Gives the same result, and Timing shows this method is better than an order of magnitude faster than the previous one.

added 38 characters in body
Source Link

Another way of doing this (http://mathforum.org/library/drmath/view/51980.html) is to find the mutual perpendicular between the two lines using the cross product, converting this to a unit vector, and then using the dot product between that cross product, and any vector going between the two lines. Like this:

newMinDist[{p1_, p2_}, {q1_, q2_}] := Module[ {u, v, n, w}, u = Normalize[p2 - p1]; v = Normalize[q2 - q1]; n = Normalize[Cross[u,v]]; w = q1 - p1; Dot[w,n] ]

newMinDist[{p1_, p2_}, {q1_, q2_}] :=
 Module[
  {u, v, n, w},
  u = p2 - p1;
  v = q2 - q1;
  n = Normalize[Cross[u,v]];
  w = q1 - p1;
  Dot[w,n]
  ]

Again, u and v are unit vectors headed along the lines. The vector n is a unit vector in the direction of the cross product of u and v (the unit vector normal to both u and v). The w is any vector between the ps and the qs. You could swap any value of 1 or 2 here (for instance q2-p1). Then w.n gives the shortest distance between the lines.

Gives the same result, and timingTiming shows this method is better than an order of magnitude faster than the previous one.

Another way of doing this (http://mathforum.org/library/drmath/view/51980.html) is to find the mutual perpendicular between the two lines using the cross product, converting this to a unit vector, and then using the dot product between that cross product, and any vector going between the two lines. Like this:

newMinDist[{p1_, p2_}, {q1_, q2_}] := Module[ {u, v, n, w}, u = Normalize[p2 - p1]; v = Normalize[q2 - q1]; n = Normalize[Cross[u,v]]; w = q1 - p1; Dot[w,n] ]

Again, u and v are unit vectors headed along the lines. The vector n is a unit vector in the direction of the cross product of u and v (the unit vector normal to both u and v). The w is any vector between the ps and the qs. You could swap any value of 1 or 2 here (for instance q2-p1). Then w.n gives the shortest distance between the lines.

Gives the same result, and timing shows this method is better than an order of magnitude faster than the previous one.

Another way of doing this (http://mathforum.org/library/drmath/view/51980.html) is to find the mutual perpendicular between the two lines using the cross product, converting this to a unit vector, and then using the dot product between that cross product, and any vector going between the two lines. Like this:

newMinDist[{p1_, p2_}, {q1_, q2_}] :=
 Module[
  {u, v, n, w},
  u = p2 - p1;
  v = q2 - q1;
  n = Normalize[Cross[u,v]];
  w = q1 - p1;
  Dot[w,n]
  ]

Again, u and v are vectors headed along the lines. The vector n is a unit vector in the direction of the cross product of u and v (the unit vector normal to both u and v). The w is any vector between the ps and the qs. You could swap any value of 1 or 2 here (for instance q2-p1). Then w.n gives the shortest distance between the lines.

Gives the same result, and Timing shows this method is better than an order of magnitude faster than the previous one.

Source Link

Another way of doing this (http://mathforum.org/library/drmath/view/51980.html) is to find the mutual perpendicular between the two lines using the cross product, converting this to a unit vector, and then using the dot product between that cross product, and any vector going between the two lines. Like this:

newMinDist[{p1_, p2_}, {q1_, q2_}] := Module[ {u, v, n, w}, u = Normalize[p2 - p1]; v = Normalize[q2 - q1]; n = Normalize[Cross[u,v]]; w = q1 - p1; Dot[w,n] ]

Again, u and v are unit vectors headed along the lines. The vector n is a unit vector in the direction of the cross product of u and v (the unit vector normal to both u and v). The w is any vector between the ps and the qs. You could swap any value of 1 or 2 here (for instance q2-p1). Then w.n gives the shortest distance between the lines.

Gives the same result, and timing shows this method is better than an order of magnitude faster than the previous one.