Update appNew update is available. Click here to update.
Last Updated: 29 Apr, 2021

METEOR GARDEN

Easy
Flipkart

Problem statement

Ninja owns a garden which is in a rectangular shape on a planet named ‘Asgard’. He somehow manages to get information that his garden is the target of the coming meteor which is in a circular shape and ninja knows the radius and the coordinates of the center of the circle so he wants to know whether the meteor will overlap with his garden or not.

So your task is to check whether the meteor will overlap with the garden or not. You were provided with the coordinates of the bottom left corner and top right corner of the garden which is rectangular in shape and the center coordinates and radius of a meteor which is circular in shape.

Note :

If any point of the circle lies on the rectangle or inside the rectangle we say the circle is overlapping the rectangle.

Input Format :

The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains seven space-separated integers ‘xC’, ‘yC’, ‘RAD’, ‘X1’, ‘Y1’, ‘X2’, ‘Y2’ where ‘xC’ and ‘yC’ represents the center coordinates of a circle and ‘RAD’ represents the radius of the circle and ‘X1’, ‘Y1’ represents the coordinates of the bottom left corner and ‘X2’, ‘Y2’ represents the coordinates of the top right corner of the rectangle respectively.

Output Format :

For each test case, print a single line containing ‘True’ if the meteor overlaps with the garden else, print ‘False’.
The output of each test case will be printed in a separate line.

Note :

You do not need to print anything, it has already been taken care of. Just implement the given function.

Constraints :

1 <= T <= 5
1 <= RAD <= 2000
-10^4 <= xC, yC, X1, Y1, X2, Y2 <= 10^4
X1 < X2, Y1 < Y2

Where ‘T’ is the number of test cases, ‘xC’ and ‘yC’ represents the center coordinates of a circle and ‘RAD’ represents the radius of the circle and ‘X1’, ‘Y1’ represents the coordinates of the bottom left corner and ‘X2’, ‘Y2’ represents the coordinates of the top right corner of the rectangle.

Time Limit: 1 sec

Approaches

01 Approach

The idea here is to find the nearest point on a rectangle from the circle for this we can use suppose circle lies to the left side of the rectangle so in this case ‘xC < X1’ and if suppose circle lies on the right side ‘xC < X2’ and if it lies inside the rectangle ‘xC > X1’ and ‘xC < x2’ and same goes for ‘Y’ coordinate so we can say the point of intersecting only depends on (X1, Y1) and (X2, Y2) then after finding the nearest point we can simply compare the distance between nearest point and center coordinate and then we can compare it with radius. If radius distance is greater or equal to the distance we can say it is overlapping else we can say they are not.

 

Algorithm:

 

  • Firstly, we find the nearest point on the rectangle from the center of the circle by using the formula of math.
    • int xN = max(X1, min(xC, X2)).
    • int yN = max(Y1, min(yC, Y2)).
  • Now use distance formula and check
    • If (xC - xN) ^ 2 + (yC - yN) ^ 2 <= ‘RAD ^ 2
      • Return ‘True’.
    • Else
      • Return ‘False’.