Area of a polygon with given n ordered vertices | ( X [ i ] , Y [ i ] ) are coordinates of i 'th point. ; Initialize area ; Calculate value of shoelace formula ; j is previous vertex to i ; Return absolute value ; Driver Code
< ? php function polygonArea ( $ X , $ Y , $ n ) { $ area = 0.0 ; $ j = $ n - 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ area += ( $ X [ $ j ] + $ X [ $ i ] ) * ( $ Y [ $ j ] - $ Y [ $ i ] ) ; $ j = $ i ; } return abs ( $ area / 2.0 ) ; } $ X = array ( 0 , 2 , 4 ) ; $ Y = array ( 1 , 3 , 7 ) ; $ n = sizeof ( $ X ) ; echo polygonArea ( $ X , $ Y , $ n ) ; ? >