19

I am searching for an algorithm where I can check whether a convex polygon (shape 1) fits into another polygon (shape 2).

My first research brought me to "Packing irregular shapes". This is in my opinion a little bit overkill. I only have one container and one object.

Shape 1 is normally a convex polygon. Shape 2 can be convex, or concave.

My application: I have 3D laser scanner to measure logs, which gives me shape 2. I also have different cutting profiles from which I consider the convex hull, giving shape 1.

Now I want to check whether a cutting profile fits into my laser profile.

6
  • You could just try brute force and see if it's fast and accurate enough. E.g. three nested loops for rotation, x translation and y translation.
    – Henrik
    May 11, 2017 at 13:30
  • You want to know the path and rotations (if there exists one) that you have to describe with shape 1, so that its vertices will cut out shape 2 (without cutting away too much)?
    – maraca
    May 11, 2017 at 18:49
  • Have you asked this question on other Stack Exchange sites? Like math.stackexchange.com
    – G4Hu
    May 17, 2017 at 10:19
  • I think you are looking for a graph-theory algorithm of some sort.. and as a side-note they're pretty complex.. your other option is to apply triangulation and work with graphics-processing algorithm
    – Khaled.K
    Jul 10, 2017 at 6:50
  • 1
    Can shapes be rotated? Jul 27, 2017 at 17:21

2 Answers 2

9

Motivation. If you would ask whether a disk B of certain radius can fit into a polygon P then there is a standard method in computational geometry: Check whether the maximum inscribed circle has a radius not smaller; see this stackoverflow answer:

Maximum inscribed circle

The above algorithm to compute the maximum inscribed circle is quite "simple": Compute the so-called Generalized Voronoi Diagram and take the Voronoi node with largest clearance radius. (This is only motivation, just keep reading for a minute.)

In your case your Shape 2 is not a ball; well, not a Euclidean ball, to be precise. But, actually, your Shape 2, as a convex polygon B, could define a convex distance function and compute the Voronoi diagram under this polyhedral distance function. But this is more theoretical background and maybe not something you want to implementation for production code.

Those Voronoi diagrams are strongly related to computing offset curves, e.g., for tool-path planing in NC-machining. See this blog article for some discussions and the following figure:

offset curves

A ball B of radius r fits into the shape P if and only if there is an offset curve at distance r. (You actually get the set of all valid centers, namely those within the offset curve at radius r.) And those offset curves can be mathematically described as a Minkowski difference, as outlined in the blog article.

Minkowski-difference. So now we can come back to your original problem. Does a convex polygon B fit within a polygon P? It does if and only if the Minkowski difference (P-B) is a non-empty set; any center out of (P-B) works as an example.

Minkowski difference

A few more details based on the figure above: Let us denote by -B = {-v : v in B} the shape B after point reflection. (Choose the origin anywhere you like; I denoted it by the little cross with 'o' for origin.) Now think of -B as the shape of a pen (blue) and you move your pen (actually the cross) along the boundary of P. You get the gray area. (This is the Minkowski sum of the boundary of P with -B.) Remove the gray area from P and you get the Minkowski difference P-B. Choose any point within P-B and place a copy of B there; it will fit within P. I placed a few copies (orange) for you.

Implementation. You can construct the gray area by considering each segment s of P individually and slide -B along it. More precisely, you place a copy of -B on each endpoint of s and find the tangents of the two copies of -B that form the boundary of the gray area:

Minkowski sum per line

Take the per-segment solutions and compute the union over all segments of P. Then subtract this union from P. Take a look at Clipper for an open source library that can do that for you.

What you get is not only the boolean answer whether B fits into P, but the set of all valid center for B in form of an set of polygons. Maybe this is interesting for your application, too.

If you allow for rotations of B as well then the problem gets significantly more complex, I think. Maybe you can work with some discretization of rotational angles. Maybe you find some solution in the field of robot-motion planning resp. related to the piano mover's problem in computational geometry.

1
  • Thank you very much for the visual explanation of the Minkowski sum/difference (I still do not know which is which, or even if they are different things), but your choice of shape and non-centered origin point for the polygon B helped a lot. I was trying to understand why my collision detection code for two rects was failing when I did not use their center as the origin. Now I finally understood why: I was not "reflecting". Your image would be a more than welcome addition to the (IMO) severely visually lacking wikipedia article on the Minkowski sum. Thanks! Feb 25, 2018 at 18:09
2

Perquisites : you should have all vertex coordinate of both (PolygonA,PolygonB) convex polygons.

Step1: Put all the points of both convex polygon in one set .
Step2: Find the convex polygon using grahamscan with new set of points.
Step3: Now you have Big convex polygon which will contain both convex polygons . That means you have vertex of the newly create polygon let's call it PolygonC.

Step4:

  • Now check if polygonC and PolygonA are same with same set of vertex points

  • if yes that means PolygonA contains PolygonB

  • If above condition is not true repeat same check with PolygonB

    if Above condition is not true for any of Polygon then no polygon is fitting into another polygon .

    Graham Scan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.