javascript - Projecting a point onto a path -
suppose have ordered array containing points (lat, lon)
describing path, , have point (lat, lon)
describing current location.
how can project point onto path (and place point in appropriate place in array)?
what tried searching nearest 2 points , assume it's in middle of them. it's guess, fails.
what way of doing this?
i see this:
p0,p1
path line segment endpointsp
positionq'
closest point on line in 3d cartessianq
q'
corrected spherical projection
so:
- convert points 3d cartessian coordinates
compute perpendicular distance point , line
q'=p0+(dot(p-p0,p1-p0)*(p1-p0)/(|p-p0|*|p1-p0|))
perpendicular_distance = |p-q'|
find segment smallest perpendicular_distance
and use rest of bullets
compute
q
if use sphere instead of ellipsoid know radius if not either compute radius algebraically or use average:
r=0.5*(|p0-(0,0,0)|+|p1-(0,0,0)|)
assuming
(0,0,0)
earth's center. can more precise if weight position:w=|q'-p0|/|p1-p0| r=(1-w)*|p0-(0,0,0)|+w*|p1-(0,0,0)|
now correct position of
q'
q=q'*r/|q'|
set vector
q'
q
sizer
if not obvious enough.|p0-(0,0,0)|=|p0|
wanted sure how got ...convert
q
cartesian spherical coordinates
[notes]
|a|
size of vectora
done like:|a|=sqrt(ax*ax+ay*ay+az*az)
dot(a,b)
dot product of vectorsa,b
done like:dot(a,b)=(a.b)=ax*bx+ay*by+az*bz
if path not complex shaped can use binary search find closest segment. distance comparison not need
sqrt
...
Comments
Post a Comment