2015-10-11 103 views
-2

我需要找到使用Java的兩個可能重疊矩形區域的總和。如何使用Java找到兩個可能重疊矩形區域的總和?

條件: 每個矩形由4個雙打是這樣的:(X0,Y0,X1,Y1) 它們可以在邊緣處進行接觸,重疊,或者沒有任何接觸

任何幫助理解。

+3

請您詳細說明您的要求嗎?什麼是'x0,y0,x1,y1'?你到目前爲止嘗試過什麼? – Rehman

回答

1

有需要被照顧,在我下面的代碼,我只顯示此圖像中的情況下,許多情況下:

enter image description here

所以這裏是該案例的代碼,你將需要編寫爲其他情況:

public class Problem { 

public static class Rectangle { 

    double x0; 
    double y0; 
    double x1; 
    double y1; 

    Rectangle(double x0, double y0, double x1, double y1) { 

     this.x0 = x0; 
     this.y0 = y0; 
     this.x1 = x1; 
     this.y1 = y1; 

    } 

} 

public static void main(String[] args) { 

    Rectangle A = new Rectangle(2.0, 2.0, 10.0, 5.0); 
    Rectangle B = new Rectangle(0.0, 0.0, 6.5, 3.5); 
    double area = 0.0; 


    if(A.x1 - B.x0 > 0 && A.y1 - B.y1 > 0) { 

     System.out.println("they're overlaping"); 
     area = (A.x1 - B.x0) * (A.y1 - B.y1); 
     System.out.println(area); 

    } else if (B.x1 - A.x0 > 0 && B.y1 - A.y1 > 0) { 

     System.out.println("they're overlaping"); 
     area = (B.x1 - A.x0) * (B.y1 - A.y1); 
     System.out.println(area); 

    } else if (other conditions....) { 

     // you're code here 
    } 


} 

} 

第一如果完全一樣在圖像中看到的情況的上方,並且下,如果條件是當B和A開關位置(當B是向上左,和甲是右下)。

相關問題