此例为坐标是否在四边形内,如果是6边型请在判断iSum增加3,
定理:从目标点出发引一条射线,看这条射线和多边形所有边的交点数目。如果有奇数个交点,则说明在内部,如果有偶数个交点,则说明在外部。
export function IsPtInPoly(ALon, ALat, APoints) {
var iSum = 0,
iCount;
var dLon1, dLon2, dLat1, dLat2, dLon;
if (APoints.length < 3) return false;
iCount = APoints.length;
for (var i = 0; i < iCount; i++) {
if (i == iCount - 1) {
dLon1 = APoints[i].lng;
dLat1 = APoints[i].lat;
dLon2 = APoints[0].lng;
dLat2 = APoints[0].lat;
} else {
dLon1 = APoints[i].lng;
dLat1 = APoints[i].lat;
dLon2 = APoints[i + 1].lng;
dLat2 = APoints[i + 1].lat;
}
//以下语句判断A点是否在边的两端点的水平平行线之间,在则可能有交点,开始判断交点是否在左射线上
if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) {
if (Math.abs(dLat1 - dLat2) > 0) {
//得到 A点向左射线与边的交点的x坐标:
dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat)) / (dLat1 - dLat2);
if (dLon < ALon)
iSum++;
}
}
}
console.log(iSum%2)
if (iSum % 2 == 0)
return false;
else if(iSum % 2==2)
return false;
return true;
}