1

My assignment is to check if two points are inside a rectangle, check if a rectangle is inside another rectangle, and also check if a rectangle overlaps another rectangle

I completed the first two steps but I can't solve the overlapping method.

This what I've done.

public class MyRectangle2D {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        MyRectangle2D rect = new MyRectangle2D();

        //returns area of default rectangle
        System.out.println(rect.getArea());

        //returns perimeter of default rectangle
        System.out.println(rect.getPerimeter());

        //returns true if default (0,0) is inside rectangle
        System.out.println(rect.contains(0,0));

        //returns false if point is not in triangle
        System.out.println(rect.contains(10,10));

        MyRectangle2D r1 = new MyRectangle2D(10,2,6,4);
        System.out.println("The area of your rectangle is " + r1.getArea() + 
            " \nthe perimeter of your rectangle is " + r1.getPerimeter());

        System.out.println("Testing Rectangle 2 with points (1,8)...");

        //following lines test the contain(x,y) method
        System.out.println(r1.contains(1,8) ? "Point (1,8) is on Rectangle" : "Point(1,8) is not on Rectangle");

        System.out.println("Testing Rectangle 2 with points (10,1)...");

        System.out.println(r1.contains(10,1) ? "Point (10,1) is on Rectangle" : "Point(10,1) is not on Rectangle");

        //following lines test contains(rect) method
        System.out.println(r1.contains(new MyRectangle2D(9,1,1,1)) ? "Rectangle is inside" : "Not inside");
        System.out.println(r1.contains(new MyRectangle2D(9,1,10,2)) ? "Rectangle is inside" : "Rectangle is not inside");

        //following lines test if rectangles overlap
        System.out.println(r1.overlaps(new MyRectangle2D(4,2,8,4)));
    }

    double x;
    double y;
    double width;
    double height;

    private MyRectangle2D(){
        x = 0;
        y = 0;
        width = 1;
        height = 1;
    }

    private MyRectangle2D(double newX, double newY, 
             double newWidth, double newHeight){
         setX(newX);
         setY(newY);
         setHeight(newHeight);
         setWidth(newWidth);
     }

    double getX(){
        return x;
    }

    double getY(){
        return y;
    }

    void setX(double newA){
        x = newA;
    }

    void setY(double newA){
        y = newA;
    }

    double getWidth(){
        return width;
    }

    double getHeight(){
        return height;
    }

    void setWidth(double newA){
        width = newA;
    }

    void setHeight(double newA){
        height = newA;
    }

    double getArea(){
        double area = width * height;
        return area;
    }

    double getPerimeter(){
        double perimeter = (2 * width) + (2 * height);
        return perimeter;
    }

    boolean contains(double x, double y){
        boolean inside = false;
        if(x<(getWidth()/2 + getX()) && x>getX()-(getWidth()/2)){
            if(y<(getY() + getHeight()/2) && y>getY()-(getHeight()/2))
            inside = true;
        }
        return inside;
    }

    boolean contains(MyRectangle2D r){
        boolean inside = false;
        if(r.getX()<(getWidth()/2 + getX()) && r.getX()>getX()-(getWidth()/2)){
            if(r.getY()<(getY() + getHeight()/2) && r.getY()>getY()-(getHeight()/2)){
                if(r.getWidth()<=getWidth() && r.getHeight()<=getHeight())
                inside = true;
            }   
        }   
        return inside;
    }

    boolean overlaps(MyRectangle2D r){
        boolean inside = false;
        if(contains(r.getX()+r.getWidth(),r.getY()+r.getHeight()))
        inside = true;

        if(contains(r.getX()-r.getWidth(),r.getY()-r.getHeight()))
        inside = true;
        return inside;

    }

}
1

1 Answer 1

1

This will find if the rectangle is overlapping another rectangle, don't forget to mark this as the answer and vote up when you can.

public boolean overlaps (MyRectangle2D r) {
    return x < r.x + r.width && x + width > r.x && y < r.y + r.height && y + height > r.y;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sucking up doesn't suit you
@MadProgrammer thanks for the input, don't forget to upvote my answer, and all my other ones too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.