# HG changeset patch # User A.M. Thurnherr # Date 1339435079 14400 # Node ID 75410953a4d5f5eba4495d633103bc4bf2f33293 # Parent d17eb00c168b71ac04b43767f4c524947c33d2ee . diff --git a/libvec.pl b/libvec.pl --- a/libvec.pl +++ b/libvec.pl @@ -1,9 +1,9 @@ #====================================================================== -# L I B V E C . P L +# . . / L I B / L I B V E C . P L # doc: Sat Mar 20 12:50:32 1999 -# dlm: Thu Apr 22 11:32:54 2010 +# dlm: Tue Jun 5 08:38:52 2012 # (c) 1999 A.M. Thurnherr -# uE-Info: 147 34 NIL 0 0 72 2 2 4 NIL ofnI +# uE-Info: 318 0 NIL 0 0 70 2 2 4 NIL ofnI #====================================================================== # HISTORY: @@ -31,6 +31,7 @@ # Jun 14, 2009: - added p_vel() # Nov 5, 2009: - added angle(); vel_bias() => angle_diff() # Apr 22, 2010: - added angle_ts() +# Jun 5, 2012: - added &closestPointOnStraightLine() require "$ANTS/libPOSIX.pl"; # acos() @@ -297,4 +298,24 @@ (&dist($S,$W,$N,$W) + &dist($S,$E,$N,$E)) / 2; } +#---------------------------------------------------------------------- +# &closestPointOnStraightLine(lat,lon,lat1,lonA,lat2,lon2) +# - determine point on line segment from to that +# is closest to target point +# - http://stackoverflow.com/questions/3120357/get-closest-point-to-a-line +# - NOT DONE IN PLANAR GEOMETRY => USE ONLY IN SMALL DOMAINS +#---------------------------------------------------------------------- + +sub closestPointOnStraightLine(@) +{ + my($latP,$lonP,$latA,$lonA,$latB,$lonB) = + &antsFunUsage(6,'ffffff','pnt_lat, pnt_lon, lne_latA, lne_lonA, lne_latB, lne_lonB',@_); + + my($ABlon) = $lonB - $lonA; my($ABlat) = $latB - $latA; + my($APlon) = $lonP - $lonA; my($APlat) = $latP - $latA; + my($t) = ($APlon*$ABlon + $APlat*$ABlat) / ($ABlon**2 + $ABlat**2); + return (undef,undef) unless ($t>=0 && $t<=1); + return ($latA + $t*$ABlat,$lonA + $t*$ABlon); +} + 1;