LeetCode solution summary 1401. Do circles and rectangles overlap?

 Directory link:

Lituo Programming Problems - Summary of Solutions_Share+Records-CSDN Blog

GitHub synchronous brushing project:

https://github.com/September26/java-algorithms

Link to the original title: Likou


describe:

You are given a  (radius, xCenter, yCenter) circle denoted by and a rectangle parallel to the coordinate axes  (x1, y1, x2, y2) , where  (x1, y1) is the coordinate of the bottom left corner of the rectangle (x2, y2) and is the coordinate of the top right corner.

If the circle and the rectangle overlap, please return  true , otherwise return  false .

In other words, you detect the  existence of  a point  (xi, yi) , which is both on the circle and on the rectangle (both including the case where the point falls on the boundary).

Example 1:

Input: radius = 1, xCenter = 0, yCenter = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1 Output
 : true
 Explanation: A circle and a rectangle have a common point (1,0).

Example 2:

Input: radius = 1, xCenter = 1, yCenter = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1
 Output: false

Example 3:

Input: radius = 1, xCenter = 0, yCenter = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1
 Output: true

hint:

  • 1 <= radius <= 2000
  • -104 <= xCenter, yCenter <= 104
  • -104 <= x1 < x2 <= 104
  • -104 <= y1 < y2 <= 104

Problem-solving ideas:

/**

* 1401. Whether the circle and rectangle overlap

* Problem-solving ideas:

* First, if the center of the circle is within the rectangle, just return true directly.

* In other cases, we need to find the shortest distance from the center of the circle to the rectangle.

* If the x-axis is in the range from x1 to x2, the closest distance is min(abs(yCenter - y1), abs(yCenter - y2)). Similarly, min(abs(xCenter - x1), abs(xCenter - x2)) is also consistent, and the smaller value of the two is taken.

* If none of them match, the minimum value is the nearest value from the 4 vertices.

* For the convenience of calculation, we calculate the value after square. distance <= pow(radius, 2) is within the range.

*/

code:

class Solution1401
{
public:
    int getDistance(int xCenter, int yCenter, int x, int y)
    {
        return pow(xCenter - x, 2) + pow(yCenter - y, 2);
    }

    bool checkOverlap(int radius, int xCenter, int yCenter, int x1, int y1, int x2, int y2)
    {
        if (((xCenter >= x1 && xCenter <= x2) || (xCenter >= x2 && xCenter <= x1)) && ((yCenter >= y1 && yCenter <= y2) || (yCenter >= y2 && yCenter <= y1)))
        {
            return true;
        }
        int distance = 20000;
        if ((xCenter >= x1 && xCenter <= x2) || (xCenter >= x2 && xCenter <= x1))
        {
            distance = min(abs(yCenter - y1), abs(yCenter - y2));
        }
        if ((yCenter >= y1 && yCenter <= y2) || (yCenter >= y2 && yCenter <= y1))
        {
            distance = min(distance, min(abs(xCenter - x1), abs(xCenter - x2)));
        }
        distance = pow(distance, 2);
        distance = min(distance, getDistance(xCenter, yCenter, x1, y1));
        distance = min(distance, getDistance(xCenter, yCenter, x1, y2));
        distance = min(distance, getDistance(xCenter, yCenter, x2, y1));
        distance = min(distance, getDistance(xCenter, yCenter, x2, y2));
        return distance <= pow(radius, 2);
    }
};

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/131375934