检查一个点是否在指定的矩形内

发布于 2024-10-23 18:33:51 字数 869 浏览 1 评论 0原文

好的,所以我正在为 Java 类做一项作业,作业的一部分是查明一个点是否在矩形的尺寸内。所以我创建了这段代码:

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());
}

我还创建了一个 Point 类,这就是为什么我要求使用 Point p 参数。为了测试这个布尔值,我在主类中创建了一个简单的 if 语句:

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

该点的位置是 (6,7)。矩形 1 的点、宽度和高度分别为 (4,5)、9 和 3。我知道这一点位于第一个矩形内,但是 println 语句没有显示,这意味着我创建的 boolean 肯定有问题,但我不知道没有看到错误,也许我的头脑很模糊,但有人可以向我指出这里出了什么问题吗?

PS 这都是控制台工作,我不处理一些 GUI 或图形编程。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事未完 2024-10-30 18:33:51

AWT 矩形已经有 contains 方法。 (链接

任务似乎是关于您是否了解命名空间如何冲突。例如,如果您很懒(这是程序员最受赞赏的品质之一),那么您可以这样写:

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);
    }
}

您通常不想重新实现功能或扩展类。

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.

_畞蕅 2024-10-30 18:33:51

我看起来还不错。我会检查您的测试用例是否确实具有您认为的数字;我还会检查您的访问器是否都返回正确的值(我无法告诉您我将 getX() 实现为 {return this.y;} 的次数)。除此之外,这是任何人的猜测。

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.

末骤雨初歇 2024-10-30 18:33:51

通常在处理计算机图形时,左上角的点是(0,0),右下角的点是(宽度,高度)。

这意味着你应该改变你的条件

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

煮酒 2024-10-30 18:33:51

虽然这是一个幼稚的方法,但我尝试了以下概念:

如果 Point (px,py) 在给定的矩形内,则通过连接 2 个矩形点和给定点形成的三角形面积之和 (比如说逆时针或顺时针方向)将等于矩形的总和。

我有一张相同的图片,但由于声誉较低(因为我是新手),无法发布。

当我将其表述为实际的 Java 代码时,我必须处理这样的情况:
小数部分有 15 个 9 的面积值四舍五入到最接近的整数。

参考这段代码:

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));
    }
}

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));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文