Java AWT Rectangle intersection

Java AWT Rectangle intersection

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
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);

Java AWT Rectangle intersection

Leave a Reply

Your email address will not be published. Required fields are marked *