Take the 2-minute tour ×

How would I find out if any points of a given Rectangle are inside a Polygon?

I have tried:

final Point p = new Point(rect.x, rect.y);
return polygon.contains(p);

This half works. It only returns true when the top left corner of the rectangle is in the polygon (Yes, I do know why).

Is there a way to check if ANY of the points of the rectangle are in the polygon?

Thanks in advance!

share|improve this question
have you tried polygon.intersects(rect)? –  user1097489 Jan 18 '12 at 12:09

2 Answers 2

public boolean contains(Rectangle2D r)

So basically you need to do:

share|improve this answer
@BenvanGompel thank you, missed it somehow –  Aviram Segal Jan 18 '12 at 12:20
This works better, but does ot check ALL the points. It merely just checks for a side of the rectangle ... –  JavaCoder-1337 Jan 18 '12 at 12:21
@JavaCoder-1337 documentation says true if the interior of the Shape entirely contains the Rectangle2D –  Aviram Segal Jan 18 '12 at 12:23
up vote 0 down vote accepted

I have found the best way to do this:

return polygon.intersects(rect);

share|improve this answer
Contains first use intersects, then uses Crossing (looking at the implementation), there might be cases that contains will be better, worth a deeper check. –  Aviram Segal Jan 18 '12 at 12:33

Your Answer


