I know I probably should have asked this question in math.stackexchange but answers there are mostly "pen-paper" type. I need an efficient approach to implement and that is why I am asking here.
Q: Given 2 points A & B and a line L. Points and line are 3D. How to find a point on the given line L which is equidistant from the given points A & B?
The approach I followed is:
finding plane, P, perpendicular to line AB and passing through the center of A & B.
point of intersection of P and L would be the answer.
I am working on large data-set (a 3d image) and doing the above calculations involves large no. of multiplications and divisions (on total).
So is there a better way of doing it.
A working code would be very helpful.
Thanks in advance.
TL;DR. The solution is at the bottom.
Alright, so let's say these are our parameters:
x0 and x1, that describe the line.
a and b, the two points.
Where
x0 = [x0, y0, z0]
x1 = [x1, y1, z1]
a = [xa, ya, za]
b = [xb, yb, zb]
δ = [δx, δy, δz] = (x1 - x0)
Then our description of the line can be seen as a parametric function:
l(λ) = x0 + λ*(x1 - x0)
So we are trying to find a value for λ that satisfies the following equation:
(l(λ) - a)2 = (l(λ) - b)2
(Here I'm cheating with notation a bit, so x2 = x.x)
So expanding everything we get:
(λx1 + (1 - λ)x0 - xa)2 +
(λy1 + (1 - λ)y0 - ya)2 +
(λz1 + (1 - λ)z0 - za)2 =
(λx1 + (1 - λ)x0 - xb)2 +
(λy1 + (1 - λ)y0 - yb)2 +
(λz1 + (1 - λ)z0 - zb)2
Simplifying, we get:
(λδx + x0 - xa)2 +
(λδy + y0 - ya)2 +
(λδz + z0 - za)2 =
(λδx + x0 - xb)2 +
(λδy + y0 - yb)2 +
(λδz + z0 - zb)2
Expanding the brackets and cancelling, we get:
-2δxxaλ + (x0 - xa)2 +
-2δyyaλ + (y0 - ya)2 +
-2δzzaλ + (z0 - za)2 =
-2δxxbλ + (x0 - xb)2 +
-2δyybλ + (y0 - yb)2 +
-2δzzbλ + (z0 - zb)2
Which we can simplify further into some nice neat vector operations:
2λδ.(xb - xa) = (x0 - xb)2 - (x0 - xa)2
Which is pretty simple to rearrange to get a linear equation in λ with one unique solution.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments