Find maximum and minimum distance between magnets | Function for finding distance between pivots ; Function for minimum distance ; Function for maximum distance ; Driver code
function pivotDis ( x0 , y0 , x1 , y1 ) { return Math . sqrt ( ( x1 - x0 ) * ( x1 - x0 ) + ( y1 - y0 ) * ( y1 - y0 ) ) ; } function minDis ( D , r1 , r2 ) { return Math . max ( ( D - r1 - r2 ) , 0 ) ; } function maxDis ( D , r1 , r2 ) { return D + r1 + r2 ; } let x0 = 0 , y0 = 0 , x1 = 8 , y1 = 0 , r1 = 4 , r2 = 5 ; let D = pivotDis ( x0 , y0 , x1 , y1 ) ; document . write ( " " + maxDis ( D , r1 , r2 ) + " " ) ; document . write ( " " + minDis ( D , r1 , r2 ) ) ;