How can I see if two rectangles have one equal point using java?

I have rectangle A = upper left point (-4,-1), bottom right point (-2-2) and rectangle B upper left point (-2,-2), bottom right point (-1,-3)

then:

Rectangle first = new Rectangle(-4,-1,2,1);  //x1,y1,width,height
Rectangle second = new Rectangle(-2,-2,1,1);

OK, I have 2 rectangles with 1 same point. I use first.intersects(second) and this returns false..

How can I use java to do this, I need something return True if One point or more into Rectangle A belongs Rectangle B?

My code now:

public class GeometriaRectangle2 {

public static void main(String[] args) {
    Scanner lector = new Scanner(System.in);
    while (lector.hasNextLine()) {
        String a[] = lector.nextLine().split(",");
        int b[] = new int[a.length];

        for (int i = 0; i < a.length; i++) {
            b[i] = Integer.parseInt(a[i]);
        }
        int c = Math.abs(b[0] - b[2]);
        int d = Math.abs(b[1] - b[3]);
        int e = Math.abs(b[4] - b[6]);
        int f = Math.abs(b[5] - b[7]);
        Rectangle r = new Rectangle(b[0], b[1], c, d);
        Rectangle r2 = new Rectangle(b[4], b[5], e, f);
        int tw = r.width;
        int th = r.height;
        int rw = r2.width;
        int rh = r2.height;
        if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
            System.out.println(false);
        }
        int tx = r.x;
        int ty = r.y;
        int rx = r2.x;
        int ry = r2.y;
        rw += rx;
        rh += ry;
        tw += tx;
        th += ty;
        // overflow || intersect or attached
        if ((rw < rx || rw >= tx) && (rh < ry || rh >= ty)
                && (tw < tx || tw >= rx) && (th < ty || th >= ry)) {
            System.out.println(true);
        } else {
            System.out.println(false);
        }
    }
}

}

Example: input:

-3,3,-1,1,1,-1,3,-3

-3,3,-1,1,-2,4,2,2

output:

False

True

Upvotes: 2

Views: 231

Answers (2)

You may also modifies the current code of intersects function to suit your needs. In your case you want to state it true when there is a pair of points from two rectangles (even the side line's) are sharing the same coordinate - (read: attached). Thus, you may try something like this:

public boolean intersectsOrAttached(Rectangle r, Rectangle r2) {
    int tw = r.width;
    int th = r.height;
    int rw = r2.width;
    int rh = r2.height;
    if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
        return false;
    }

    int tx = r.x;
    int ty = r.y;
    int rx = r2.x;
    int ry = r2.y;
    rw += rx;
    rh += ry;
    tw += tx;
    th += ty;

    //overflow || intersect or attached
    return ((rw < rx || rw >= tx) &&
        (rh < ry || rh >= ty) &&
        (tw < tx || tw >= rx) &&
        (th < ty || th >= ry));
}

UPDATE:

Actually online judging system is a little bit tricky. I managed to get it accepted partially, I'm not sure whether it's time or memory constraints, as well as some overlooked bound-cases that we couldn't figure out easily. But here it is, it's better if you can make it optimized by yourself:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class OverlappingRectangles {

    public static void main(String[] args) {
        String f = args[0];
        try {
            BufferedReader br = new BufferedReader(new FileReader(f));
            while (br.ready()) {
                String cs[] = br.readLine().split(",");
                int[] ci = new int[cs.length];
                int i = 0;
                for (String s : cs) {
                    ci[i] = Integer.parseInt(s);
                    i += 1;
                }

                if (ck(ci[0], ci[1], ci[2], ci[3], ci[4], ci[5], ci[6], ci[7])) {
                    System.out.println("True");
                } else {
                    System.out.println("False");
                }
            }
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }

    public static boolean ck(int rx, int ry, int rw, int rh, int tx, int ty, int tw, int th) {
        //ATTENTION!
        //rx,ry -> upper-left corner of rect A
        //rw,rh -> bottom-right corner of rect A
        //tx,ty -> upper-left corner of rect B
        //tw,th -> bottom-right corner of rect B
        return ((rw < rx || rw >= tx)
                && (rh < ry || rh >= ty)
                && (tw < tx || tw >= rx)
                && (th < ty || th >= ry));
    }
}

Upvotes: 0

Jason C
Jason C

Reputation: 40335

java.awt.Rectangle is part of AWT and is designed for representing discrete areas in images (and on the screen). Points and dimensions are integers. The intersects() method does what you want, and the rectangles you have given do not intersect. false is the correct value.

Think of pixels in an image. The coordinates of a java.awt.Rectangle are essentially the centers of pixels, the dimensions are the count. So new Rectangle(-4, -1, 2, 1), for example, does not have its bottom right at (-2, -2) as you assume, its bottom right is at (-3, -1):

enter image description here

The upper-most left-most pixel of the red rectangle is the pixel at (-4, -1). The bottom-most right-most pixel is the pixel at (-3, -1). Also note that it does not overlap the blue rectangle.

Even if this were not the case, your rectangles do not intersect. They touch at one point, but do not overlap. Arguably, the intersection is a "rectangle" at (-2, -2) with width 0 and height 0 - which is empty.

Upvotes: 2

Related Questions