多边形中的点算法对负点给出错误的结果 [英] Point in Polygon algorithm giving wrong results for negative points
问题描述
我正在尝试检查纬度是否在多边形中. 这是我的数组:
I am trying to check if the lat,lon is in the polygon or not. Here is my array :
$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;
我正在使用以下功能进行检查:
I am using below function to check :
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
在该多边形区域中.
And this function always gives me 0 (Not in polygon) but if you check then my point $longitude_x : 173.5385 , $latitude_y : -34.472
is in that polygon area.
我认为函数中的上述算法仅适用于正值.
I think the above algorithm in the function is only work with positive values.
推荐答案
我认为您的观点(173.5385,-34.472)不在多边形中.
I don't think your point (173.5385,-34.472) is in the polygon.
您的x值比最大x顶点大得多.如果绘制它,您可以看到它.从橙色点可以看到,即使只是将经纬度混合在一起,它也仍然不在多边形中.
Your x value is much larger than the largest x vertex. You can see this if you plot it. You can see from the orange point that even if you just have your lat/long mixed up it's still not in the polygon.
在第一次检查新多边形时,指出该多边形似乎更可能包含在该多边形中(x_min< x< x_max和y_min< y< y_max).
Upon first inspection of your new polygon and point it looks as though this one is more likely to be contained within the polygon (x_min < x < x_max and y_min < y < y_max).
-36.236432, 176.467563
-37.936530, 172.688266
-39.801068, 177.895786
-35.345287,-177.446011
-34.625208,-177.907437
点:
(-37.0278,176.6158)
但是,再次绘制它表明该点在多边形之外:
However, plotting this again reveals that the point is outside the polygon:
...并放大...
... and zooming in ...
我使用 matplotlib 在python中绘制了这些图形,我建议您在调试此类内容时执行类似的操作.如果您想使用php,html等,可以使用 svg多边形或 html canvas 代替.
I plotted these in python with matplotlib and I recommend you do something similar when debugging this type of thing. If you want to stay with php, html, etc., you could use svg polygons or html canvas instead.
这篇关于多边形中的点算法对负点给出错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!