多边形中的点为负点给出错误的结果

问题描述 投票:-2回答:1

我正在尝试检查纬度是否在多边形中。这是我的数组:

$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位于该多边形区域中。

我认为函数中的上述算法仅适用于正值。

php polygon point-in-polygon
1个回答
2
投票

我认为您的观点(173.5385,-34.472)在多边形中。

您的x值比最大x顶点大得多。如果绘制它,您可以看到它。您可以从橙色的角度看到,即使只是将经纬度混合在一起,它也仍然不在多边形中。

enter image description here

编辑:

在第一次检查新多边形时,指出它看起来更像是包含在多边形中(x_min

-36.236432, 176.467563
-37.936530, 172.688266
-39.801068, 177.895786
-35.345287,-177.446011
-34.625208,-177.907437 

point:

(-37.0278,176.6158)

但是,再次绘制此图则表明该点在多边形之外:

enter image description here

...并放大...

enter image description here


我在matplotlib中用python绘制了这些内容,建议您在调试此类内容时执行类似的操作。如果您想使用php,html等,则可以改用svg polygonshtml canvas

© www.soinside.com 2019 - 2024. All rights reserved.