text: Find the centroid of a non | Javascript implementation of the approach ; For all vertices ; Calculate value of A using shoelace formula ; Calculating coordinates of centroid of polygon ; Coordinate of the vertices
code: function find_Centroid ( v ) { let ans = new Array ( 2 ) ; ans . fill ( 0 ) ; let n = v . length ; let signedArea = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let x0 = v [ i ] [ 0 ] , y0 = v [ i ] [ 1 ] ; let x1 = v [ ( i + 1 ) % n ] [ 0 ] , y1 = v [ ( i + 1 ) % n ] [ 1 ] ; let A = ( x0 * y1 ) - ( x1 * y0 ) ; signedArea += A ; ans [ 0 ] += ( x0 + x1 ) * A ; ans [ 1 ] += ( y0 + y1 ) * A ; } signedArea *= 0.5 ; ans [ 0 ] = ( ans [ 0 ] ) / ( 6 * signedArea ) ; ans [ 1 ] = ( ans [ 1 ] ) / ( 6 * signedArea ) ; return ans ; } let vp = [ [ 1 , 2 ] , [ 3 , - 4 ] , [ 6 , - 7 ] ] ; let ans = find_Centroid ( vp ) ; document . write ( ans [ 0 ] . toFixed ( 11 ) + " " + ans [ 1 ] ) ;