checking if a point is inside a specified Rectangle

前端 未结 4 1088
既然无缘
既然无缘 2020-12-11 16:41

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:

相关标签:
4条回答
  • 2020-12-11 17:14

    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.

    0 讨论(0)
  • 2020-12-11 17:16

    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:

    import static java.lang.Math.sqrt;
    
    public class PointInsideRect
    {
        private static double square(double n)
        {
            return n*n;
        }
        private static double areaOfTriangle(
                    int xa, int ya,
                    int xb, int yb,
                    int px, int py )
        {
            double side1 = sqrt(square(ya-yb) + square(xa-xb));
            double side2 = sqrt(square(ya-py) + square(xa-px));
            double side3 = sqrt(square(yb-py) + square(xb-px));
    
            double semi_perimeter = (side1 + side2 + side3) / 2;
    
            return sqrt(semi_perimeter
                        * ( semi_perimeter - side1 )
                        * ( semi_perimeter - side2 )
                        * ( semi_perimeter - side3 ));
        }
    
        private static double areaOfRect(
                    int x1, int y1,
                    int x2, int y2,
                    int x3, int y3,
                    int x4, int y4 )
        {
            double side1 = sqrt(square(y1-y2) + square(x1-x2));
            double side2 = sqrt(square(y2-y3) + square(x2-x3));
            return side1 * side2;
        }
    
        public boolean check(
                    int x1, int y1,
                    int x2, int y2,
                    int x3, int y3,
                    int x4, int y4, 
                    int pointX, int pointY)
        {
            double tri1Area = areaOfTriangle(x1,y1, x2,y2, pointX,pointY);
            double tri2Area = areaOfTriangle(x2,y2, x3,y3, pointX,pointY);
            double tri3Area = areaOfTriangle(x3,y3, x4,y4, pointX,pointY);
            double tri4Area = areaOfTriangle(x4,y4, x1,y1, pointX,pointY);
    
            double rectArea = areaOfRect(x1,y1, x2,y2, x3,y3, x4,y4);
    
            double triAreaSum = tri1Area + tri2Area + tri3Area+ tri4Area;
    
    
            if(triAreaSum % Math.pow(10, 14) >= 0.999999999999999)
            {
                triAreaSum = Math.ceil(triAreaSum);
                System.out.println(triangleAreaSum);
            }
            return triAreaSum == rectArea;
        }
    
    
        public static void main(String[] args)
        {
            PointInsideRect inRect = new PointInsideRect();
    
            System.out.println(inRect.check(1,1, 1,3, 3,3, 3,1, 2,2));
        }
    }
    
    0 讨论(0)
  • 2020-12-11 17:19

    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.

    0 讨论(0)
  • 2020-12-11 17:24

    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

    0 讨论(0)
提交回复
热议问题