total area of intersecting rectangles

前端 未结 6 1220
余生分开走
余生分开走 2020-12-23 09:43

I need an algorithm to solve this problem: Given 2 rectangles intersecting or overlapping together in any corner, how do I determine the total area for the two rectangles wi

6条回答
  •  执念已碎
    2020-12-23 10:22

    A Swift-version solution with analysis and LeetCode test results.

    /**
     Calculate the area of intersection of two given rectilinear rectangles.
    
     - Author:
     Cong Liu 
    
     - Returns:
     The area of intersection of two given rectilinear rectangles.
    
     - Parameters:
     - K: The x coordinate of the lower left point of rectangle A
     - L: The y coordinate of the lower left point of rectangle A
     - M: The x coordinate of the upper right point of rectangle A
     - N: The y coordinate of the upper right point of rectangle A
     - P: The x coordinate of the lower left point of rectangle B
     - Q: The y coordinate of the lower left point of rectangle B
     - R: The x coordinate of the upper right point of rectangle B
     - S: The y coordinate of the upper right point of rectangle B
    
     - Assumptions:
     All the eight given coordinates (K, L, M, N, P, Q, R and S) are integers
     within the range [-2147483648...2147483647], that is, Int32-compatible.
     K < M, L < N, P < R, Q < S
    
     - Analysis:
     The area of intersected is dyIntersected * dxIntersected.
    
     To find out dyIntersected, consider how y coordinates of two rectangles relate
     to each other, by moving rectangle A from above rectangle B down.
    
     Case 1: when N >  L >= S >  Q, dyIntersected = 0
     Case 2: when N >= S >  L >= Q, dyIntersected = S - L
     Case 3: when S >  N >  L >= Q, dyIntersected = N - L
     Case 4: when S >= N >= Q >  L, dyIntersected = N - Q
     Case 5: when N >  S >  Q >  L, dyIntersected = S - Q
     Cases 2 and 3 can be merged as Case B:
             when           L >= Q, dyIntersected = min(N, S) - L
     Cases 4 and 5 can be merged as Case C:
             when           Q >  L, dyIntersected = min(N, S) - Q
     Cases B and C can be merged as Case D:
             when      S >  L     , dyIntersected = min(N, S) - max(L, Q)
    
     Likewise, x coordinates of two rectangles relate similarly to each other:
     Case 1: when R >  P >= M >  K, dxIntersected = 0
     Case 2: when      M >  P     , dxIntersected = min(R, M) - max(P, K)
    
     - Submission Date:
     Sat 20 Jan 2018 CST at 23:28 pm
    
     - Performance:
     https://leetcode.com/problems/rectangle-area/description/
     Status: Accepted
     3081 / 3081 test cases passed.
     Runtime: 78 ms
     */
    class Solution {
      public static func computeArea(_ K: Int, _ L: Int, _ M: Int, _ N: Int, _ P: Int, _ Q: Int, _ R: Int, _ S: Int) -> Int {
        let areaA : Int = Int((M - K) * (N - L))
        let areaB : Int = Int((R - P) * (S - Q))
        var xIntersection : Int = 0
        var yIntersection : Int = 0
        var areaIntersection : Int = 0
    
        if ((min(M, R) - max(K, P)) > 0) {
          xIntersection = Int(min(M, R) - max(K, P))
        }
    
        if ((min(N, S) - max(L, Q)) > 0) {
          yIntersection = Int(min(N, S) - max(L, Q))
        }
    
        if ((xIntersection == 0) || (yIntersection == 0)) {
          areaIntersection = 0
        } else {
          areaIntersection = Int(xIntersection * yIntersection)
        }
    
        return (areaA + areaB - areaIntersection)
      }
    }
    
    // A simple test
    Solution.computeArea(-4, 1, 2, 6, 0, -1, 4, 3) // returns 42
    

提交回复
热议问题