Calculating the time for a specific distance between two lines in ℝ3

31 Views Asked by At

I have given two lines in $ℝ3$:

$p1(t)=v1 * t + P1$

$p2(t)=v2 * t + P2$

where $v1, v2$ are the velocities/slopes and $P1, P2$ are the offsets from the origin. I'm trying to write an algorithm that calculates the time $t$ at which the distance between the two points $p1(t)$ and $p2(t)$ is at a specific value.

Given a time $t$, I can calculate the positions $p1(t)$, $p2(t)$ on those two lines. The distance between $p1(t)$ and $p2(t)$ at any given time should be

$d = \sqrt{(p2(t)_x - p1(t)_x)^2 + (p2(t)_y - p1(t)_y)^2 + (p2(t)_z - p1(t)_z)^2}$

or

$d = \sqrt{(v2_xt + P2_x - v1_xt-P1_x)^2 + (v2_yt + P2_y - v1_yt-P1_y)^2 + (v2_zt + P2_z - v1_zt-P1_z)^2}$

If this is correct, then solving the equation for $t$ should give me the formula for calculating the the time $t$ at a given distance $d$.

However, solving this equation for $t$ turned out harder than I thought. Is there a simpler approach to this problem?

2

There are 2 best solutions below

3
On BEST ANSWER

A slight rewriting of your problem:

$$ p(t) = tv + d\\ q(t) = tw + e $$ You want to solve for when $\| p(t) - q(t) \|^2 = K$.

Write $\|u \|^2 = u \cdot u$, and apply this to get $$ \| t(v-w) + (d-e) \|^2 = K $$ To make things easier, let's call $u = v-w$ and $b = (d-e)$. Then we want to find $t$ with $$ \| tu + b \|^2 = K $$ i.e. \begin{align} K &= (tu + b) \cdot (tu + b) \\ &= t(u \cdot u) + 2 t (u \cdot b) + b \cdot b \\ 0 &= t(u \cdot u) + 2 t (u \cdot b) + (b \cdot b - K). \end{align} Now let $$ A = u \cdot u \\ B = 2 (u \cdot b)\\ C = (b \cdot b - K) $$ and you've got a quadratic, $$ 0 = At^2 + Bt + C $$ whose solution is $$ t = \frac{-B \pm \sqrt{B^2 - 4AC}}{2A} $$ I'll let you do the (small) algebraic work to simplify that to something in terms of $u$ and $b$.

Now you also know why folks like to use dot-products rather than writing out individual terms: it helps see the bigger picture.

Notice, too, that the problem of solving this for two lines reduced, after the first couple of steps, to finding when a SINGLE line made its closest approach to the origin; that's a typical thing, and corresponds to the idea in physics that it's often wise to choose a good frame of reference. (In this case, the frame is one that moves along the first line!)

2
On

The first one

$$d = \sqrt{(p_2(t)_x - p_1(t)_x)^2 + (p_2(t)_y - p_1(t)_y)^2 + (p_2(t)_z - p_1(t)_z)^2}$$

is correct by Pythagoras theorem and equivalent to the second one.

To simplify we can simply consider $d^2$ to avoid the square root. I can't see other methods to evaluate $d$.