2015-11-14 34 views
4

我做了一個程序,用戶用CenterX和CenterY座標,寬度和高度創建兩個自定義矩形。用戶指定這些尺寸後,程序將顯示一個矩形是否包含另一個矩形,與另一個矩形重疊,還是完全不符合。這裏是我的代碼:矩形重疊,但說它在裏面?

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.shape.*; 
import javafx.scene.text.*; 
import javafx.scene.paint.Color; 
import javafx.scene.layout.Pane; 
import java.util.*; 

public class TwoRectangles extends Application 
{ 
    public void start(Stage stage) 
    { 
     Pane pane = new Pane(); 

     Scanner input = new Scanner(System.in); 
     System.out.print("Enter the X and Y center coordinates for rectangle1: "); 
     double xCord1 = input.nextDouble(); 
     double yCord1 = input.nextDouble(); 

     System.out.print("Enter the Width and Height for rectangle1: "); 
     double width1 = input.nextDouble(); 
     double height1 = input.nextDouble(); 

     System.out.print("Enter the X and Y center coordinates for rectangle2: "); 
     double xCord2 = input.nextDouble(); 
     double yCord2 = input.nextDouble(); 

     System.out.print("Enter the Width and Height for rectangle2: "); 
     double width2 = input.nextDouble(); 
     double height2 = input.nextDouble(); 

     Rectangle rectangle1 = new Rectangle(xCord1, yCord1, width1, height1); 
     Rectangle rectangle2 = new Rectangle(xCord2, yCord2, width2, height2); 

     rectangle1.setFill(null); 
     rectangle2.setFill(null); 
     rectangle1.setStroke(Color.BLACK); 
     rectangle2.setStroke(Color.BLACK); 

    // Compute the 4 corners's coordinates for the rectangle 
     double r1x1 = xCord1 - (width1/2.0); 
     double r1x2 = xCord1 + (width1/2.0); 
     double r1y1 = yCord1 + (height1/2.0); 
     double r1y2 = yCord1 - (height1/2.0); 

     double r2x1 = xCord2 - (width2/2.0); 
     double r2x2 = xCord2 + (width2/2.0); 
     double r2y1 = yCord2 + (height2/2.0); 
     double r2y2 = yCord2 - (height2/2.0); 


    if ((r1x1 >= r2x1) && (r1x2 <= r2x2) && (r1y1 >= r2y1) && (r1y2 <= r2y2)) 
     { 
     Text containText = new Text(500, 500, "One rectangle is contained in another"); 
     pane.getChildren().add(containText); 
     } 

    else if ((r1x1 < r2x2) && (r1x2 > r2x1) && (r1y1 < r2y2) && (r1y2 > r2y1)) 
     { 
      Text overlapText = new Text(500, 500, "The rectangles overlap"); 
      pane.getChildren().add(overlapText); 

     } 

     else 
     { 
      Text noneText = new Text(500, 500, "The rectangles do not overlap"); 
      pane.getChildren().add(noneText); 
     }  
     pane.getChildren().addAll(rectangle1, rectangle2); 
     Scene scene = new Scene(pane); 
     stage.setTitle("Overlapping Rectangles"); 
     stage.setScene(scene); 
     stage.show(); 
    } 
} 

每當我進入,(50,50)的centerX和centerY座標兩個矩形,寬度:20,爲rectangle1高度50和寬度50,爲rectangle2高度20它告訴我矩形在彼此內部而不是彼此重疊。我究竟做錯了什麼?

回答

0

你的問題是Y座標。第二個比第一個小。這意味着測試將變得不太可讀,例如

(r1x1 >= r2x1) && (r1x2 <= r2x2) && (r1y1 >= r2y1) && (r1y2 <= r2y2) 

變爲(與也檢查,如果R 2是在R1):

(r1x1 >= r2x1 && r1x2 <= r2x2 && r1y1 <= r2y1 && r1y2 >= r2y2) || 
//         ^^    ^^ 
(r2x1 >= r1x1 && r2x2 <= r1x2 && r2y1 <= r1y1 && r2y1 >= r1y2) 
+1

我要說的是,計算有錯誤'雙r1y1 = yCord1 +(height1/2.0);'必須'雙r1y1 = yCord1 - (height1/2.0);'和類似的'雙r1y2 = yCord1 +(height1/2.0);'和r2y1和r2y2以及 – ItachiUchiha

+0

@IchichiUchiha通常是的,但是當你繪畫的東西,它可以更好另一種方法。 – maraca

+0

@IchichiUchiha我把我的第一個條件改爲你的條件,但我仍然得到相同的錯誤 – Bytes