检查点是否在指定的Rectangle内 [英] checking if a point is inside a specified Rectangle

查看:258
本文介绍了检查点是否在指定的Rectangle内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我为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());
}

我也创建了一个点类,这就是为什么我要求点p参数。为了测试这个布尔值,我在我的Main类中创建了一个简单的'if'语句:

  //检查一个矩形的点是否在里面另一个
if(rectangle.contains(rectangle2.getLocation()))
System.out.println(点在矩形中);

点的位置是(6,7)。矩形1的点,宽度和高度分别是(4,5),9和3。我知道这一点是在第一个矩形内,但println语句没有显示,这意味着我创建的布尔值必须存在问题,但我没有看到错误,也许我的头部多云,但是可以有人指出我这里有什么不对?



PS这是所有控制台的工作,我不处理一些GUI或图形编程。 我会检查你的测试用例实际上是否有你认为它的数字;我也会检查你的访问器是否都返回了正确的值(我不能告诉你我实现getX()的次数为{return this.y;})。除此之外,这是任何人的猜测。


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.

解决方案

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内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