Problem statement

https://leetcode.com/problems/circle-and-rectangle-overlapping/

Solution

Continue sides of rectangles: so we have 9 parts of plane: 1, 2, 3, 4, 5, 6, 7, 8, 9 (from left to right and from top to down). Then we can have several cases:

  1. Condition for parts 4, 5, 6
  2. Condition for parts 2, 5, 8
  3. Condition for and last one is 1, 3, 7, 9

Complexity

Time and space complexity is O(1).

Code

class Solution:
    def checkOverlap(self, R, x_c, y_c, x1, y1, x2, y2):
        h1 = abs(x1 - x2)/2
        h2 = abs(y1 - y2)/2
        q1 = abs(x_c - (x1 + x2)/2)
        q2 = abs(y_c - (y1 + y2)/2)
        
        if q1 <= h1 and q2 <= h2 + R: return True
        if q2 <= h2 and q1 <= h1 + R: return True
        if (q1 - h1)**2 + (q2 - h2)**2 <= R**2: return True
    
        return False