• 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));
    

      

  • 相关阅读:
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    C语言I博客作业03
    C语言I博客作业02
    第一次作业
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
  • 原文地址:https://www.cnblogs.com/Czc963239044/p/7736164.html
Copyright © 2020-2023  润新知