Given two circles centered at (x1, y1) and (x2, y2) with radii r1 and r2 respectively, find whether the circles have at least one common point.
Examples:
Input: x1=3, y1=4, r1=5, x2=14, y2=18, r2=8
Output: false
Explanation: The distance between the centers is greater than the sum of the radii, so the circles do not intersect.Input: x1=3, y1=4, r1=5, x2=14, y2=18, r2=18
Output: true
Explanation: The circles intersect at two points, so they have common points.
Table of Content
[Naive Approach] Using Distance Formula - O(1) Time and O(1) Space
The idea is to compute the distance between the centers of the two circles using the Euclidean distance formula. If this distance is at least the difference of the radii and at most their sum, then the circles touch or intersect; otherwise, they do not have any common point.
Working of Approach:
- Compute the Euclidean distance between the centers of the two circles.
- Find the minimum possible distance (|r1 - r2|) and the maximum possible distance (r1 + r2) for the circles to intersect.
- Compare the center distance with these limits.
- Return true if it lies within the range (inclusive); otherwise, return false.
#include <bits/stdc++.h>
using namespace std;
bool circleTouch(int x1, int y1, int r1, int x2, int y2, int r2)
{
// Find the distance between the centers.
double dist = sqrt(1.0 * (x2 - x1) * (x2 - x1) + 1.0 * (y2 - y1) * (y2 - y1));
// Check whether the circles have at least one common point.
return (dist >= abs(r1 - r2) && dist <= r1 + r2);
}
int main()
{
int x1 = 3, y1 = 4, r1 = 5;
int x2 = 14, y2 = 18, r2 = 18;
if (circleTouch(x1, y1, r1, x2, y2, r2))
cout << "true";
else
cout << "false";
return 0;
}
import java.lang.Math;
public class GFG {
public static boolean circleTouch(int x1, int y1,
int r1, int x2,
int y2, int r2)
{
// Find the distance between the centers.
double dist
= Math.sqrt(1.0 * (x2 - x1) * (x2 - x1)
+ 1.0 * (y2 - y1) * (y2 - y1));
// Check whether the circles have at least one
// common point.
return (dist >= Math.abs(r1 - r2)
&& dist <= r1 + r2);
}
public static void main(String[] args)
{
int x1 = 3, y1 = 4, r1 = 5;
int x2 = 14, y2 = 18, r2 = 18;
if (circleTouch(x1, y1, r1, x2, y2, r2))
System.out.println("true");
else
System.out.println("false");
}
}
import math
def circleTouch(x1, y1, r1, x2, y2, r2):
# Find the distance between the centers.
dist = math.sqrt(1.0 * (x2 - x1) * (x2 - x1) + 1.0 * (y2 - y1) * (y2 - y1))
# Check whether the circles have at least one common point.
return (dist >= abs(r1 - r2) and dist <= r1 + r2)
if __name__ == "__main__":
x1 = 3
y1 = 4
r1 = 5
x2 = 14
y2 = 18
r2 = 18
if circleTouch(x1, y1, r1, x2, y2, r2):
print("true")
else:
print("false")
using System;
public class GFG {
public static bool circleTouch(int x1, int y1, int r1,
int x2, int y2, int r2)
{
// Find the distance between the centers.
double dist
= Math.Sqrt(1.0 * (x2 - x1) * (x2 - x1)
+ 1.0 * (y2 - y1) * (y2 - y1));
// Check whether the circles have at least one
// common point.
return (dist >= Math.Abs(r1 - r2)
&& dist <= r1 + r2);
}
public static void Main()
{
int x1 = 3, y1 = 4, r1 = 5;
int x2 = 14, y2 = 18, r2 = 18;
if (circleTouch(x1, y1, r1, x2, y2, r2))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
function circleTouch(x1, y1, r1, x2, y2, r2)
{
// Find the distance between the centers.
var dist = Math.sqrt(1.0 * (x2 - x1) * (x2 - x1)
+ 1.0 * (y2 - y1) * (y2 - y1));
// Check whether the circles have at least one common
// point.
return (dist >= Math.abs(r1 - r2) && dist <= r1 + r2);
}
// Driver Code
var x1 = 3, y1 = 4, r1 = 5;
var x2 = 14, y2 = 18, r2 = 18;
if (circleTouch(x1, y1, r1, x2, y2, r2)) {
console.log("true");
}
else {
console.log("false");
}
Output
true
[Expected Approach] Comparing Squared Distances - O(1) Time and O(1) Space
The idea is to eliminate the square root calculation by comparing squared values. Instead of finding the actual distance between the centers, compare its square with the squared minimum and maximum allowable distances to determine whether the circles intersect.
Working of Approach:
- Compute the squared distance between the centers of the two circles.
- Compute the squared values of the minimum ((r1 - r2)²) and maximum ((r1 + r2)²) valid distances.
- Compare the squared distance with these two limits.
- Return true if it lies within the valid range; otherwise, return false.
#include <bits/stdc++.h>
using namespace std;
bool circleTouch(int x1, int y1, int r1, int x2, int y2, int r2)
{
int dx = x2 - x1;
int dy = y2 - y1;
// Squared distance between the centres.
int d = dx * dx + dy * dy;
// Squared minimum and maximum valid distances.
int mn = (r1 - r2) * (r1 - r2);
int mx = (r1 + r2) * (r1 + r2);
if (d < mn)
return false;
if (d > mx)
return false;
return true;
}
int main()
{
int x1 = 3, y1 = 4, r1 = 5;
int x2 = 14, y2 = 18, r2 = 18;
if (circleTouch(x1, y1, r1, x2, y2, r2))
cout << "true";
else
cout << "false";
return 0;
}
public class GFG {
public static boolean circleTouch(int x1, int y1,
int r1, int x2,
int y2, int r2)
{
int dx = x2 - x1;
int dy = y2 - y1;
// Squared distance between the centres.
int d = dx * dx + dy * dy;
// Squared minimum and maximum valid distances.
int mn = (r1 - r2) * (r1 - r2);
int mx = (r1 + r2) * (r1 + r2);
if (d < mn)
return false;
if (d > mx)
return false;
return true;
}
public static void main(String[] args)
{
int x1 = 3, y1 = 4, r1 = 5;
int x2 = 14, y2 = 18, r2 = 18;
if (circleTouch(x1, y1, r1, x2, y2, r2))
System.out.println("true");
else
System.out.println("false");
}
}
def circleTouch(x1, y1, r1, x2, y2, r2):
dx = x2 - x1
dy = y2 - y1
# Squared distance between the centres.
d = dx * dx + dy * dy
# Squared minimum and maximum valid distances.
mn = (r1 - r2) * (r1 - r2)
mx = (r1 + r2) * (r1 + r2)
if d < mn:
return False
if d > mx:
return False
return True
if __name__ == "__main__":
x1 = 3
y1 = 4
r1 = 5
x2 = 14
y2 = 18
r2 = 18
if circleTouch(x1, y1, r1, x2, y2, r2):
print("true")
else:
print("false")
using System;
public class GFG {
public static bool circleTouch(int x1, int y1, int r1,
int x2, int y2, int r2)
{
int dx = x2 - x1;
int dy = y2 - y1;
// Squared distance between the centres.
int d = dx * dx + dy * dy;
// Squared minimum and maximum valid distances.
int mn = (r1 - r2) * (r1 - r2);
int mx = (r1 + r2) * (r1 + r2);
if (d < mn)
return false;
if (d > mx)
return false;
return true;
}
public static void Main()
{
int x1 = 3, y1 = 4, r1 = 5;
int x2 = 14, y2 = 18, r2 = 18;
if (circleTouch(x1, y1, r1, x2, y2, r2))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
function circleTouch(x1, y1, r1, x2, y2, r2)
{
let dx = x2 - x1;
let dy = y2 - y1;
// Squared distance between the centres.
let d = dx * dx + dy * dy;
// Squared minimum and maximum valid distances.
let mn = (r1 - r2) * (r1 - r2);
let mx = (r1 + r2) * (r1 + r2);
if (d < mn)
return false;
if (d > mx)
return false;
return true;
}
// Driver Code
let x1 = 3, y1 = 4, r1 = 5;
let x2 = 14, y2 = 18, r2 = 18;
if (circleTouch(x1, y1, r1, x2, y2, r2))
console.log("true");
else
console.log("false");
Output
true