http://qs321.pair.com?node_id=1224773


in reply to searching polygons not merged

It seems you are looking for an easy solution to a not-so-easy problem - maybe this doesn't exist.
Very simple way is that obtain all points in 1st polygon and check which point is inside of 2nd polygon. but this algorithm is not that good performance and stupid way I think.

This algorithm will give wrong results for e.g. the following two polygons, where two rectangles intersect each other but not a single of its vertices lies within the other rectangle.

+-+ | | +--+-+--+ | | | | +--+-+--+ | | +-+

A better and easy-to-implement check would be whether any two edges of the polygons intersect. Of course, you also need to take into account that polygons might share one or more vertices, or a vertex might sit exactly on an edge of another polygon.

For performance, follow the hint by Lanx to start with the bounding boxes (waaay faster than calculating surrounding circles, sorry, hippo). This will easily eliminate polygons which don't overlap, and also provide some polygons which must overlap. Looking at my example above, if the two rectangles are bounding boxes of arbitrary polygons, then these polygons overlap.

But for sure this isn't complete. For some types of polygons, a strategy could be to cut the polygons into pieces (that's what Math::Polygon::Tree does, and it uses Math::Geometry::Planar::GPC for the hard work) and then repeat checking whether the bounding boxes overlap. Below are some border cases to check any algorithm - but maybe you know that you don't have any of these:

+----------+ +------+ +------+ | | | | | | | +-------+ | | | | | | | | | | | | +--+ | | | | | | | | | | | | | | +--+ | | | +--+ | | | | | | | | | | +-------+ +-+--+-+ +-+--+-+ | | | | +----------+ +--+