10-25-2023 07:40 AM
Hello,
I need to create a program which checks if a 4-vertex polygon is entirely within a larger 5-vertex polygon. I am following the logic behind the PIP (Point in polygon) VI which is included in LabVIEW, though I need this to be expanded to check for a whole geometry rather than a single point.
Would an 'easy' solution be to quickly cycle between all four points of the smaller polygon as the input for the PIP tool?
Thank you,
LB
10-25-2023 08:10 AM
I doubt there is an easy way to do that, not in any language.
Polygon Collision - GPWiki (archive.org)
Of course someone has probably done this before. Not sure about this: Polygon Clipping Download - NI
Any solution is very specific to the details of your problem. For instance, it makes a huge difference if one or both polygons are known to be convex.
I'm pretty sure that if all points of any polygon A are in a convex polygon B, the entire polygon A is in B. But if B isn't convex, it can be much harder, although it might help to know you only have 4 vertexes.
10-25-2023 10:16 AM - edited 10-25-2023 12:39 PM
As Wiebe already said this can be a hard problem, depending on the details.
In the most general case, you have and ordered set of four points for polygon 1 and five points for polygon 2. The problem is simple if these are regular polygons but it can get tricky of e.g. crossovers are allowed.
Have a look at the examples here. Are all these inputs for four or five points valid?
Let's have a look at the following 5 point polygon? Which locations (A,B, C, D) would be "inside"?
If you just want to distinguish between "completely inside" and "not completely inside" (assuming the "completely outside" case does not occur, but you could test for it later), you could just iterate over all pairwise sides and see if there is at last one crossover, a mathematically simple problem.
10-25-2023 10:20 AM
Draw them with different colors to a picture and see if any pixels have the wrong color? It's not 100%, but might be close enough. 🙂
(Some vectors might be just outside, but not much enough to become a pixel and so on)
10-30-2023 07:06 AM
You raise an interesting point, though my case would be much simpler than the examples you provided. Both polygons would have their vertices connected through exclusively straight lines with no cross-over.
The overall objective of the program would be to determine if there is a 'collision' while moving the smaller-internal polygon around the larger-fixed one.
10-30-2023 09:28 AM
Windows API provide a set of functions for Regions.
You can use the vertex of the outside polygon to create a region, and then use the vertex of the inside polygon to check if a point is in a region.
https://learn.microsoft.com/en-us/windows/win32/gdi/region-functions
10-30-2023 10:35 AM
@SpacePlasma wrote:
The overall objective of the program would be to determine if there is a 'collision' while moving the smaller-internal polygon around the larger-fixed one.
As I said, just iterate pairwise over all segments of each polygon (i.e. adjacent points and closure) and stop once a collision is detected. I already gave you the link for the math earlier.
So you have sides ABCD and sides VWXYZ, so do A?V, A?W, A?X, ..... D?Y, D?Z.
10-30-2023 10:47 AM
@SpacePlasma wrote:
You raise an interesting point, though my case would be much simpler than the examples you provided. Both polygons would have their vertices connected through exclusively straight lines with no cross-over.
This only means something to you.
Exclusively straight lines? I'd think if the lines where bend (e.g. Bezier curves), you'd have mentioned it.
If you mean all polygons are concave, than that does (as mentioned) make a huge difference.
If one of the polygons is a square, I bed you can optimize more.
Or use one of the algorithms in the first reply. We can help translate that to LabVIEW, but we need to know your the exact input types.
To make matters more complex, it can be faster to hit test with a rough algorithm, before testing details. For instance, if the x min and x max don't overlap, you wouldn't have to test using a complex algorithm. If this happens a lot, it can be much faster to do a fast check first.
I need to create a program which checks if a 4-vertex polygon is entirely within a larger 5-vertex polygon.
Yet you're showing a 4 and 6 vertex polygon?
10-30-2023 12:34 PM
Also be aware if one vertex is exactly on a side of the other polygon, you might get false results due to the limitations of floating point math, You probably need to decide on a threshold for closeness.
10-30-2023 01:55 PM
Shrink the polygon by 1 pixel, touching becomes collision.
Shrink more pixels to predict a collision.