Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking if a point is inside a specified Rectangle

Tags:

java

geometry

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.

like image 623
mastrgamr Avatar asked Mar 17 '11 20:03

mastrgamr


People also ask

How do you check if a point lies inside a rectangle?

Approach: If we observe carefully, It will be clear that for a point to be lie inside the rectangle, it should be lie inside the x-coordinate (x1, x2) of the rectangle as well as it should also lie inside the y-coordinate (y1, y2) of the rectangle.

How do you determine if a point is inside an area?

Draw a horizontal line to the right of each point and extend it to infinity. Count the number of times the line intersects with polygon edges. A point is inside the polygon if either count of intersections is odd or point lies on an edge of polygon.

How do you check if a point is within a rectangle Java?

Set boolean b to true if if the point with coordinates (x,y) is inside the rectangle with coordinates (x1,y1,x2,y2) , or to false otherwise. Describe if the edges are considered to be inside the rectangle. boolean b = x <= x2 && x >= x1 && y <= y2 && y >= y1; Assume x1 <= x2 and y1 <= y2.

How do you find the inside of a rectangle?

To find the area of a rectangle, multiply its width by its height. If we know two sides of the rectangle that are different lengths, then we have both the height and the width.


2 Answers

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.

like image 83
Margus Avatar answered Oct 13 '22 00:10

Margus


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.

like image 38
Paul Z Avatar answered Oct 13 '22 00:10

Paul Z