You are currently viewing the gmlscripts.pro static mirror. Forum access and script submissions are not available through this mirror.

Invert gmlscripts.pro

polygon_area

The area of a simple concave polygon is one half the sum of the cross products of the adjacent edges.

$$\large A=\frac{1}{2}\sum_{i=0}^{N-1}(x_iy_{i+1}-x_{i+1}y_i)$$

The sign of the result indicates the winding of the polygon, clockwise or counter-clockwise. Holes can be accounted for if the winding of the hole edges is the reverse of the perimeter edges.

NOTE: Be sure to use the absolute value of the return for the true area of the polygon.

polygon_area(polygon)
Returns the internal area of the given polygon.
COPY/// polygon_area(polygon)
//
//  Returns the internal area of the given polygon. The value may
//  positive or negative depending on the winding of the polygon.
//
//      polygon     ds_list of an ordered series of coordinate 
//                  pairs defining the shape of a polygon
//
//  Polygons are closed figures with edges spanning consecutive 
//  vertices and from the last vertex to the first. Polygons must be
//  simple, which means they cannot have edges that cross one another.
//
/// gmlscripts.pro/license
{
    var a, i, j, x1, y1, x2, y2;
    a = 0;
    j = ds_list_size(argument0);
    for (i=0; i<j div 2; i+=1) 
    {
        x1 = ds_list_find_value(argument0, (2*i)   mod j);
        y1 = ds_list_find_value(argument0, (2*i+1) mod j);
        x2 = ds_list_find_value(argument0, (2*i+2) mod j);
        y2 = ds_list_find_value(argument0, (2*i+3) mod j);
        a += x1 * y2 - x2 * y1;
    }
    return a / 2;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw