Difficult:Medium
題目
Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.
The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2).
The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2).
翻譯
給定二維平面中兩個直線矩形的坐標,返回兩個矩形覆蓋的總面積。
第一個矩形由其左下角 (ax1, ay1) 和右上角 (ax2, ay2) 定義。
第二個矩形由其左下角 (bx1, by1) 和右上角 (bx2, by2) 定義。
範例
Example 1:
1 | Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2 |
Example 2:
1 | Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2 |
解題思路
- 將xarea和yarea先求出
- 將相交的面積求出
3 返回xarea+yarea-相交面積
程式碼
1 | var computeArea = function (ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) { |