Location\Polygon::getSegments PHP Method

getSegments() public method

public getSegments ( ) : array
return array
    public function getSegments()
    {
        $segments = [];
        if (count($this->points) <= 1) {
            return $segments;
        }
        $previousPoint = reset($this->points);
        while ($point = next($this->points)) {
            $segments[] = new Line($previousPoint, $point);
            $previousPoint = $point;
        }
        // to close the polygon we have to add the final segment between
        // the last point and the first point
        $segments[] = new Line(end($this->points), reset($this->points));
        return $segments;
    }

Usage Example

Beispiel #1
0
 public function testIfGetSegmentsWorksAsExpected()
 {
     $polygon = new Polygon();
     $point1 = new Coordinate(10, 20);
     $point2 = new Coordinate(10, 40);
     $point3 = new Coordinate(30, 40);
     $point4 = new Coordinate(30, 20);
     $polygon->addPoint($point1);
     $polygon->addPoint($point2);
     $polygon->addPoint($point3);
     $polygon->addPoint($point4);
     $expected = array(new Line($point1, $point2), new Line($point2, $point3), new Line($point3, $point4), new Line($point4, $point1));
     $this->assertEquals($expected, $polygon->getSegments());
 }