Check whether it is possible to join two points given on circle such that distance between them is k

Given two circles and a length, K. Find whether we can join two points (one on perimeter of each circle), so that distance between the points is K. (Coordinates of both points need not be an integer value).
Examples:Â
Input: Circle-1 Center (0, 0) Radius = 5
Circle-2 Center (8, 3) Radius = 2
K = 3
Output: Yes
Maximum Distance: 15
Minimum Distance: 2
Approach:Â
- We have to find the maximum and minimum distance possible between any two points on these circles, if K lies in this range then the answer is Yes otherwise we cannot find such a Line segment.
- To find minimum and maximum distance
Â
- Case 1: When two circles do not intersect or just touch at one point.Â
In this scenario, the maximum distance would be distance between centers + Radius (circle 1) + Radius (circle 2). The minimum distance would be distance between centers – Radius(circle 1) – Radius (circle 2).Â
Â
Â
- Case 2: When the two circles intersect at exactly two points.Â
In this scenario, the maximum distance would be distance between centers + Radius (circle 1) + Radius (circle 2). The minimum distance would be 0. (We have two points common in both the circles).Â
Â
Â
- Case 3: When Circle 1 is completely inside Circle 2.Â
In this scenario, the maximum distance would be distance between centers + Radius (circle 1) + Radius (circle 2). The minimum distance would be Radius (Circle 2) – distance between centers – Radius (Circle 1)Â
Â
Â
- Case 4: When Circle 2 is completely inside Circle 1.Â
In this scenario, the maximum distance would be distance between centers + Radius (circle 1) + Radius (circle 2). The minimum distance would be Radius (Circle 1) – distance between centers – Radius (Circle 2)Â
Â
Â
- Case 5: When both Circles have same centerÂ
- Sub Case 1: Radius is also same. Both minimum distance and maximum distance are 0.
- Sub Case 2: Radius is different(R1<R2)Â
Maximum distance is R1+R2Â
Minimum distance is R2-R1Â
Â
Below is the implementation of above approach:Â
C++
// C++ program to implement above approach#include <bits/stdc++.h>#define ll long long intusing namespace std;Â
struct t {Â Â Â Â ll x, y, r;};typedef struct t node;Â
// Return distance between the centerslong 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;    // Distance between centers    long double de = dis(c1.x, c1.y, c2.x, c2.y);    // Case 5    if (de == 0) {        // SubCase 1        if (c1.r == c2.r) {            min = 0;            max = 0;        }        // Subcase 2        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;            }        }    }    // Case 1    else if (de >= c1.r + c2.r) {        min = de - c1.r - c2.r;        max = de + c1.r + c2.r;    }    // Case 3    else if (de + c2.r < c1.r) {        max = c2.r + c1.r + de;        min = c1.r - de - c2.r;    }    // Case 4    else if (de + c1.r < c2.r) {Â
        max = c2.r + c1.r + de;        min = c2.r - de - c1.r;    }    // Case 2    else if ((de + c2.r >= c1.r) || (de + c1.r >= c2.r)) {        max = c2.r + c1.r + de;        min = 0;    }    // Since value of k will always be an integer    ll temin = (ll)(ceil(min));    ll re = (ll)max;    if (k >= temin && k <= re)        return true;    return false;}Â
// Driver Codeint 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;} |
Java
// Java program to implement above approachclass GFG {Â
    static class node     {        long x, y, r;    };Â
    // Return distance between the centers    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;                 // Distance between centers        long de = dis(c1.x, c1.y, c2.x, c2.y);                 // Case 5        if (de == 0)         {            // SubCase 1            if (c1.r == c2.r)            {                min = 0;                max = 0;            }             // Subcase 2            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;            }        }                  // Case 1        else if (de >= c1.r + c2.r)         {            min = de - c1.r - c2.r;            max = de + c1.r + c2.r;        }         // Case 3        else if (de + c2.r < c1.r)        {            max = c2.r + c1.r + de;            min = c1.r - de - c2.r;        }         // Case 4        else if (de + c1.r < c2.r)         {Â
            max = c2.r + c1.r + de;            min = c2.r - de - c1.r;        }        // Case 2        else if ((de + c2.r >= c1.r) || (de + c1.r >= c2.r))        {            max = c2.r + c1.r + de;            min = 0;        }                 // Since value of k will always be an integer        long temin = (long) (Math.ceil(min));        long re = (long) max;        if (k >= temin && k <= re)        {            return true;        }        return false;    }Â
    // Driver Code    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");        }    }}Â
