Use std::clamp function to determine whether circle and rectangle overlap

1 Introduction

In computer programming, we often need to solve calculation problems with various geometric shapes. In LeetCode's question, there is a question asking us to determine whether the given circle and rectangle overlap. In order to solve this problem, we can use the std::clamp function in the C++ standard library to calculate the minimum distance from the center of the circle to the rectangle and compare it with the radius of the circle. This article will introduce the usage of std::clamp function in detail and analyze it based on the given code.

2. Topic description

Question link:Circle and Rectangle Overlapping

Given the radius of a circle, the coordinates of the center of the circle, and the coordinates of the lower left corner and upper right corner of a rectangle, determine whether the circle and the rectangle overlap. If they overlap, return true; otherwise, return false.

The given function prototype is as follows:

class Solution {
public:
    bool checkOverlap(int radius, int xCenter, int yCenter, int x1, int y1, int x2, int y2) {
        //圆心到区域【x1,x2】的最小值
        int x = std::clamp(xCenter,x1,x2)-xCenter;
        //圆心到区域【y1,y2】的最小值
        int y = std::clamp(yCenter,y1,y2)-yCenter;
        //圆心到矩形的最短距离与半径比较
        return x*x+y*y<=radius*radius;
    }
};

3. Introduction to std::clamp function

The std::clamp function is a function template in the C++ standard library, which is used to limit the range of a given value to a specified upper and lower limit. It is defined as follows:

template< class T >
constexpr const T& clamp( const T& value, const T& lo, const T& hi );

std::clamp accepts three parameters: value, lo and hi. It returns a value limited to the range [lo, hi]. If value is less than lo, return lo; if value is greater than hi, return hi; otherwise return value.

4. Use the std::clamp function to determine whether the circle and rectangle overlap

In the given code, we are using std::clamp function to calculate the minimum value from the center of the circle to the given area. Specifically, we calculate the minimum value from the center of the circle to the x-axis and y-axis of the area respectively.

First, we calculate the minimum value from the center of the circle to the area [x1, x2]:

int x = std::clamp(xCenter, x1, x2) - xCenter;

Here, std::clamp(xCenter, x1, x2) will return the value of xCenter in the range [x1, x2], and then we subtract xCenter from this value to get the minimum value x from the center of the circle to the x-axis boundary.

Next, we calculate the minimum value from the center of the circle to the area [y1, y2]:

int y = std::clamp(yCenter, y1, y2) - yCenter;

Similarly, std::clamp(yCenter, y1, y2) will return the value of yCenter in the range [y1, y2], and then we subtract yCenter from this value to get the minimum value y from the center of the circle to the y-axis boundary.

Finally, we use the Pythagorean theorem to calculate the shortest distance from the center of the circle to the rectangle, which is x * x + y * y, and then compare it to the square of the radius. If the shortest distance from the center of the circle to the rectangle is less than or equal to the square of the radius, the circle and the rectangle are considered to overlap and true is returned; otherwise, false is returned.

5. Summary

This article describes how to use the std::clamp function in the C++ standard library to determine whether a given circle and rectangle overlap. We first understood the description of the topic, and then introduced the usage of the std::clamp function in detail. Finally, combined with the given code, we show how to use the std::clamp function to calculate the minimum distance from the center of the circle to the rectangle and compare it with the radius to determine whether it overlaps or not.

The std::clamp function is a very convenient tool that can help us limit the range of values ​​and avoid manually writing cumbersome logical judgment statements. When dealing with various computing problems, we can make full use of the function templates provided in the C++ standard library to improve the readability and maintainability of the code.

Guess you like

Origin blog.csdn.net/bigBbug/article/details/131374277