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.
Input: R = 1, Xc = 1, Yc = 1, X1 = 3, Y1 = 3, X2 = 5, Y2 = 6
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)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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check whether a given point lies on or inside the rectangle | Set 3
- Check if a point lies on or inside a rectangle | Set-2
- Check whether a given point lies inside a rectangle or not
- Check whether a point exists in circle sector or not.
- The biggest possible circle that can be inscribed in a rectangle
- Finding the best fit rectangle that covers a given point
- Area of the largest Rectangle without a given point
- Shortest distance between a point and a circle
- Check if a circle lies inside another circle or not
- Find if a point lies inside a Circle
- Neighbors of a point on a circle using Bresenham's algorithm
- Find minimum radius such that atleast k point lie inside the circle
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- Area of a circle inscribed in a rectangle which is inscribed in a semicircle
- Check if four segments form a rectangle
- Check if given four integers (or sides) make rectangle
- Check if a point is inside, outside or on the parabola
- Check whether given floating point number is even or odd
- Check if a point is inside, outside or on the ellipse
- Check whether the point (x, y) lies on a given line
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.