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