checking if a point is inside a specified Rectangl

2020-02-07 02:01发布

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.

标签: java geometry
4条回答
成全新的幸福
2楼-- · 2020-02-07 02:24

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.

查看更多
相关推荐>>
3楼-- · 2020-02-07 02:38

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:

public class pointInsideRect {

public static double areaOfTriangle(int xa,int ya, int xb, int yb, int px, int py)
{
    double side1 = Math.sqrt(Math.pow(Math.abs(ya-yb),2) + Math.pow(Math.abs(xa-xb),2));
    double side2 = Math.sqrt(Math.pow(Math.abs(ya-py),2) + Math.pow(Math.abs(xa-px),2));
    double side3 = Math.sqrt(Math.pow(Math.abs(yb-py),2) + Math.pow(Math.abs(xb-px),2));

    double semi_perimeter = (side1+side2+side3)/2;

    double area = Math.sqrt(semi_perimeter*(semi_perimeter-side1)*(semi_perimeter-side2)*(semi_perimeter-side3));

    return area;
}


public static double areaOfRect(int x1, int y1,
         int x2, int y2,
         int x3, int y3,
         int x4, int y4)
{

    double side1 = Math.sqrt(Math.pow(Math.abs(y1-y2),2) + Math.pow(Math.abs(x1-x2),2));
    double side2 = Math.sqrt(Math.pow(Math.abs(y2-y3),2) + Math.pow(Math.abs(x2-x3),2));

    double area = side1*side2;

    return area;

}



public boolean check(int x1, int y1,
                     int x2, int y2,
                     int x3, int y3,
                     int x4, int y4, 
                     int pointX, int pointY)
{   

    double trinagle1Area = areaOfTriangle(x1, y1, x2, y2, pointX, pointY);
    double trinagle2Area = areaOfTriangle(x2, y2, x3, y3, pointX, pointY);
    double trinagle3Area = areaOfTriangle(x3, y3, x4, y4, pointX, pointY);
    double trinagle4Area = areaOfTriangle(x4, y4, x1, y1, pointX, pointY);



    double rectArea = areaOfRect(x1, y1, x2, y2, x3, y3, x4, y4);

    double triangleAreaSum = (trinagle1Area+trinagle2Area+trinagle3Area+trinagle4Area);


    if(triangleAreaSum%(Math.pow(10, 14))>=0.999999999999999)
    {
        triangleAreaSum = Math.ceil(triangleAreaSum);
        System.out.println(triangleAreaSum);
    }

    if(triangleAreaSum==rectArea)
        return true;
    else
        return false;
}


public static void main(String[] args)
{
    pointInsideRect obj = new pointInsideRect();


    System.out.println(obj.check(1, 1, 1, 3, 3, 3, 3, 1, 2, 2));




}


}
查看更多
ゆ 、 Hurt°
4楼-- · 2020-02-07 02:44

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

查看更多
萌系小妹纸
5楼-- · 2020-02-07 02:46

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:

public static class Rectangle {
    java.awt.Rectangle _r;

    public Rectangle(int x, int y) {
        this._r = new java.awt.Rectangle(x, y);
    }
    public boolean contains(Point p) {
        return this._r.contains(p);
    }
}

You generally do not want to reimplementing features nor extend classes.

查看更多
登录 后发表回答