Similar Problems

Similar Problems not available

Circle And Rectangle Overlapping - Leetcode Solution

Companies:

LeetCode:  Circle And Rectangle Overlapping Leetcode Solution

Difficulty: Medium

Topics: math  

The Circle And Rectangle Overlapping problem on LeetCode is a problem that requires you to determine whether a given circle and rectangle overlap.

The problem statement specifies that we are given the coordinates of the center of the circle, the radius of the circle, and the coordinates of the bottom-left and top-right corners of the rectangle. We are required to determine whether the circle and rectangle overlap.

To solve this problem, we will need to follow a few steps:

Step 1: Calculate the distance between the circle center and the closest point on the rectangle. To do this, we will first determine the x-coordinate and y-coordinate of the closest point on the rectangle to the center of the circle. We can calculate the x-coordinate of the closest point by comparing the x-coordinate of the circle center to the x-coordinates of the left and right sides of the rectangle. If the x-coordinate of the circle center is less than the x-coordinate of the left side of the rectangle, then the x-coordinate of the closest point is the x-coordinate of the left side of the rectangle. Otherwise, if the x-coordinate of the circle center is greater than the x-coordinate of the right side of the rectangle, then the x-coordinate of the closest point is the x-coordinate of the right side of the rectangle. If the x-coordinate of the circle center is between the x-coordinates of the left and right sides of the rectangle, then the x-coordinate of the closest point is the x-coordinate of the circle center.

We can calculate the y-coordinate of the closest point in a similar manner by comparing the y-coordinate of the circle center to the y-coordinates of the top and bottom sides of the rectangle. If the y coordinate of the circle center is less than the y-coordinate of the bottom side of the rectangle, then the y-coordinate of the closest point is the y-coordinate of the bottom side of the rectangle. Otherwise, if the y-coordinate of the circle center is greater than the y-coordinate of the top side of the rectangle, then the y-coordinate of the closest point is the y-coordinate of the top side of the rectangle. If the y-coordinate of the circle center is between the y-coordinates of the bottom and top sides of the rectangle, then the y-coordinate of the closest point is the y-coordinate of the circle center.

Once we have determined the x-coordinate and y-coordinate of the closest point, we can calculate the distance between the circle center and the closest point using the distance formula:

distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)

where x1 and y1 are the coordinates of the circle center and x2 and y2 are the coordinates of the closest point on the rectangle.

Step 2: Check if the distance between the circle center and the closest point on the rectangle is less than or equal to the radius of the circle. If it is, then the circle and rectangle overlap. Otherwise, they do not overlap.

Here is the Python code that implements the above algorithm:

def checkOverlap(radius: int, x_center: int, y_center: int, x1: int, y1: int, x2: int, y2: int) -> bool:
    # Determine the x-coordinate of the closest point on the rectangle
    if x_center < x1:
        closest_x = x1
    elif x_center > x2:
        closest_x = x2
    else:
        closest_x = x_center

    # Determine the y-coordinate of the closest point on the rectangle
    if y_center < y1:
        closest_y = y1
    elif y_center > y2:
        closest_y = y2
    else:
        closest_y = y_center

    # Calculate the distance between the circle center and the closest point
    distance = ((closest_x - x_center) ** 2 + (closest_y - y_center) ** 2) ** 0.5

    # Check if the distance is less than or equal to the radius of the circle
    if distance <= radius:
        return True
    else:
        return False

This code takes in the required parameters and returns a boolean value indicating whether the circle and rectangle overlap or not.

Circle And Rectangle Overlapping Solution Code

1