Find 3d coordinates of a point on a line projected

2019-08-18 15:28发布

Working in Swift, ARTKit / SceneKit

enter image description here

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条回答
叼着烟拽天下
2楼-- · 2019-08-18 16:09

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;
查看更多
登录 后发表回答