Minimum Sum of Euclidean Distances to all given Points | Function to calculate Euclidean distance ; Function to calculate the minimum sum of the euclidean distances to all polets ; Calculate the centroid ; Calculate distance of all polets ; Initializing the points
function find ( x , y , p ) { let mind = 0 ; for ( let i = 0 ; i < p . length ; i ++ ) { let a = p [ i ] [ 0 ] , b = p [ i ] [ 1 ] ; mind += Math . sqrt ( ( x - a ) * ( x - a ) + ( y - b ) * ( y - b ) ) ; } return mind ; } function getMinDistSum ( p ) { let x = 0 , y = 0 ; for ( let i = 0 ; i < p . length ; i ++ ) { x += p [ i ] [ 0 ] ; y += p [ i ] [ 1 ] ; } x = x / p . length ; y = y / p . length ; let mind = find ( x , y , p ) ; return mind ; } let vec = [ [ 0 , 1 ] , [ 1 , 0 ] , [ 1 , 2 ] , [ 2 , 1 ] ] ; let d = getMinDistSum ( vec ) ; document . write ( d ) ;