Java 如何编写在对象重叠或位于另一个对象中时返回true的方法

Java 如何编写在对象重叠或位于另一个对象中时返回true的方法,java,methods,Java,Methods,我已经完成了程序的其余部分,我只是对这些方法感到困惑。我们在课堂上没有复习过这个,笔记上也没有。我不想要答案,我只需要一双新的眼睛 编辑:忘记添加方法的说明 如果指定的点(x,y)位于该矩形内,则该方法包含(双x,双y),并返回true。见下图。方法包含(矩形2D矩形),如果指定的矩形在此矩形内,则该方法将返回true。请参见图..如果指定的矩形与此矩形重叠,则返回true的方法重叠(矩形2D矩形)。见下图 class Rectangle2D { double x; double

我已经完成了程序的其余部分,我只是对这些方法感到困惑。我们在课堂上没有复习过这个,笔记上也没有。我不想要答案,我只需要一双新的眼睛

编辑:忘记添加方法的说明 如果指定的点(x,y)位于该矩形内,则该方法包含(双x,双y),并返回true。见下图。方法包含(矩形2D矩形),如果指定的矩形在此矩形内,则该方法将返回true。请参见图..如果指定的矩形与此矩形重叠,则返回true的方法重叠(矩形2D矩形)。见下图

class Rectangle2D {
    double x;
    double y;
    double width;
    double height;


    public Boolean Contains(double x, double y) {
        return false;
    }

    public Boolean Contains(Rectangle2D R1) {
        return false;
    }

    public Boolean Overlap(Rectangle2D R1) {
        return false;
    }
}

一点数学知识,其实很简单

class Rectangle2D {
    double x;
    double y;
    double width;
    double height;

    /**
     * This rectangle contains the specified point if
     *
     * The x coordinate of the point lies between x and x + width
     *
     * and
     *
     * The y coordinate of the point lies between y and y + height
     *
     * @param x - The x position of the coordinate to check
     * @param y - The y position of the coordinate to check
     * @return true if the specified coordinate lies within the rectangle.
     */
    public boolean contains(double x, double y) {
        return x >= this.x
                && y >= this.y
                && x <= this.x + this.width
                && y <= this.y + this.height;
    }

    /**
     * The rectangle contains the specified rectangle if the rectangle contains both diagonally opposite corners.
     *
     * @param r - The rectangle to check.
     * @return - true if the specified rectangle is entirely contained.
     */
    public boolean contains(Rectangle2D r) {
        return contains(r.x, r.y)
                && contains(r.x + r.width, r.y + r.height);
    }

    /**
     * The rectangle overlaps the specified rectangle if the rectangle contains any of the corners.
     *
     * @param r - The rectangle to check
     * @return - true if any corner of the rectangle is contained.
     */
    public boolean overlaps(Rectangle2D r) {
        return contains(r.x, r.y)
                || contains(r.x + r.width, r.y + r.height)
                || contains(r.x, r.y + r.height)
                || contains(r.x + r.width, r.y);
    }
}
类矩形2D{
双x;
双y;
双倍宽度;
双高;
/**
*此矩形包含指定的点,如果
*
*点的x坐标位于x和x+宽度之间
*
*及
*
*点的y坐标位于y和y+高度之间
*
*@param x-要检查的坐标的x位置
*@param y-要检查的坐标的y位置
*@如果指定的坐标位于矩形内,则返回true。
*/
公共布尔包含(双x,双y){
返回x>=此.x
&&这个

&&这是一个算法问题如果你想学习编程,你必须学会拿一张纸,检查你是如何手动操作的,这样你就可以“自动化”这个process@Delton所有这些信息都应该在问题本身中,而不是在注释中。拿一张纸,在上面画一些正方形,一些重叠。提出一些关于如何确定两个正方形是否重叠的观点,我发现视觉表示更容易,然后看看你是否能写一些代码来匹配你的答案ideas@vmrvictor我一直在打保龄球我花了几天时间在我的电脑上试着弄明白。我会试试的。真傻。哈哈,为什么我没想到。不过我是朝那个方向走的。我把它画在纸上,刚刚完成了第一个包含方法。