Find 3d coordinates of a point on a line projected from another point in 3d space

戏子无情 提交于 2020-01-06 05:45:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!