本文转载自:CSDN博客
JS判断点在几何内部
重新编辑我
点击查看详细内容
var checkPoint = [120.94816,31.28292];
var polygonPoints =[
[ 118.2216439660001, 33.940503869000054 ],
[ 118.22536301900004, 33.94065932400002 ],
[ 118.22544694300007, 33.93926268600006 ],
[ 118.22175661300003, 33.939258077000034 ],
[ 118.22175552400006, 33.93926727100006 ],
[ 118.2217219050001, 33.93983108300006 ],
[ 118.22171830600007, 33.93987282300003 ],
[ 118.2217123260001, 33.939914291000036 ],
[ 118.22170407500005, 33.93995548700008 ],
[ 118.22168107000005, 33.94005563700006 ],
[ 118.2216728200001, 33.940096924000045 ],
[ 118.221666948, 33.94013839200005 ],
[ 118.22166324000011, 33.94018013200008 ],
[ 118.2216439660001, 33.940503869000054 ]
];
function isInPolygon(checkPoint, polygonPoints) {
var counter = 0;
var i;
var xinters;
var p1, p2;
var pointCount = polygonPoints.length;
p1 = polygonPoints[0];
for (i = 1; i <= pointCount; i++) {
p2 = polygonPoints[i % pointCount];
if (
checkPoint[0] > Math.min(p1[0], p2[0]) &&
checkPoint[0] <= Math.max(p1[0], p2[0])
) {
if (checkPoint[1] <= Math.max(p1[1], p2[1])) {
if (p1[0] != p2[0]) {
xinters =
(checkPoint[0] - p1[0]) *
(p2[1] - p1[1]) /
(p2[0] - p1[0]) +
p1[1];
if (p1[1] == p2[1] || checkPoint[1] <= xinters) {
counter++;
}
}
}
}
p1 = p2;
}
if (counter % 2 == 0) {
return false;
} else {
return true;
}
}
指的一提的是,参数checkPoint, polygonPoints不论是经纬度坐标还是空间直角坐标系XYZ都能够运行。