METEOR GARDEN

Easy
0/40
Average time to solve is 15m
profile
Contributed by
7 upvotes
Asked in company
Flipkart limited

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.
Detailed explanation ( Input/output format, Notes, Images )

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

Sample Input 1 :

2
0 0 2 1 0 3 2
0 0 1 1 -1 3 1

Sample Output 1 :

True
True

Explanation for sample input 1 :

Test Case 1 :

Example

As you can observe the radius of the circle is β€˜2’ and according to their coordinates, we can say they will collapse so we return β€˜True’ as the answer.

Test Case 2 :

Example

As you can see in the picture there is a common point (1, 0) that lies both on the circle and rectangle so we return β€˜True’ for this case.

Sample Input 2 :

1
0 0 1 3 2 6 4

Sample Output 2 :

False

Explanation for sample input 2 :

Test Case 1:

Example

As you can observe, rectangles and circles have no common points so we can say they will not collapse so we return β€˜False’ as the answer.
Hint

Can you find the nearest point on the rectangle from the circle?

Approaches (1)
Iterative 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’.
Time Complexity

O(1).

 

As we are using a formula that takes constant time.

Space Complexity

O(1).

 

As we are using constant space.

Code Solution
(100% EXP penalty)
METEOR GARDEN
Full screen
Console