检查一个点是否在指定的矩形内
好的,所以我正在为 Java 类做一项作业,作业的一部分是查明一个点是否在矩形的尺寸内。所以我创建了这段代码:
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 参数。为了测试这个布尔值,我在主类中创建了一个简单的 if 语句:
//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
肯定有问题,但我不知道没有看到错误,也许我的头脑很模糊,但有人可以向我指出这里出了什么问题吗?
PS 这都是控制台工作,我不处理一些 GUI 或图形编程。
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());
}
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");
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?
P.S. this is all Console work, i'm not dealing with some GUI or graphics programming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AWT 矩形已经有
contains
方法。 (链接)任务似乎是关于您是否了解命名空间如何冲突。例如,如果您很懒(这是程序员最受赞赏的品质之一),那么您可以这样写:
您通常不想重新实现功能或扩展类。
AWT Rectangle already has
contains
method. ( link )Task seems about if you understand how naming spaces conflict. For example, if you are lazy (it's one of most admired qualities of a programmer), then you can write:
You generally do not want to reimplementing features nor extend classes.
我看起来还不错。我会检查您的测试用例是否确实具有您认为的数字;我还会检查您的访问器是否都返回正确的值(我无法告诉您我将 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.
通常在处理计算机图形时,左上角的点是(0,0),右下角的点是(宽度,高度)。
这意味着你应该改变你的条件
Usually when dealing with computer graphics, the top left point is (0,0) and the bottom right corner is (width, height).
This means that you should reverse your conditions
虽然这是一个幼稚的方法,但我尝试了以下概念:
如果
Point
(px,py) 在给定的矩形内,则通过连接 2 个矩形点和给定点形成的三角形面积之和 (比如说逆时针或顺时针方向)将等于矩形的总和。我有一张相同的图片,但由于声誉较低(因为我是新手),无法发布。
当我将其表述为实际的 Java 代码时,我必须处理这样的情况:
小数部分有 15 个 9 的面积值四舍五入到最接近的整数。
参考这段代码:
Though its a naive method, I tried the following concept:
If the
Point
(px,py) is inside the given rectangle, the sum of areas of triangles formed by joining 2 rectangle points and the given point (say in anti-clockwise or clockwise direction) would be equal to the sum of rectangle.I have a picture for the same, but due to low reputation (as I am a newbie), cannot post it.
When I was formulating this into actual Java code, I had to handle a situation where
the area value with decimal part having 15 9s was rounded to its nearest integer.
Refer this code: