Given two opposite **diagonal points** of a rectangle **(X1, Y1), (X2, Y2)** and the centre, 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 centre of the circle can be found using**sqrt((Xc- Xn)**. If this distance ≤ the radius of the circle, the two shapes intersect.^{2}+ (Yc- Yn)^{2})**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++

`// C++ implementation to check if any ` `// point overlaps the given circle ` `// and rectangle ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if any point ` `// overlaps the given circle ` `// and rectangle ` `bool` `checkOverlap(` `int` `R, ` `int` `Xc, ` `int` `Yc, ` ` ` `int` `X1, ` `int` `Y1, ` ` ` `int` `X2, ` `int` `Y2) ` `{ ` ` ` ` ` `// Find the nearest point on the ` ` ` `// rectangle to the center of ` ` ` `// the circle ` ` ` `int` `Xn = max(X1, min(Xc, X2)); ` ` ` `int` `Yn = max(Y1, min(Yc, Y2)); ` ` ` ` ` `// Find the distance between the ` ` ` `// nearest point and the center ` ` ` `// of the circle ` ` ` `// Distance between 2 points, ` ` ` `// (x1, y1) & (x2, y2) in ` ` ` `// 2D Euclidean space is ` ` ` `// ((x1-x2)**2 + (y1-y2)**2)**0.5 ` ` ` `int` `Dx = Xn - Xc; ` ` ` `int` `Dy = Yn - Yc; ` ` ` `return` `(Dx * Dx + Dy * Dy) <= R * R; ` `} ` ` ` `// Driver code ` `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"` `; ` ` ` `} ` `} ` ` ` `// This code is contributed by BhupendraSingh ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to check if any ` `// point overlaps the given circle ` `// and rectangle ` `class` `GFG{ ` ` ` `// Function to check if any point ` `// overlaps the given circle ` `// and rectangle ` `static` `boolean` `checkOverlap(` `int` `R, ` `int` `Xc, ` `int` `Yc, ` ` ` `int` `X1, ` `int` `Y1, ` ` ` `int` `X2, ` `int` `Y2) ` `{ ` ` ` ` ` `// Find the nearest point on the ` ` ` `// rectangle to the center of ` ` ` `// the circle ` ` ` `int` `Xn = Math.max(X1, Math.min(Xc, X2)); ` ` ` `int` `Yn = Math.max(Y1, Math.min(Yc, Y2)); ` ` ` ` ` `// Find the distance between the ` ` ` `// nearest point and the center ` ` ` `// of the circle ` ` ` `// Distance between 2 points, ` ` ` `// (x1, y1) & (x2, y2) in ` ` ` `// 2D Euclidean space is ` ` ` `// ((x1-x2)**2 + (y1-y2)**2)**0.5 ` ` ` `int` `Dx = Xn - Xc; ` ` ` `int` `Dy = Yn - Yc; ` ` ` `return` `(Dx * Dx + Dy * Dy) <= R * R; ` `} ` ` ` `// Driver code ` `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"` `); ` ` ` `} ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to check if any ` `# point overlaps the given Circle ` `# and Rectangle ` ` ` `# Function to check if any point ` `# overlaps the given Circle ` `# and Rectangle ` `def` `checkOverlap(R, Xc, Yc, X1, Y1, X2, Y2): ` ` ` ` ` `# Find the nearest point on the ` ` ` `# rectangle to the center of ` ` ` `# the circle ` ` ` `Xn ` `=` `max` `(X1, ` `min` `(Xc, X2)) ` ` ` `Yn ` `=` `max` `(Y1, ` `min` `(Yc, Y2)) ` ` ` ` ` `# Find the distance between the ` ` ` `# nearest point and the center ` ` ` `# of the circle ` ` ` `# Distance between 2 points, ` ` ` `# (x1, y1) & (x2, y2) in ` ` ` `# 2D Euclidean space is ` ` ` `# ((x1-x2)**2 + (y1-y2)**2)**0.5 ` ` ` `Dx ` `=` `Xn ` `-` `Xc ` ` ` `Dy ` `=` `Yn ` `-` `Yc ` ` ` `return` `(Dx` `*` `*` `2` `+` `Dy` `*` `*` `2` `) <` `=` `R` `*` `*` `2` ` ` `# Driver code ` `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)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to check if any ` `// point overlaps the given circle ` `// and rectangle ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to check if any point ` `// overlaps the given circle ` `// and rectangle ` `static` `bool` `checkOverlap(` `int` `R, ` `int` `Xc, ` `int` `Yc, ` ` ` `int` `X1, ` `int` `Y1, ` ` ` `int` `X2, ` `int` `Y2) ` `{ ` ` ` ` ` `// Find the nearest point on the ` ` ` `// rectangle to the center of ` ` ` `// the circle ` ` ` `int` `Xn = Math.Max(X1, ` ` ` `Math.Min(Xc, X2)); ` ` ` `int` `Yn = Math.Max(Y1, ` ` ` `Math.Min(Yc, Y2)); ` ` ` ` ` `// Find the distance between the ` ` ` `// nearest point and the center ` ` ` `// of the circle ` ` ` `// Distance between 2 points, ` ` ` `// (x1, y1) & (x2, y2) in ` ` ` `// 2D Euclidean space is ` ` ` `// ((x1-x2)**2 + (y1-y2)**2)**0.5 ` ` ` `int` `Dx = Xn - Xc; ` ` ` `int` `Dy = Yn - Yc; ` ` ` `return` `(Dx * Dx + Dy * Dy) <= R * R; ` `} ` ` ` `// Driver code ` `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"` `); ` ` ` `} ` `} ` `} ` ` ` `// This code is contributed by Nidhi_biet ` |

*chevron_right*

*filter_none*

**Output:**

True

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Check whether a given point lies inside a rectangle or not
- Check whether a given point lies on or inside the rectangle | Set 3
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- Check if a point lies on or inside a rectangle | Set-2
- Ratio of area of a rectangle with the rectangle inscribed in it
- Check if given point lies in range of any of the given towers
- Check if a circle lies inside another circle or not
- Finding the best fit rectangle that covers a given point
- Area of the largest Rectangle without a given point
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Check whether a point exists in circle sector or not.
- The biggest possible circle that can be inscribed in a rectangle
- Area of a circle inscribed in a rectangle which is inscribed in a semicircle
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- Equation of circle when three points on the circle are given
- Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given
- Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
- Area of the circle that has a square and a circle inscribed in it
- Shortest distance between a point and a circle

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.