Maximum distance between two points in coordinate plane using Rotating Caliper 's Method | Function calculates distance between two points ; Function to find the maximum distance between any two points ; Iterate over all possible pairs ; Update maxm ; Return actual distance ; Driver Code Number of points ; Given points ; Function Call
function dist ( p1 , p2 ) { var x0 = p1 [ 0 ] - p2 [ 0 ] ; var y0 = p1 [ 1 ] - p2 [ 1 ] ; return x0 * x0 + y0 * y0 ; } function maxDist ( p ) { var n = p . length ; var maxm = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { maxm = Math . max ( maxm , dist ( p [ i ] , p [ j ] ) ) ; } } return Math . sqrt ( maxm ) ; } var n = 5 ; var p = [ ] ; p . push ( [ 4 , 0 ] ) ; p . push ( [ 0 , 2 ] ) ; p . push ( [ - 1 , - 7 ] ) ; p . push ( [ 1 , 10 ] ) ; p . push ( [ 2 , - 3 ] ) ; document . write ( maxDist ( p ) ) ;