Checking if a point is inside the specified rectangle

ok, so I'm doing the assignment for the Java class, and one part of the job is to find out if the point is within the size of the 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 also created a point class, so I asked for the "Point p" parameter. To test this boolean, I created a simple "if" statement in my main class:

 //check if one rectangle point is inside another if (rectangle.contains(rectangle2.getLocation())) System.out.println("the point is in the rectangle"); 

Point location (6.7). The point, width, and height of rectangle 1 are (4,5), 9, and 3, respectively. I know that this point is inside the first rectangle, but the println instruction is not displayed, that is, there must be a problem with the Boolean I created, but I do not see an error, maybe my head is cloudy, but can someone tell me that here not this way?

PS it all works in the console, I do not do graphical interface or graphical programming.

+6
java geometry
source share
4 answers

It looks good to me. I would check that your test case really has numbers that you think it does; I would also like to check that your accessors all return the correct values ​​(I cannot tell you how many times I implemented getX () as {return this.y;}). In addition, this is all guessed.

+3
source share

AWT Rectangle already has a contains method. ( link )

The task looks if you understand how namespaces conflict. For example, if you are lazy (this is one of the 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); } } 

Usually you do not want to override functions and not extend classes.

+8
source share

Typically, when working with computer graphics, the upper left point is (0,0), and the lower right corner (width, height).

This means that you must change your conditions.

+1
source share

Although it is a naive method, I tried the following concept:

If the point Point (px, py) is inside the given rectangle, the sum of the areas of the triangles formed by the union of two rectangular points and the given point (say, counterclockwise or clockwise) will be equal to the sum of the rectangle.

I have a picture for the same, but because of the low reputation (since I'm new), I can not publish it.

When I formulated this in real Java code, I had to handle a situation where the value of the area with the decimal part, which has 15 9 s, is rounded to the nearest integer.

Refer to 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)); } } 
0
source share

All Articles