地圖區域劃分轉換成數學模型解決問題

計算機與數學是息息相關的,計算機模型中無時無刻不體現數學的理念。例如餘弦定理用來求兩個文案的相似度。今天我這裏解決的問題也與數學有關。實際需求是這樣的,在項目當中,需要人工在百度地圖中劃分配送區域,要求劃分出來的區域不能是雜亂無章的,即線段與線段之間不能相穿。當時接到這個需求有點懵逼,如何是好,開完會坐下來,慢慢畫圖發現劃出的圖形只要是凹凸多邊形即可,突然茅塞頓開,問題迎刃而解,因爲規則的凹凸多邊形的頂點數與邊數相等,根據這段理念簡單寫了一段驗證,結果驗證無誤。

用對象的方式實現代碼邏輯:
<?php
/**
 * 平面點
 *
 * Class Point2D
 */
class Point2D
{
    public $x;
    public $y;

    public function __construct($x, $y)
    {
        $this->x = $x;
        $this->y = $y;
    }

    public function __toString()
    {
        return "({$this->x}, {$this->y})";
    }
}

/**
 * 平面線
 *
 * Class Line2D
 */
class Line2D
{
    /**
     * @var Point2D
     */
    public $p1;

    /**
     * @var Point2D
     */
    public $p2;

    public function __construct(Point2D $p1, Point2D $p2)
    {
        $this->p1 = $p1;
        $this->p2 = $p2;
    }

    public function __toString()
    {
        return "({$this->p1}, {$this->p2})";
    }
}

/**
 * 平面向量
 *
 * Class Vector
 */
class Vector extends Line2D
{
    /**
     * 向量
     *
     * @var Point2D
     */
    public $v;

    public function __construct(Point2D $p1, Point2D $p2)
    {
        parent::__construct($p1, $p2);

        $this->v = new Point2D($p2->x - $p1->x, $p2->y - $p1->y);
    }

    public function __toString()
    {
        return "({$this->p1}->{$this->p2})";
    }

    /**
     * 向量叉積
     *
     * @param Vector $v1
     * @param Vector $v2
     * @return Vector
     */
    public static function mul(Vector $v1, Vector $v2)
    {
        return $v1->v->x * $v2->v->y - $v2->v->x * $v1->v->y;
    }

}

public function checkMapIsRegular($aPoint)
{

       $aFormatPoint   = array();
       $aFormatLine    = array();
       try {
           if ($aPoint) {
               //拆分出座標點,初始化座標對象
               foreach ($aPoint as $item) {
                   list($pointX,$pointY) = explode(',' ,$item);
                   $aFormatPoint[] = array('x' => $pointX, 'y' => $pointY);
               }

               //按順序構成矢量線
               $index  = count($aFormatPoint) - 1;
               foreach ($aFormatPoint as $key=>$point) {
                   $aFormatLine[]['start'] = $point;
                   if ($index == $key) {
                       $aFormatLine[$key]['end']   = $aFormatPoint[0];
                   } else {
                       $aFormatLine[$key]['end']   = $aFormatPoint[$key+1];
                   }
               }

               $aOutPoint      = $aFormatLine;
               $edgeCount      = 0;
               $vertexCount    = count($aOutPoint);
               array_shift($aFormatLine);

               //兩兩求交點
               foreach ($aOutPoint as $key=>$aLine) {
                   if (empty($aFormatLine)) {
                       continue;
                   }
                   foreach ($aFormatLine as $innerKey => $aInnerLine) {
                       //初始化座標對象
                       $oAPointStart   = new Point2D($aOutPoint[$key]['start']['x'],$aOutPoint[$key]['start']['y']);
                       $oAPointEnd     = new Point2D($aOutPoint[$key]['end']['x'],$aOutPoint[$key]['end']['y']);
                       $oBPointStart   = new Point2D($aFormatLine[$innerKey]['start']['x'],$aFormatLine[$innerKey]['start']['y']);
                       $oBPointEnd     = new Point2D($aFormatLine[$innerKey]['end']['x'],$aFormatLine[$innerKey]['end']['y']);

                       //兩兩畫出矢量線
                       $vector         = new Vector($oAPointStart, $oAPointEnd);
                       $vABStart       = new Vector($oAPointStart, $oBPointStart);
                       $vABEnd         = new Vector($oAPointStart, $oBPointEnd);

                       $isInterSect1   = false;
                       $isInterSect2   = false;

                       if (Vector::mul($vABStart, $vector) * Vector::mul($vABEnd, $vector) <= 0) {
                           $isInterSect1   = true;
                       }

                       $oppositeVector = new Vector($oBPointStart, $oAPointStart);
                       $oVABStart      = new Vector($oBPointStart, $oAPointEnd);
                       $oVABEnd        = new Vector($oBPointStart, $oBPointEnd);

                       if (Vector::mul($oppositeVector, $oVABEnd) * Vector::mul($oVABStart, $oVABEnd) <= 0) {
                           $isInterSect2   = true;
                       }

                       if ($isInterSect1 && $isInterSect2) {
                           $edgeCount += 1;
                       }
                   }
                   array_shift($aFormatLine);
               }
           }

           if ($edgeCount == $vertexCount) {
               return true;
           } else {
               return false;
           }

       }catch (\Exception $e) {
           return $e->getMessage();
       }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章