checking if a point is inside a specified Rectangle
Solution 1
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.
Solution 2
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.
Solution 3
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
![mastrgamr](https://i.stack.imgur.com/3n9FD.jpg?s=256&g=1)
mastrgamr
Mobile Dev making his way through the engineering world.
Updated on August 03, 2022Comments
-
mastrgamr almost 2 years
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 aPoint p
parameter. To test thisboolean
I created a simpleif
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 theboolean
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.
-
mastrgamr over 13 yearsno luck, maybe it's worth mentioning i'm not dealing with graphics, this is all console work.
-
Captain Giraffe over 13 yearsRegardless of the orientation the conditions involved remain the same.
-
mastrgamr over 13 yearsthanks, there was a part in my code where I mixed up the width and height values of a point, instead of x for width, and y for height, i had y for width and x for height.
-
dantuch over 13 years@mastrgamr that's the reason for following one of main rules - you should name your fields and methods with name showing thier usage, so, plz, try
int width
in place ofint x
-
IEatBagels over 9 yearsMay I know why you wouldn't want to extend classes?
-
bekce about 8 yearsThis is not applicable in most cases because it does not support floating point values.