private static double distance ( double [ ] p1 , double [ ] p2 ) { switch ( p1 . length ) { case 2 : return Math . sqrt ( ( p1 [ 0 ] - p2 [ 0 ] ) * ( p1 [ 0 ] - p2 [ 0 ] ) + ( p1 [ 1 ] - p2 [ 1 ] ) * ( p1 [ 1 ] - p2 [ 1 ] ) ) ; case 3 : return Math . sqrt ( ( p1 [ 0 ] - p2 [ 0 ] ) * ( p1 [ 0 ] - p2 [ 0 ] ) + ( p1 [ 1 ] - p2 [ 1 ] ) * ( p1 [ 1 ] - p2 [ 1 ] ) + ( p1 [ 2 ] - p2 [ 2 ] ) * ( p1 [ 2 ] - p2 [ 2 ] ) ) ; default : { double dist = 0 ; for ( int i = p1 . length - 1 ; i >= 0 ; i -- ) { final double d = ( p1 [ i ] - p2 [ i ] ) ; dist += d * d ; } return Math . sqrt ( dist ) ; } } }
Compute the Euclidean distance between p1 and p2 .