2
$\begingroup$

This question already has an answer here:

I'm a newbie with Mathematica and I managed to draw a line and a point in 3D on a graph

Q = Graphics3D[Point[{50, 10, 25}]];
line = ParametricPlot3D[{5 + t, 5 + t, 5 + t}, {t, 0, 100}];
Show[Q, line]

not sure if I wrote the parametric line command correctly (I wanted to draw a $P(t) = P + Vt$ parametric line where $P$ is the starting point (5;5;5) and $V$ a unit vector (1;1;1)).

Now I would like to calculate the distance between the point Q and the line. I know how to do this in linear algebra ($d = ||(Q-P)-\frac{(Q-P)\cdot V}{||v||^2} V||$), but I'm unsure on how to do this. Is there a pre-defined function in mathematica to get the distance from a point to a line?

$\endgroup$

marked as duplicate by RunnyKine, Öskå, user9660, MarcoB, Daniel Lichtblau May 6 '16 at 15:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ This answer is related. There is also an implementation of the formula in the question. $\endgroup$ – Anton Antonov May 6 '16 at 13:16
  • $\begingroup$ Check out RegionDistance $\endgroup$ – chuy May 6 '16 at 13:23
  • $\begingroup$ You can also write your parametric formula {5, 5, 5} + t {1, 1, 1}. See also this Q&A -- there are many ways to approach this problem, and many functions one might use (Norm, EuclideanDistance, Dot, Projection,...). $\endgroup$ – Michael E2 May 6 '16 at 13:32
5
$\begingroup$

You can use RegionNearest

pt = {50, 10, 25};
line = Line[Table[{5 + t, 5 + t, 5 + t}, {t, 0, 100}]];
npt = RegionNearest[line, pt];
Graphics3D[{line, Blue, Line[{pt, npt}], Red, Point[{pt, npt}]}]

enter image description here

$\endgroup$
2
$\begingroup$

Is there a pre-defined function in Mathematica to get the distance from a point to a line?

There isn't one, but thankfully for you, both EuclideanDistance[] and Projection[] are built-in:

PointLineDistance[pt_, {s1_, s2_}] :=
                  With[{tp = s1 - pt}, EuclideanDistance[tp, Projection[tp, s2 - s1]]]

Thus,

PointLineDistance[{50, 10, 25}, {{5, 5, 5}, {5, 5, 5} + {1, 1, 1}}]
   35 Sqrt[2/3]
$\endgroup$
2
$\begingroup$
p = {50, 10, 25};
l = InfiniteLine[{5, 5, 5}, {1, 1, 1}];

RegionDistance[l, p]

enter image description here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.