src/Processor/Polyline/SimplifyDouglasPeucker.php
protected function douglasPeucker(array $line): array
    {
        $distanceMax = 0;
        $index = 0;

        $lineSize = count($line);

        $pdCalc = new PerpendicularDistance();

        for ($i = 1; $i <= ($lineSize - 2); $i++) {
            $distance = $pdCalc->getPerpendicularDistance($line[$i], new Line($line[0], $line[$lineSize - 1]));

            if ($distance > $distanceMax) {
                $index = $i;
                $distanceMax = $distance;
            }
        }

        if ($distanceMax > $this->tolerance) {
            $lineSplitFirst = array_slice($line, 0, $index + 1);
            $lineSplitSecond = array_slice($line, $index, $lineSize - $index);

            if (count($lineSplitFirst) > 2) {
                $resultsSplit1 = $this->douglasPeucker($lineSplitFirst);
            } else {
                $resultsSplit1 = $lineSplitFirst;
            }

            if (count($lineSplitSecond) > 2) {
                $resultsSplit2 = $this->douglasPeucker($lineSplitSecond);
            } else {
                $resultsSplit2 = $lineSplitSecond;
            }

            array_pop($resultsSplit1);

            return array_merge($resultsSplit1, $resultsSplit2);
        }

        return [$line[0], $line[$lineSize - 1]];
    }
protected function douglasPeucker(array $line): array
    {
        $distanceMax = 0;
        $index = 0;

        $lineSize = count($line);

        $pdCalc = new PerpendicularDistance();

        for ($i = 1; $i <= ($lineSize - 2); $i++) {
            $distance = $pdCalc->getPerpendicularDistance($line[$i], new Line($line[0], $line[$lineSize - 1]));

            if ($distance > $distanceMax) {
                $index = $i;
                $distanceMax = $distance;
            }
        }

        if ($distanceMax > $this->tolerance) {
            $lineSplitFirst = array_slice($line, 0, $index + 1);
            $lineSplitSecond = array_slice($line, $index, $lineSize - $index);

            if (count($lineSplitFirst) > 2) {
                $resultsSplit1 = $this->douglasPeucker($lineSplitFirst);
            } else {
                $resultsSplit1 = $lineSplitFirst;
            }

            if (count($lineSplitSecond) > 2) {
                $resultsSplit2 = $this->douglasPeucker($lineSplitSecond);
            } else {
                $resultsSplit2 = $lineSplitSecond;
            }

            array_pop($resultsSplit1);

            return array_merge($resultsSplit1, $resultsSplit2);
        }

        return [$line[0], $line[$lineSize - 1]];
    }
protected function douglasPeucker ( array $ line ) : array { $ distanceMax = 0 ; $ index = 0 ; $ lineSize = count ( $ line ) ; $ pdCalc = new PerpendicularDistance ( ) ; for ( $ i = 1 ; $ i <= ( $ lineSize - 2 ) ; $ i ++ ) { $ distance = $ pdCalc -> getPerpendicularDistance ( $ line [ $ i ] , new Line ( $ line [ 0 ] , $ line [ $ lineSize - 1 ] ) ) ; if ( $ distance > $ distanceMax ) { $ index = $ i ; $ distanceMax = $ distance ; } } if ( $ distanceMax > $ this -> tolerance ) { $ lineSplitFirst = array_slice ( $ line , 0 , $ index + 1 ) ; $ lineSplitSecond = array_slice ( $ line , $ index , $ lineSize - $ index ) ; if ( count ( $ lineSplitFirst ) > 2 ) { $ resultsSplit1 = $ this -> douglasPeucker ( $ lineSplitFirst ) ; } else { $ resultsSplit1 = $ lineSplitFirst ; } if ( count ( $ lineSplitSecond ) > 2 ) { $ resultsSplit2 = $ this -> douglasPeucker ( $ lineSplitSecond ) ; } else { $ resultsSplit2 = $ lineSplitSecond ; } array_pop ( $ resultsSplit1 ) ; return array_merge ( $ resultsSplit1 , $ resultsSplit2 ) ; } return [ $ line [ 0 ] , $ line [ $ lineSize - 1 ] ] ; }
@param array $line

@return array
@param array $line