Given two opposite diagonal points of a rectangle (X1, Y1), (X2, Y2) and the center, radius of the circle R, (Xc, Yc), the task is to check if there exists any point P that belongs to both the circle as well as the rectangle.
Examples:
Input: R = 2, Xc = 0, Yc = 0, X1 = 1, Y1 = 0, X2 = 3, Y2 = 3
Output: true
Explanation:
Clearly, from the below illustration, the circle and the rectangle intersect.

Input: R = 1, Xc = 1, Yc = 1, X1 = 3, Y1 = 3, X2 = 5, Y2 = 6
Output: false
Approach: The idea is to simply check if the circle and the rectangle intersect or not. There are essentially 2 possible cases when the intersection occurs.
- Case 1: The side of the rectangle touches or intersects the circle. In order to check whether the shapes intersect, we need to find a point on or inside the rectangle that is closest to the center of the circle. If this point lies on or inside the circle, it is guaranteed that both the shapes intersect. Let the closest point be denoted by (Xn, Yn). Then the distance between the closest point and the center of the circle can be found using sqrt((Xc- Xn)2 + (Yc- Yn)2). If this distance ? the radius of the circle, the two shapes intersect.
- Case 2: The center of the circle lies inside the rectangle. Since the center of the circle lies inside the rectangle, the closest point will be (Xc, Yc).
On close observation, it can be observed that the point of interest only depends on the locations of (X1, Y1) and (X2, Y2) relative to (Xc, Yc). Therefore, the closest point in both the above cases can be calculated as:
- Xn= max(X1, min(Xc, X2))
- Yn= max(Y1, min(Yc, Y2))
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
bool checkOverlap( int R, int Xc, int Yc,
int X1, int Y1,
int X2, int Y2)
{
int Xn = max(X1, min(Xc, X2));
int Yn = max(Y1, min(Yc, Y2));
int Dx = Xn - Xc;
int Dy = Yn - Yc;
return (Dx * Dx + Dy * Dy) <= R * R;
}
int main()
{
int R = 1;
int Xc = 0, Yc = 0;
int X1 = 1, Y1 = -1;
int X2 = 3, Y2 = 1;
if (checkOverlap(R, Xc, Yc,
X1, Y1,
X2, Y2))
{
cout << "True" << endl;
}
else
{
cout << "False" ;
}
}
|
Java
class GFG{
static boolean checkOverlap( int R, int Xc, int Yc,
int X1, int Y1,
int X2, int Y2)
{
int Xn = Math.max(X1, Math.min(Xc, X2));
int Yn = Math.max(Y1, Math.min(Yc, Y2));
int Dx = Xn - Xc;
int Dy = Yn - Yc;
return (Dx * Dx + Dy * Dy) <= R * R;
}
public static void main(String[] args)
{
int R = 1 ;
int Xc = 0 , Yc = 0 ;
int X1 = 1 , Y1 = - 1 ;
int X2 = 3 , Y2 = 1 ;
if (checkOverlap(R, Xc, Yc,
X1, Y1,
X2, Y2))
{
System.out.print( "True" + "\n" );
}
else
{
System.out.print( "False" );
}
}
}
|
Python3
def checkOverlap(R, Xc, Yc, X1, Y1, X2, Y2):
Xn = max (X1, min (Xc, X2))
Yn = max (Y1, min (Yc, Y2))
Dx = Xn - Xc
Dy = Yn - Yc
return (Dx * * 2 + Dy * * 2 ) < = R * * 2
if (__name__ = = "__main__" ):
R = 1
Xc, Yc = 0 , 0
X1, Y1 = 1 , - 1
X2, Y2 = 3 , 1
print (checkOverlap(R, Xc, Yc, X1, Y1, X2, Y2))
|
C#
using System;
class GFG{
static bool checkOverlap( int R, int Xc, int Yc,
int X1, int Y1,
int X2, int Y2)
{
int Xn = Math.Max(X1,
Math.Min(Xc, X2));
int Yn = Math.Max(Y1,
Math.Min(Yc, Y2));
int Dx = Xn - Xc;
int Dy = Yn - Yc;
return (Dx * Dx + Dy * Dy) <= R * R;
}
public static void Main()
{
int R = 1;
int Xc = 0, Yc = 0;
int X1 = 1, Y1 = -1;
int X2 = 3, Y2 = 1;
if (checkOverlap(R, Xc, Yc,
X1, Y1,
X2, Y2))
{
Console.Write( "True" + "\n" );
}
else
{
Console.Write( "False" );
}
}
}
|
Javascript
<script>
function checkOverlap(R, Xc, Yc, X1, Y1, X2, Y2)
{
let Xn = Math.max(X1, Math.min(Xc, X2));
let Yn = Math.max(Y1, Math.min(Yc, Y2));
let Dx = Xn - Xc;
let Dy = Yn - Yc;
return (Dx * Dx + Dy * Dy) <= R * R;
}
let R = 1;
let Xc = 0, Yc = 0;
let X1 = 1, Y1 = -1;
let X2 = 3, Y2 = 1;
if (checkOverlap(R, Xc, Yc,
X1, Y1,
X2, Y2))
{
document.write( "True" + "\n" );
}
else
{
document.write( "False" );
}
</script>
|
Time Complexity: O(1)
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 geeks!
Last Updated :
08 Nov, 2021
Like Article
Save Article