問題描述
好的,所以我正在為一個 Java 類進行分配,其中一部分是確定一個點是否在矩形的尺寸范圍內.所以我創建了這段代碼:
ok, so i'm doing an assignment for a Java class and one part of the assignment is to find out if a point is within the dimensions of a rectangle. so I created this code:
public boolean contains(Point p) {
return (this.getLocation().getX() < p.getX() && this.getLocation().getY() < p.getY() &&
this.getLocation().getX() + this.getWidth() > p.getX() &&
this.getLocation().getY() + this.getHeight() > p.getY());
}
我也創建了一個 Point
類,這就是我要求 Point p
參數的原因.為了測試這個 boolean
我在我的 Main 類中創建了一個簡單的 if
語句:
I created a Point
class as well, which is why I asked for a Point p
parameter. To test this boolean
I created a simple if
statement in my Main class:
//check if one rectangle's point is inside another
if (rectangle.contains(rectangle2.getLocation()))
System.out.println("the point is in the rectangle");
點的位置是(6,7).矩形 1 的點、寬和高分別為 (4,5)、9 和 3.我知道這一點在第一個矩形內,但是 println
語句沒有顯示,這意味著我創建的 boolean
肯定有問題,但我沒有'沒有看到錯誤,也許我的頭是多云的,但有人可以向我指出這里有什么問題嗎?
The location of the point is (6,7). The point, width, and height of rectangle 1 is (4,5), 9, and 3, respectively. I know for a fact that this point is inside the first rectangle, but the println
statement is not showing, meaning there must be a problem with the boolean
i created but I don't see an error, maybe my head is cloudy but can someone point out to me what's wrong here?
附:這都是控制臺工作,我不是在處理一些 GUI 或圖形編程.
P.S. this is all Console work, i'm not dealing with some GUI or graphics programming.
推薦答案
我覺得沒問題.我會檢查你的測試用例是否真的有你認為的數字;我還會檢查您的訪問器是否都返回了正確的值(我無法告訴您我將 getX() 實現為 {return this.y;} 的次數).除此之外,這是任何人的猜測.
It looks ok to me. I would check that your test case actually has the numbers you think it does; I would also check that your accessors are all returning the right values (I can't tell you the number of times I've implemented getX() as {return this.y;}). Other than that it's anyone's guess.
這篇關于檢查一個點是否在指定的 Rectangle 內的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!