gpt4 book ai didi

php - 点对面PHP错误

转载 作者:行者123 更新时间:2023-12-01 13:37:04 24 4
gpt4 key购买 nike

我在php中使用了多边形中点检查功能,但出现重大错误-因为不在多边形中的点在内部出现。

我的基本函数在下面键入(在此处找到,已从类修改为简单函数:http://www.assemblysys.com/dataServices/php_pointinpolygon.php)。我唯一能想到的就是某种舍入错误?

举一个例子,我试图确定一个点是否在中央公园(一个简单的正方形)中,但是我从公园外的点得到了肯定。

感谢您的见解,

-D

$central_park = array('40.768109,-73.981885', '40.800636,-73.958067', '40.796900,-73.949184', '40.764307,-73.972959');

$test_points = array('40.7546755,-73.9758343', '40.764405,-73.973951', '40.7594219,-73.9733896', '40.768137896318315,-73.9814176061', '40.7982394,-73.9523718', '40.685135,-73.973562', '40.7777062,-73.9632719', '40.764109,-73.975948', '40.758908,-73.9813128', '40.7982782,-73.9525028', '40.7463886,-73.9817654', '40.7514592,-73.9760405', '40.7514592,-73.9760155', '40.7514592,-73.9759905', '40.7995079,-73.955431', '40.7604354,-73.9758778', '40.7642878,-73.9730075', '40.7655335,-73.9800484', '40.7521678,-73.9777978', '40.7521678,-73.9777728')

function pointStringToCoordinates($pointString) {
$coordinates = explode(",", $pointString);
return array("x" => trim($coordinates[0]), "y" => trim($coordinates[1]));
}

function isWithinBoundary($point,$polygon){

$point = pointStringToCoordinates($point);

$vertices = array();

foreach ($polygon as $vertex) {
$vertices[] = pointStringToCoordinates($vertex);
}

// Check if the point is inside the polygon or on the boundary
$intersections = 0;
$vertices_count = count($vertices);

for ($i=1; $i < $vertices_count; $i++) {

$vertex1 = $vertices[$i-1];
$vertex2 = $vertices[$i];

if ($vertex1['y'] == $vertex2['y'] and $vertex1['y'] == $point['y'] and $point['x'] > min($vertex1['x'], $vertex2['x']) and $point['x'] < max($vertex1['x'], $vertex2['x'])) { // Check if point is on an horizontal polygon boundary
$result = TRUE;
}

if ($point['y'] > min($vertex1['y'], $vertex2['y']) and $point['y'] <= max($vertex1['y'], $vertex2['y']) and $point['x'] <= max($vertex1['x'], $vertex2['x']) and $vertex1['y'] != $vertex2['y']) {

$xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x']) / ($vertex2['y'] - $vertex1['y']) + $vertex1['x'];

if ($xinters == $point['x']) { // Check if point is on the polygon boundary (other than horizontal)
$result = TRUE;
}

if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) {
$intersections++;
}

}

}

// If the number of edges we passed through is even, then it's in the polygon.
if ($intersections % 2 != 0) {
$result = TRUE;
} else {
$result = FALSE;
}

return $result;

}

最佳答案

原始代码有几个问题,关闭固定了其中一个的多边形,但是代码也给出了多边形边界线上的点的错误结果。仅当点不在边界上时,才需要执行isWithinBoundary函数末尾的if..else语句。由于边界上的点实际上不会与边界相交,因此边界点的相交数始终为奇数,这意味着该最终IF语句对于边界点将始终返回FALSE。

我对代码进行了一些调整,此版本是一个自包含页面,其中包含一些简单的测试数据,并输出正在做出的决策。

<?php
$myPolygon = array('4,3', '4,6', '7,6', '7,3','4,3');

$test_points = array('0,0','1,1','2,2','3,3','3.99999,3.99999','4,4','5,5','6,6','6.99999,5.99999','7,7');
echo "The test polygon has the co-ordinates ";
foreach ($myPolygon as $polypoint){
echo $polypoint.", ";
}
echo "<br/>";
foreach ($test_points as $apoint)
{
echo "Point ".$apoint." is ";
if (!isWithinBoundary($apoint,$myPolygon))
{
echo " NOT ";
}
echo "inside the test polygon<br />";
}

function pointStringToCoordinates($pointString)
{
$coordinates = explode(",", $pointString);
return array("x" => trim($coordinates[0]), "y" => trim($coordinates[1]));
}

function isWithinBoundary($point,$polygon)
{
$result =FALSE;
$point = pointStringToCoordinates($point);
$vertices = array();
foreach ($polygon as $vertex)
{
$vertices[] = pointStringToCoordinates($vertex);
}
// Check if the point is inside the polygon or on the boundary
$intersections = 0;
$vertices_count = count($vertices);
for ($i=1; $i < $vertices_count; $i++)
{
$vertex1 = $vertices[$i-1];
$vertex2 = $vertices[$i];
if ($vertex1['y'] == $vertex2['y'] and $vertex1['y'] == $point['y'] and $point['x'] > min($vertex1['x'], $vertex2['x']) and $point['x'] < max($vertex1['x'], $vertex2['x']))
{
// This point is on an horizontal polygon boundary
$result = TRUE;
// set $i = $vertices_count so that loop exits as we have a boundary point
$i = $vertices_count;
}
if ($point['y'] > min($vertex1['y'], $vertex2['y']) and $point['y'] <= max($vertex1['y'], $vertex2['y']) and $point['x'] <= max($vertex1['x'], $vertex2['x']) and $vertex1['y'] != $vertex2['y'])
{
$xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x']) / ($vertex2['y'] - $vertex1['y']) + $vertex1['x'];
if ($xinters == $point['x'])
{ // This point is on the polygon boundary (other than horizontal)
$result = TRUE;
// set $i = $vertices_count so that loop exits as we have a boundary point
$i = $vertices_count;
}
if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters)
{
$intersections++;
}
}
}
// If the number of edges we passed through is even, then it's in the polygon.
// Have to check here also to make sure that we haven't already determined that a point is on a boundary line
if ($intersections % 2 != 0 && $result == FALSE)
{
$result = TRUE;
}
return $result;
}
?>

您可能自己已经发现并解决了这些问题,但这可能会对其他发现和使用此代码的人有所帮助。

关于php - 点对面PHP错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8040671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com