php 判断一个点是否在一个多边形区域内

<?php

class pointMap{
 private static $coordArray;
 private static $vertx = [];
 private static $verty = [];
 

 public static function setArray(array $Array)
 {
  self::$coordArray = $Array;
 }


 public static function isCenter(array $testarray){
  if(!self::vaildatePoint($testarray)){
   return false;
  }

  return self::intra(count(self::$coordArray), $testarray['lng'], $testarray['lat']);
 }



 private static function intra($n,$testx, $testy)
 {

   $c = false;
   for ($i = 0, $j = $n-1; $i < $nvert; $j = $i++) {
   if ( ( (self::$verty[$i]>$testy) != (self::$verty[$j]>$testy) ) && ($testx < (self::$vertx[$j]-self::$vertx[$i]) * ($testy-self::$verty[$i]) / (self::$verty[$j]-self::$verty[$i]) + self::$vertx[$i]) )
    $c = !$c;
  }
  return $c;
 }


 private static function vaildatePoint(array $pointArray){
  $maxY = $maxX = 0;
  $minY = $minX = 9999;
  foreach (self::$coordArray as $item){
   if($item['lng']>$maxX) $maxX = $item['lng'];
   if($item['lng'] < $minX) $minX = $item['lng'];
   if($item['lat']>$maxY) $maxY = $item['lat'];
   if($item['lat'] < $minY) $minY = $item['lat'];
   self::$vertx[] = $item['lng'];
   self::$verty[] = $item['lat'];
  }
  if ($pointArray['lng'] < $minX || $pointArray['lng'] > $maxX || $pointArray['lat'] < $minY || $pointArray['lat'] > $maxY) {
   return false;
  }
  return true;
 }
}

$map = [ 
 ["lng" => 0.0, "lat" => 0.0],
 ["lng" => 0.0, "lat" => 1.0],
 ["lng" => 0.0, "lat" => 2.0],
 ["lng" => 1.0, "lat" => 2.0],
 ["lng" => 2.0, "lat" => 2.0],
 ["lng" => 2.0, "lat" => 1.0],
 ["lng" => 2.0, "lat" => 0.0],
];

$array = ["lat"=>2.0,"lng"=>6.0];

pointMap::setArray($map);
var_dump(pointMap::isCenter($array));

  

posted @ 2017-10-26 12:22  czcColud  阅读(806)  评论(0编辑  收藏  举报