0

There are 2 areas (java.awt.geom.Area) area1 and area2. I need to verify if area2 is inside area1? (area1 contains area2).

I have tried:

areaDelta = (Area)area1.clone();
areaDelta.add(area2);
return areaDelta.equals(area1);

But it doesnt always work as it should (If the bounds of area2 are on the bounds of area1, it returns true, should return false).

In fact I have 2 polygons (java.awt.Polygon) and I need the Polygon.contains(Polygon) method, may be this can be easier then for areas.

Any ideas?

Polygons can be convex and non-convex.

Cenius
  • 103
  • 1
  • 1
  • 9
  • Check for http://stackoverflow.com/questions/4833802/check-if-polygon-is-inside-a-polygon and http://stackoverflow.com/questions/3017872/determining-polygon-intersection-and-containment – Napte Jan 18 '12 at 13:56
  • can you get away with a bounding rectangle check? – Randy Jan 18 '12 at 14:12
  • Found it - how to do It without any additional libraries: do the line intersection check for each pair of polygon's lines, It isn't hard to do with standart java library. But It isn't the most perfomance solution. – Cenius Jan 18 '12 at 14:24

1 Answers1

1

Since you have Polygon.contains(Point), you can achieve what you need by testing if every point of your second polygon is inside the first polygon (I dream of a JDK version which would implement every basic need of a programmer, like .NET).

EDIT: To handle concave polygons, you first need to split your polygons into convex ones, then you'll be able to properly use the method mentioned above. I used two different polygon decomposition algorithms in one of my applications, and I would suggest you to look at their code:

Aurelien Ribon
  • 7,548
  • 3
  • 43
  • 54
  • It doesn't work because the polygons can be both convex and non-convex. I've thought about it. About a note - thank you, but I know it, someone has edited my post... – Cenius Jan 18 '12 at 14:08
  • It works if you also check that every vertex of the first polygon is outside the second polygon. – Joni Jan 18 '12 at 15:16
  • You can also convert your polygons into convex ones, then the test is trivial. See my edition. – Aurelien Ribon Jan 18 '12 at 15:22
  • Thank you, but it means a full rewrite of standart library (there is no decomposion function) or using additional libraries. Anyway, it works, what I needed. I'll try to link this with java's standart one. – Cenius Jan 19 '12 at 12:24