Check whether it is possible to join two points given on circle such that distance between them is k | C ++ 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
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; struct t { ll x , y , r ; } ; typedef struct t node ; long double dis ( ll x1 , ll y1 , ll x2 , ll y2 ) { return sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ; } bool check ( node c1 , node c2 , int k ) { long double min = 0 ; long double max = 0 ; long double 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 ; } ll temin = ( ll ) ( ceil ( min ) ) ; ll re = ( ll ) max ; if ( k >= temin && k <= re ) return true ; return false ; } int main ( ) { node circle1 , circle2 ; 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 ) ) cout << " YES " << endl ; else cout << " NO " << endl ; }