We are provided with a 2D plane and a point (, ). We have to find a rectangle (, , , ) such that itencompasses the given point (, ). The rectangle chosen must satisfy the given condition . Ifmultiple rectangles are possible the we must choose the one with the least Euclid distance between the center of the rectangle and the point (, ).
Image Source – codeforces
Input : 70 10 20 5 5 3 Output :12 0 27 9 Input :100 100 32 63 2 41 Output :30 18 34 100
The logic behind the problem is as follows. First of all we reduce to lowest irreducible form by dividing a and b by . We think of the problem in two separate dimensions and independently. We find out the after dividing a and b by their gcd to find the maximum distance that we can cover safely by staying in the range of . Since we have to find rectangle with minimum distance of its center from the point (, ), therefore we start with the assumption that point (, ) is our center. Then we find and by subtracting and adding half of length to . If either or goes out of range then we shift the coordinates accordingly to bring it inside the range . Similarly we proceed to calculate and .
For the first example according to above logic the answer comes out to be .
12 0 27 9
- Check whether a given point lies inside a rectangle or not
- Check if a point lies on or inside a rectangle | Set-2
- Check whether a given point lies on or inside the rectangle | Set 3
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Ratio of area of a rectangle with the rectangle inscribed in it
- Reflection of a point at 180 degree rotation of another point
- Rotation of a point about another point in C++
- Circumradius of the rectangle
- Path in a Rectangle with Circles
- Draw Rectangle in C graphics
- Area and Perimeter of Rectangle in PL/SQL
- Finding LCM of more than two (or array) numbers without using GCD
- Stein's Algorithm for finding GCD
- Finding n-th term of series 3, 13, 42, 108, 235…
- Finding all subsets of a given set in Java
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.