Check whether it is possible to join two points given on circle such that distance between them is k | Java program to implement above approach ; Return distance between the centers ; Distance between centers ; Case 5 ; SubCase 1 ; Subcase 2 ; Case 1 ; Case 3 ; Case 4 ; Case 2 ; Since value of k will always be an integer ; Driver Code
class GFG { static class node { long x , y , r ; } ; static long dis ( long x1 , long y1 , long x2 , long y2 ) { return ( long ) Math . sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ; } static boolean check ( node c1 , node c2 , int k ) { long min = 0 ; long max = 0 ; long de = dis ( c1 . x , c1 . y , c2 . x , c2 . y ) ; if ( de == 0 ) { if ( c1 . r == c2 . r ) { min = 0 ; max = 0 ; } else if ( c1 . r - c2 . r > 0 ) { min = c1 . r - c2 . r ; max = min + 2 * c2 . r ; } else { min = c2 . r - c1 . r ; max = min + 2 * c1 . r ; } } else if ( de >= c1 . r + c2 . r ) { min = de - c1 . r - c2 . r ; max = de + c1 . r + c2 . r ; } else if ( de + c2 . r < c1 . r ) { max = c2 . r + c1 . r + de ; min = c1 . r - de - c2 . r ; } else if ( de + c1 . r < c2 . r ) { max = c2 . r + c1 . r + de ; min = c2 . r - de - c1 . r ; } else if ( ( de + c2 . r >= c1 . r ) || ( de + c1 . r >= c2 . r ) ) { max = c2 . r + c1 . r + de ; min = 0 ; } long temin = ( long ) ( Math . ceil ( min ) ) ; long re = ( long ) max ; if ( k >= temin && k <= re ) { return true ; } return false ; } public static void main ( String [ ] args ) { node circle1 = new node ( ) ; node circle2 = new node ( ) ; int k = 3 ; circle1 . x = 0 ; circle1 . y = 0 ; circle1 . r = 5 ; circle2 . x = 8 ; circle2 . y = 3 ; circle2 . r = 2 ; if ( check ( circle1 , circle2 , k ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }