Projecting a point onto a path

自古美人都是妖i 提交于 2019-11-29 12:56:41

I see it like this:

  • p0,p1 are path line segment endpoints
  • p is your position
  • q' closest point on line in 3D cartessian
  • q is q' corrected by spherical projection

So:

  1. convert points to 3D cartessian coordinates
  2. compute perpendicular distance from point and line

    • q'=p0+(dot(p-p0,p1-p0)*(p1-p0)/(|p-p0|*|p1-p0|))
    • perpendicular_distance = |p-q'|
  3. find segment with smallest perpendicular_distance

    and use only it for the rest of bullets

  4. compute q

    If you use sphere instead of ellipsoid then you already know the radius if not then either compute the radius algebraically or use average:

    r=0.5*(|p0-(0,0,0)|+|p1-(0,0,0)|)
    

    assuming (0,0,0) is Earth's center. You can also be more precise if you weight by position:

    w=|q'-p0|/|p1-p0|
    r=(1-w)*|p0-(0,0,0)|+w*|p1-(0,0,0)|
    

    now just correct the position of q'

    q=q'*r/|q'|
    

    set vector q' as q with size r if it is not obvious enough. Also |p0-(0,0,0)|=|p0| obviously but I wanted to be sure you get how I got it ...

  5. convert q from Cartesian to spherical coordinates

[Notes]

  • |a| is size of vector a done like: |a|=sqrt(ax*ax+ay*ay+az*az)
  • dot(a,b) is dot product of vectors a,b done like: dot(a,b)=(a.b)=ax*bx+ay*by+az*bz

    if your path is not too complex shaped then you can use binary search to find the closest segment. For distance comparison you do not need the sqrt ...

Find the path segment which is closest to the current position.

Distance from point to a line

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