VPNkeyXchange/poly/test_poly.awk

47 lines
746 B
Awk
Executable File

#!/usr/bin/awk -f
# USAGE
#
# ./test_poly.awk -- $LAT $LON < PolyhoodCoords.poly
# https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html
function test_intersection(x1,y1,x2,y2,xt,yt) {
return ((y1>yt) != (y2>yt)) && (xt < (x2-x1) * (yt-y1) / (y2-y1) + x1)
}
BEGIN {
lat = ARGV[1]
lon = ARGV[2]
ARGC -= 2
in_poly = 0
}
NR==1{
firstlat = $1
firstlon = $2
prevlat = $1
prevlon = $2
}
NR>1{
curlat = $1
curlon = $2
if (test_intersection(curlon,curlat,prevlon,prevlat,lon,lat)) {
in_poly = !in_poly
}
prevlat = curlat
prevlon = curlon
#printf("%f %f\n", curlat, curlon)
}
END {
if (test_intersection(firstlon,firstlat,prevlon,prevlat,lon,lat)) {
in_poly = !in_poly
}
if (in_poly) {
exit 0
} else {
exit 1
}
}