.
authorA.M. Thurnherr <ant@ldeo.columbia.edu>
Mon, 11 Jun 2012 13:17:59 -0400
changeset 2 75410953a4d5
parent 1 d17eb00c168b
child 3 55a8c407d38e
.
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 <lat1,lonA> to <lat2,lon2> that
+#	  is closest to target point <lat,lon>
+#   - 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;