Minimum Time Taken for N Points to coincide | Java program for the above approach ; Function to check for common area between three circles ; Find the distance between the centers of circle 1 & circle 2 ; Find the distance between the centers of circle 1 & circle 3 ; Find the distance between the centers of circle 2 & circle 3 ; If sum of radius is less than the distance between their centers then false ; If circle 1 lies within circle 2 or if circle 2 lies within circle 1 ; If circle 1 lies within circle 2 ; Check whether R1 ( common area between R1 and R2 ) and R3 intersect ; Check whether R2 ( common area between R1 and R2 ) and R3 intersect ; If circle 1 lies within circle 3 or if circle 3 lies within circle 1 ; If circle 1 lies within circle 3 ; Check whether R1 ( common area between R1 and R3 ) and R2 intersect ; Check whether R3 ( common area between R1 and R3 ) and R2 intersect ; If circle 2 lies within circle 3 or if circle 3 lies within circle 2 ; If circle 2 lies within circle 3 ; Check whether R2 ( common area between R2 and R3 ) and R1 intersect ; Check whether R3 ( common area between R2 and R3 ) and R1 intersect ; Find the point of intersection for circle 1 & circle 2 ; First point of intersection ( x121 , y121 ) ; Check whether the point of intersection lies within circle 3 or not ; Second point of intersection ( x122 , y122 ) ; Check whether the point of intersection lies within circle 3 or not ; Find the point of intersection for circle 1 & circle 3 ; First point of intersection ( x131 , y131 ) ; Check whether the point of intersection lies within circle 2 or not ; Second point of intersection ( x132 , y132 ) ; Check whether the point of intersection lies within circle 2 or not ; Find the point of intersection for circle 2 & circle 3 ; First point of intersection ( x231 , y231 ) ; Check whether the point of intersection lies within circle 1 or not ; Second point of intersection ( x232 , y232 ) ; Check whether the point of intersection lies within circle 1 or not ; Function to check if there is a common area between N circles ; Check for a common area for all combination of 3 circles ; t * V give the radius of the circle ; For N = 2 ; ( x2 - x1 ) ^ 2 + ( y2 - y1 ) ^ 2 <= ( r1 + r2 ) ^ 2 for 2 circles to intersect ; Function to find minimum time ; Time depends on the area of the 2D plane Area = ( Max ( X ) - Min ( X ) ) * ( Max ( Y ) - Min ( Y ) ) ; Number of iteration depends on the precision ; If there is a common area between N circles for time t ; If there is no common area between N circles for time t ; Print the minimum time ; Driver Code ; Function Call
class GFG { public static boolean intersection ( int X1 , int Y1 , double R1 , int X2 , int Y2 , double R2 , int X3 , int Y3 , double R3 ) { double d12 = Math . sqrt ( ( X1 - X2 ) * ( X1 - X2 ) + ( Y1 - Y2 ) * ( Y1 - Y2 ) ) ; double d13 = Math . sqrt ( ( X1 - X3 ) * ( X1 - X3 ) + ( Y1 - Y3 ) * ( Y1 - Y3 ) ) ; double d23 = Math . sqrt ( ( X2 - X3 ) * ( X2 - X3 ) + ( Y2 - Y3 ) * ( Y2 - Y3 ) ) ; if ( ( R1 + R2 < d12 ) || ( R1 + R3 < d13 ) || ( R2 + R3 < d23 ) ) { return false ; } else { if ( Math . abs ( R1 - R2 ) >= d12 ) { if ( R1 < R2 ) { return R1 + R3 >= d13 ; } else { return R2 + R3 >= d23 ; } } else if ( Math . abs ( R1 - R3 ) >= d13 ) { if ( R1 < R3 ) { return R1 + R2 >= d12 ; } else { return R2 + R3 >= d23 ; } } else if ( Math . abs ( R2 - R3 ) >= d23 ) { if ( R2 < R3 ) { return R1 + R2 >= d12 ; } else { return R1 + R3 >= d13 ; } } else { double x121 , y121 , x122 , y122 , x131 , y131 , x132 , y132 , x231 , y231 , x232 , y232 , a , b ; a = ( R1 * R1 - R2 * R2 ) / ( 2 * d12 * d12 ) ; b = Math . sqrt ( 2 * ( R1 * R1 + R2 * R2 ) / ( d12 * d12 ) - ( R1 * R1 - R2 * R2 ) * ( R1 * R1 - R2 * R2 ) / ( Math . pow ( d12 , 4 ) ) - 1 ) / 2 ; x121 = ( X1 + X2 ) / 2.0 + a * ( X2 - X1 ) + b * ( Y2 - Y1 ) ; y121 = ( Y1 + Y2 ) / 2.0 + a * ( Y2 - Y1 ) + b * ( X1 - X2 ) ; if ( R3 >= Math . sqrt ( ( x121 - X3 ) * ( x121 - X3 ) + ( y121 - Y3 ) * ( y121 - Y3 ) ) ) { return true ; } x122 = ( X1 + X2 ) / 2.0 + a * ( X2 - X1 ) - b * ( Y2 - Y1 ) ; y122 = ( Y1 + Y2 ) / 2.0 + a * ( Y2 - Y1 ) - b * ( X1 - X2 ) ; if ( R3 >= Math . sqrt ( ( x122 - X3 ) * ( x122 - X3 ) + ( y122 - Y3 ) * ( y122 - Y3 ) ) ) { return true ; } a = ( R1 * R1 - R3 * R3 ) / ( 2 * d13 * d13 ) ; b = Math . sqrt ( 2 * ( R1 * R1 + R3 * R3 ) / ( d13 * d13 ) - ( R1 * R1 - R3 * R3 ) * ( R1 * R1 - R3 * R3 ) / ( Math . pow ( d13 , 4 ) ) - 1 ) / 2 ; x131 = ( X1 + X3 ) / 2.0 + a * ( X3 - X1 ) + b * ( Y3 - Y1 ) ; y131 = ( Y1 + Y3 ) / 2.0 + a * ( Y3 - Y1 ) + b * ( X1 - X3 ) ; if ( R2 >= Math . sqrt ( ( x131 - X2 ) * ( x131 - X2 ) + ( y131 - Y2 ) * ( y131 - Y2 ) ) ) { return true ; } x132 = ( X1 + X3 ) / 2.0 + a * ( X3 - X1 ) - b * ( Y3 - Y1 ) ; y132 = ( Y1 + Y3 ) / 2.0 + a * ( Y3 - Y1 ) - b * ( X1 - X3 ) ; if ( R2 >= Math . sqrt ( ( x132 - X2 ) * ( x132 - X2 ) + ( y132 - Y2 ) * ( y132 - Y2 ) ) ) { return true ; } a = ( R2 * R2 - R3 * R3 ) / ( 2 * d23 * d23 ) ; b = Math . sqrt ( 2 * ( R2 * R2 + R3 * R3 ) / ( d23 * d23 ) - ( R2 * R2 - R3 * R3 ) * ( R2 * R2 - R3 * R3 ) / ( Math . pow ( d23 , 4 ) ) - 1 ) / 2 ; x231 = ( X2 + X3 ) / 2.0 + a * ( X3 - X2 ) + b * ( Y3 - Y2 ) ; y231 = ( Y2 + Y3 ) / 2.0 + a * ( Y3 - Y2 ) + b * ( X2 - X3 ) ; if ( R1 >= Math . sqrt ( ( x231 - X1 ) * ( x231 - X1 ) + ( y231 - Y1 ) * ( y231 - Y1 ) ) ) { return true ; } x232 = ( X2 + X3 ) / 2.0 + a * ( X3 - X2 ) - b * ( Y3 - Y2 ) ; y232 = ( Y2 + Y3 ) / 2.0 + a * ( Y3 - Y2 ) - b * ( X2 - X3 ) ; return R1 >= Math . sqrt ( ( x232 - X1 ) * ( x232 - X1 ) + ( y232 - Y1 ) * ( y232 - Y1 ) ) ; } } } public static boolean isGood ( double t , int N , int [ ] X , int [ ] Y , int [ ] V ) { if ( N >= 3 ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { for ( int k = j + 1 ; k < N ; k ++ ) { if ( ! intersection ( X [ i ] , Y [ i ] , t * V [ i ] , X [ j ] , Y [ j ] , t * V [ j ] , X [ k ] , Y [ k ] , t * V [ k ] ) ) return false ; } } } return true ; } else { return Math . sqrt ( ( X [ 0 ] - X [ 1 ] ) * ( X [ 0 ] - X [ 1 ] ) + ( Y [ 0 ] - Y [ 1 ] ) * ( Y [ 0 ] - Y [ 1 ] ) ) <= t * ( V [ 0 ] + V [ 1 ] ) ; } } public static void binarySearch ( int N , int [ ] X , int [ ] Y , int [ ] V ) { double tl = 0.0 , tu = 100000.0 , t ; for ( int i = 0 ; i < 1000 ; i ++ ) { t = ( tl + tu ) / 2.0 ; if ( isGood ( t , N , X , Y , V ) ) { tu = t ; } else { tl = t ; } } System . out . println ( tu ) ; } public static void main ( String [ ] args ) { int N = 4 ; int [ ] X = { 1 , - 3 , - 1 , 2 } ; int [ ] Y = { 2 , 4 , - 2 , - 2 } ; int [ ] V = { 3 , 2 , 4 , 5 } ; binarySearch ( N , X , Y , V ) ; } }