CFLib.org – Common Function Library Project

CalcPolygonArea(data)

Last updated January 27, 2004

author

Tim Dudek

Version: 1 | Requires: CF5 | Library: MathLib

Description:
Purpose = Calcuate the area of an N sided irregular polygon. Formula = Area = 1/2 * ((x1+x2)(y1-y2)+(x2+x3)(y2-y3)+...+(xn+x1)(yn-y1)) Units = Units out are the same units as units in. Input = Array of X,Y points making up the polygon. Array must have at least three items.

Return Values:
Returns a number.

Example:

<cfset p = arrayNew(1)>
<cfset p[1] = structNew()>
<cfset p[1].x = 0>
<cfset p[1].y = 0>
<cfset p[2] = structNew()>
<cfset p[2].x = 5>
<cfset p[2].y = 0>
<cfset p[3] = structNew()>
<cfset p[3].x = 5>
<cfset p[3].y = 5>
<cfset p[4] = structNew()>
<cfset p[4].x = 0>
<cfset p[4].y = 5>

Parameters:

Name Description Required
data Array of structs Yes

Full UDF Source:

/**
 * Calculates the area of an irregular N sided Polygon.
 * 
 * @param data      Array of structs (Required)
 * @return Returns a number. 
 * @author Tim Dudek (tim@igl.net) 
 * @version 1, January 26, 2004 
 */
function CalcPolygonArea(data) {
    var area = "0";
    var i = 1;

    // Check for valid Stucture with at least 3 records
    if(not isArray(data) or arrayLen(data) lte 2) return 0;
    
    data[arrayLen(data)+1] = structNew();
    data[arrayLen(data)].x = data[1].x;
    data[arrayLen(data)].y = data[1].y;
    

    // Loop through the structure performing the area calculation.
    // Formula = Area = 1/2 * ((x1+x2)(y1-y2)+(x2+x3)(y2-y3)+...+(xn+x1)(yn-y1))
    for(; i LT arrayLen(data) ; i=i+1) {
        area = area + ( data[i+1].x-data[i].x) * (data[i+1].y + data[i].y) / 2;
    }
    
    // Only return positive values.
    return abs(area);
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson