问题
Working in Swift, ARTKit / SceneKit
I have a line AB in 3d and I have xyz coordinates of both points A and B. I also have a point C and I know its xyz coordinates too.
Now, I want to find out the xyz coordinates of point D on line AB; given that CD is perpendicular to AB.
What would be a simple way to do it in Swift.
回答1:
Parameterize the line AB
with a scalar t
:
P(t) = A + (B - A) * t`
The point D = P(t)
is such that CD
is perpendicular to AB
, i.e. their dot product is zero:
dot(C - D, B - A) = 0
dot(C - A - (B - A) * t, B - A) = 0
dot(C - A, B - A) = t * dot(B - A, B - A)
// Substitute value of t
--> D = A + (B - A) * dot(C - A, B - A) / dot(B - A, B - A)
Swift code:
var BmA = B - A
var CmA = C - A
var t = dot(CmA, BmA) / dot(BmA, BmA)
var D = A + BmA * t;
来源:https://stackoverflow.com/questions/52822663/find-3d-coordinates-of-a-point-on-a-line-projected-from-another-point-in-3d-spac