-1

I have the rest of the program finished I am just stumped on these methods. we didn't go over this in class and its not in any of the notes. I don't want the answer per se I just need a fresh pair of eyes.

Edit: forgot to add the instructions for the methods A method contains(double x, double y) that returns true if the specified point (x, y) is inside this rectangle. See Figure below. .A method contains(Rectangle 2D rectangle) that returns true if the specified rectangle is inside this rectangle. See Figure. .A method overlaps(Rectangle 2D rectangle) that returns true if the specified rectangle overlaps with this rectangle. See the figure.

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;
    }
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
Delton
  • 9
  • 3
  • 1
    This is an algorithm question – flyingfox Oct 05 '18 at 05:26
  • if you want to learn to program you have to learn to take a piece of paper and check how are you doing manually so then you can "automatize" the process – vmrvictor Oct 05 '18 at 05:28
  • @Delton All of that information should be in the question itself, not in a comment. – mypetlion Oct 05 '18 at 05:28
  • 3
    Get a piece of paper and draw some squares on it, some overlapping. Make some points about how might determine if two squares overlap, I find a visual representation easier, then see if you can write some code to match your ideas – MadProgrammer Oct 05 '18 at 05:28
  • @vmrvictor I have been looming over my computer for a few days trying to figure it out. I will try that. – Delton Oct 05 '18 at 05:39
  • Bouncing some ideas off here. double x and double y represent the bottom left corner. If the point or object were in the rectangle, then any point on the object should have a greater x value or y value or both. Sounds like an if else statement. – Delton Oct 05 '18 at 05:46
  • Possible duplicate of [Collision detection between two rectangles in java](https://stackoverflow.com/questions/31022269/collision-detection-between-two-rectangles-in-java) – Talendar Oct 05 '18 at 05:56
  • fresh pair of eyes on What!!! – gpasch Oct 05 '18 at 06:39
  • @gpasch a fresh pair of eyes on creating the methods. I have been going over different ways to approach the methods, hence " a new set of eyes" could help. No need to be so excited, its just a phrase lol. – Delton Oct 05 '18 at 06:50

1 Answers1

1

A little bit of maths and it's actually quite easy.

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);
    }
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • What a dolt. lol why didn't I think of that. I was headed that way though. I drew it out on paper an had just finished the first contains method. – Delton Oct 05 '18 at 07:00