0
我想测试Google Maps多段线是否通过Google Maps多边形。听起来很简单。但我已经搜索并搜索......并没有找到真正的答案。此多段线是否穿过此多边形?
最近我得到了这个功能。它的工作原理令人沮丧地回报了偶尔的误报。
//nvert = the number of points in the polygon
//vertx = an array of all the polygon's latitudes
//verty = an array of all the polygon's longitudes
//elat = the current point's latitude
//elng = the current point's longitude
function pnpoly(nvert, vertx, verty, elat, elng) {
var i, j, c = false;
for(i = 0, j = nvert-1; i < nvert; j = i++) {
if(((verty[i] > elng) != (verty[j] > elng)) &&
(elat < (vertx[j] - vertx[i]) * (elng - verty[i])/(verty[j] - verty[i]) + vertx[i])) {
c = !c;
}
}
return c;
}
之前,我尝试了一个全新的方法(crazy math idea这让我回到12年级微积分),我不知道任何人知道如何做到这一点。
回想起来,我认为我的问题是通过这个函数的所有点 - 包括最后一点和第一点在一起。这将会产生一个封闭的多边形,它会返回一个误报。 – podcastfan88 2012-03-31 22:01:21