PHP: How to calculate signed area of a Polygon


Calculating the signed area for an polygon can be found when you know the coordinates of the vertices.

We offer 3 methods below:

Method 1:

function ComputeArea($polygon)
{ 
    $NumPoints = count($polygon);
    
    if($polygon[$NumPoints-1] == $polygon[0]){
        $NumPoints--;
    }else{
        //Add the first point at the end of the array.
        $polygon[$NumPoints] = $polygon[0];
    }
        
    $area = 0;
    
    for( $i = 0; $i <= $NumPoints; ++$i )
      $area += $polygon[$i][0]*( $polygon[$i+1][1] - $polygon[$i-1][1] );
    $area /= 2;
    return $area;
}

Method 2:

function ComputeArea($polygon)
{ 
    $NumPoints = count($polygon);
    
    if($polygon[$NumPoints-1] == $polygon[0]){
        $NumPoints--;
    }else{
        //Add the first point at the end of the array.
        $polygon[$NumPoints] = $polygon[0];
    }
        
    $area = 0;
    
    for( $i = 0; $i < $NumPoints; $i += 2 )
       $area += $polygon[$i+1][0]*($polygon[$i+2][1]-$polygon[$i][1]) + $polygon[$i+1][1]*($polygon[$i][0]-$polygon[$i+2][0]);
    
    $area /= 2;
    return $area;
}

Method 3:

function ComputeArea($polygon)
{ 
    $NumPoints = count($polygon);
    
    if($polygon[$NumPoints-1] == $polygon[0]){
        $NumPoints--;
    }else{
        //Add the first point at the end of the array.
        $polygon[$NumPoints] = $polygon[0];
    }
        
    $area = 0;
    
    for ($i = 0; $i < $NumPoints; $i++) {
      $i1 = ($i + 1) % $NumPoints;
      $area += ($polygon[$i][1] + $polygon[$i1][1]) * ($polygon[$i1][0] - $polygon[$i][0]);
    }
    
    $area /= 2;
    return $area;
}

Method 4:

function ComputeArea($polygon)
{ 
    $NumPoints = count($polygon);
    
    if($polygon[$NumPoints-1] == $polygon[0]){
        $NumPoints--;
    }else{
        //Add the first point at the end of the array.
        $polygon[$NumPoints] = $polygon[0];
    }
    
    if ($NumPoints < 3) {
        return 0;
    } else {
        $area = 0;
        $lastPoint = $polygon[$NumPoints - 1];
        foreach ($polygon as $point) {
            $area += ($lastPoint[0] * $point[1] - $lastPoint[1] * $point[0]);
            $lastPoint = $point;
        }
        return ($area / 2.0);
    }     
}

Note: If you list the points in a clockwise order instead of counterclockwise, you will get the negative of the area. Hence this can be used as a tool to identify the cyclic path or sequence of a given set of points forming a polygon.

To get positive value, you can use abs function.

function ComputeArea2($polygon){
    return abs(ComputeArea($polygon));
}

Example 1:

$polygon = array(
    array(0,0),
    array(4,0),
    array(2,3),
    array(4,6),
    array(0,6),
);

echo ComputeArea($polygon);

Result:

18

Example 2:

$polygon = array(
    array(0,6),
    array(4,6),
    array(2,3),
    array(4,0),
    array(0,0),
);

echo ComputeArea($polygon);

Result:

-18

Note: Shoelace formula apply for simple polygon (non-self-intersecting polygon).

Leave a Reply