// This code is contributed by Princi Singh |
Python
# Python3 program to implement above approachfrom math import sqrt,ceil,floorÂ
# Return distance between the centersdef dis(x1, y1, x2, y2):Â Â Â Â return sqrt((x1 - x2) * (x1 - x2) +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (y1 - y2) * (y1 - y2))Â
def check(c1, c2, k):    min = 0    max = 0         # Distance between centers    de = dis(c1[0], c1[1], c2[0], c2[1])         # Case 5    if (de == 0):                 # SubCase 1        if (c1[2] == c2[2]):            min = 0            max = 0             # Subcase 2        else:            if (c1[2] - c2[2] > 0):                min = c1[2] - c2[2]                max = min + 2 * c2[2]Â
            else:                min = c2[2] - c1[2]                max = min + 2 * c1[2]Â
    # Case 1    elif (de >= c1[2] + c2[2]):        min = de - c1[2] - c2[2]        max = de + c1[2] + c2[2]         # Case 3    elif (de + c2[2] < c1[2]):        max = c2[2] + c1[2] + de        min = c1[2] - de - c2[2]         # Case 4    elif (de + c1[2] < c2[2]):Â
        max = c2[2] + c1[2] + de        min = c2[2] - de - c1[2]         # Case 2    elif ((de + c2[2] >= c1[2]) or (de + c1[2] >= c2[2])):        max = c2[2] + c1[2] + de        min = 0Â
    # Since value of k wialways be an integer    temin = ceil(min)    re = max    if (k >= temin and k <= re):        return True    return FalseÂ
# Driver Codecircle1 = [0, 0, 5]circle2 = [8, 3, 2]k = 3Â
if (check(circle1, circle2, k)):Â Â Â Â print("YES")else:Â Â Â Â print("NO" )Â Â Â Â Â # This code is contributed by mohit kumar 29 |
C#
// C# program to implement above approach using System;Â Â Â Â Â class GFG {Â
    public class node     {        public long x, y, r;    };Â
    // Return distance between the centers    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;                 // Distance between centers        long de = dis(c1.x, c1.y, c2.x, c2.y);                 // Case 5        if (de == 0)         {            // SubCase 1            if (c1.r == c2.r)            {                min = 0;                max = 0;            }             // Subcase 2            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;            }        }                  // Case 1        else if (de >= c1.r + c2.r)         {            min = de - c1.r - c2.r;            max = de + c1.r + c2.r;        }         // Case 3        else if (de + c2.r < c1.r)        {            max = c2.r + c1.r + de;            min = c1.r - de - c2.r;        }         // Case 4        else if (de + c1.r < c2.r)         {Â
            max = c2.r + c1.r + de;            min = c2.r - de - c1.r;        }        // Case 2        else if ((de + c2.r >= c1.r) || (de + c1.r >= c2.r))        {            max = c2.r + c1.r + de;            min = 0;        }                 // Since value of k will always be an integer        long temin = (long) (Math.Ceiling((double)min));        long re = (long) max;        if (k >= temin && k <= re)        {            return true;        }        return false;    }Â
    // Driver Code    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))        {            Console.WriteLine("Yes");        }         else        {            Console.WriteLine("No");        }    }}Â
// This code contributed by Rajput-Ji |
Javascript
<script>Â
// JavaScript program to implement above approachÂ
// Return distance between the centersfunction dis(x1,y1,x2,y2){    return Math.sqrt((x1 - x2) * (x1 - x2)                + (y1 - y2) * (y1 - y2));}Â
function check(c1,c2,k){    let min = 0;        let max = 0;                   // Distance between centers        let de = dis(c1[0], c1[1], c2[0], c2[1]);                   // Case 5        if (de == 0)         {            // SubCase 1            if (c1[2] == c2[2])            {                min = 0;                max = 0;            }             // Subcase 2            else if (c1[2] - c2[2] > 0)             {                min = c1[2] - c2[2];                max = min + 2 * c2[2];            }             else            {                min = c2[2] - c1[2];                max = min + 2 * c1[2];            }        }                    // Case 1        else if (de >= c1[2] + c2[2])         {            min = de - c1[2] - c2[2];            max = de + c1[2] + c2[2];        }         // Case 3        else if (de + c2[2] < c1[2])        {            max = c2[2] + c1[2] + de;            min = c1[2] - de - c2[2];        }         // Case 4        else if (de + c1[2] < c2[2])         {               max = c2[2] + c1[2] + de;            min = c2[2]- de - c1[2];        }        // Case 2        else if ((de + c2[2] >= c1[2]) ||         (de + c1[2] >= c2[2]))        {            max = c2[2] + c1[2] + de;            min = 0;        }                   // Since value of k will always be an integer        let temin = (Math.ceil(min));        let re = max;        if (k >= temin && k <= re)        {            return true;        }        return false;}Â
// Driver Codelet circle1 = [0, 0, 5];let circle2 = [8, 3, 2];Â
let k = 3;Â
if (check(circle1, circle2, k)){Â Â Â Â document.write("YES");} else{Â Â Â Â document.write("NO");}Â
Â
Â
// This code is contributed by unknown2108Â
</script> |
Output:Â
YES
Â
Time Complexity: O(log((x1-x2)2+(y1-y2)2)) because it is using sqrt function
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




