Reputation: 1326
I have two Rectangles:
Rectangle rectZR = new Rectangle(513, 163, 38, 39); //(x,y,width,height)
Rectangle rectCorrect = new Rectangle(76, 206, 44, 44);
and I do:
Rectangle intersect = rectZR.intersection(rectCorrect);
As the coordinates show, the two rectangles actually do not intersect, but Java still returns me an intersection and intersect
has the following dimension (x,y, width, height):
513, 206, -393, -4
Is this a bug, or am I doing something wrong?
Upvotes: 1
Views: 2179
Reputation: 14247
JavaDoc on intersect method states:
Computes the intersection of this Rectangle with the specified Rectangle. Returns a new Rectangle that represents the intersection of the two rectangles. If the two rectangles do not intersect, the result will be an empty rectangle.
While JavaDoc for the whole class specifies:
A Rectangle whose width or height is negative has neither location nor dimension along those axes with negative dimensions. Such a Rectangle is treated as non-existant along those axes. Such a Rectangle is also empty with respect to containment calculations and methods which test if it contains or intersects a point or rectangle will always return false. Methods which combine such a Rectangle with a point or rectangle will ignore the Rectangle entirely in generating the result. If two Rectangle objects are combined and each has a negative dimension, the result will have at least one negative dimension.
So, rectangle created by intersection with both negative dimensions actually means that there is no intersection. You can check this with intersects()
method:
boolean intersects = rectZR.intersects(rectCorrect);
Upvotes: 3