【发布时间】:2020-08-01 18:27:54
【问题描述】:
我正在尝试检查 lat,lon 是否在多边形中。 这是我的数组:
$vertices_x :
Array
(
[0] => -32.581189
[1] => -38.785885
[2] => -39.26384
[3] => -34.919383
[4] => -32.284464
)
$vertices_y:
Array
(
[0] => 170.643905
[1] => 170.424179
[2] => -178.15004
[3] => -176.524063
[4] => -178.325821
)
$longitude_x : 173.5385
$latitude_y : -34.472
$points_polygon = count($vertices_x) - 1;
我正在使用以下功能进行检查:
function is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y) {
$i = $j = $c = 0;
for ($i = 0, $j = $points_polygon; $i < $points_polygon; $j = $i++) {
if ((($vertices_y[$i] > $latitude_y != ($vertices_y[$j] > $latitude_y)) &&
($longitude_x < ($vertices_x[$j] - $vertices_x[$i]) * ($latitude_y - $vertices_y[$i]) / ($vertices_y[$j] - $vertices_y[$i]) + $vertices_x[$i])))
$c = !$c;
}
return $c;
}
这个函数总是给我 0(不在多边形中),但如果你检查,那么我的点 $longitude_x : 173.5385 , $latitude_y : -34.472 就在那个多边形区域中。
我认为函数中的上述算法仅适用于正值。
【问题讨论】:
-
如您所见here 该算法存在负值问题。该线程上提供了替代方案和解决方案
-
那个帖子也没有用
标签: php polygon point-in-polygon