GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python-codings/Math/check_if_two_rectangles_overlap.py at master · wpewpelepew/python-codings Expand file tree/ Copy pathcheck_if_two_rectangles_overlap.pyMore file actions 62 lines (48 loc) · 1.88 KB / Copy pathcheck_if_two_rectangles_overlap.pyFile metadata and controls62 lines (48 loc) · 1.88 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 ''' Check if Two Rectangles Overlap Given two rectangles, find if the given two rectangles overlap or not. Note that a rectangle can be represented by two coordinates, top left and bottom right. So mainly we are given following four coordinates (min X and Y and max X and Y). - l1: Bottom Left coordinate of first rectangle. (mins) - r1: Top Right coordinate of first rectangle. (maxs) - l2: Bottom Left coordinate of second rectangle. (mins) - r2: Top Right coordinate of second rectangle. (maxs) It may be assumed that the rectangles are PARALLEL to the coordinate axis. Input: (0, 0), (3, 2), (1, 1), (5, 4) Output: True ========================================= First check if rectangles are overlapping on X axis and after that if they are overlapping on Y axis. Time Complexity: O(1) Space Complexity: O(1) ''' ############ # Solution # ############ def check_if_two_rectangles_overlap(l1, r1, l2, r2): # first check by X coordinates, if rectangles can overlap on X axis # longer form (l1[0] < l2[0] and r1[0] < l2[0]) or (l1[0] > r2[0] and r1[0] > r2[0]) # but we know that l1[0] is always smaller than r1[0] if (r1[0] < l2[0]) or (l1[0] > r2[0]): return False # now we know that the rectangles are overlapping on X axis # check if they are overlapping on Y axis # (use the same logic from previous) if (r1[1] < l2[1]) or (l1[1] > r2[1]): return False return True ########### # Testing # ########### # Test 1 # Correct result => True print(check_if_two_rectangles_overlap((0, 0), (3, 2), (1, 1), (5, 4))) # Test 2 # Correct result => True print(check_if_two_rectangles_overlap((0, 0), (3, 2), (3, 2), (5, 4))) # Test 3 # Correct result => True print(check_if_two_rectangles_overlap((0, 0), (3, 2), (1, -1), (5, 4))) # Test 4 # Correct result => False print(check_if_two_rectangles_overlap((0, 0), (3, 2), (2, 3), (5, 4))) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |