我有一个数组(多边形)
[[-24.837452, -65.452484],[-24.837433, -65.450645],[-24.828865000000004, -65.449997],[-24.828709, -65.451607],[-24.837452, -65.452484]]
并且点-24.8330000,-65.452484
如何检测点是否在多边形内?
如果使用示例数组,可以正常工作。 但如果使用真实坐标,就无法正常工作...
<?php
class pointLocation {
var $pointOnVertex = true; // Checar si el punto se encuentra exactamente en uno de los vértices?
function pointLocation() {
}
function pointInPolygon($point, $polygon, $pointOnVertex = true) {
$this->pointOnVertex = $pointOnVertex;
// Transformar la cadena de coordenadas en matrices con valores "x" e "y"
$point = $this->pointStringToCoordinates($point);
$vertices = array();
foreach ($polygon as $vertex) {
$vertices[] = $this->pointStringToCoordinates($vertex);
}
// Checar si el punto se encuentra exactamente en un vértice
if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) {
return "vertex";
}
// Checar si el punto está adentro del poligono o en el borde
$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'])) { // Checar si el punto está en un segmento horizontal
return "boundary";
}
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']) { // Checar si el punto está en un segmento (otro que horizontal)
return "boundary";
}
if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) {
$intersections++;
}
}
}
// Si el número de intersecciones es impar, el punto está dentro del poligono.
if ($intersections % 2 != 0) {
return "inside";
} else {
return "outside";
}
}
function pointOnVertex($point, $vertices) {
foreach($vertices as $vertex) {
if ($point == $vertex) {
return true;
}
}
}
function pointStringToCoordinates($pointString) {
$coordinates = explode(" ", $pointString);
return array("x" => $coordinates[0], "y" => $coordinates[1]);
}
}
//original examples is OK
//$points = array("50 70","70 40","-20 30","100 10","-10 -10","40 -20","110 -20");
//$polygon = array("-50 30","50 70","100 50","80 10","110 -10","110 -30","-20 -50","-30 -40","10 -10","-10 10","-30 -20","-50 30");
$json="[[-24.837452, -65.452484],[-24.837433, -65.450645],[-24.828865000000004, -65.449997],[-24.828709, -65.451607],[-24.837452, -65.452484]]";
$resultado = str_replace(",", "", $json);
$resultado = str_replace("[[", "", $resultado);
$resultado = str_replace("]]", "", $resultado);
$polygon=explode("][", $resultado);
$points=array("-24.8330000 -65.452484");
//print_r ($resultado);
$pointLocation = new pointLocation();
// Las últimas coordenadas tienen que ser las mismas que las primeras, para "cerrar el círculo"
foreach($points as $key => $point) {
echo "point " . ($key+1) . " ($point): " . $pointLocation->pointInPolygon($point, $polygon) . "<br>";
}
?>
这些数据是真实的,且该点位于多边形内部。
[[-24.837452, -65.452484],[-24.837433, -65.450645],[-24.828865000000004, -65.449997],[-24.828709, -65.451607],[-24.837452, -65.452484]]
该点为-24.8330000,-65.452484。