good chemistry is complicated,and a little bit messy -LW PerlMonks

Polygon interact other polygon

by animal_x (Initiate)
 on Oct 24, 2004 at 11:35 UTC Need Help??

animal_x has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys, I need a function that check if one polygon interact(intersect,touch or inside) other polygon. thanks

Replies are listed 'Best First'.
Re: Polygon interact other polygon
by borisz (Canon) on Oct 24, 2004 at 11:49 UTC
Re: Polygon interact other polygon
by tall_man (Parson) on Oct 24, 2004 at 22:27 UTC
The module Math::Geometry::Planar contains a GpcClip function that can find the intersection of two polygons. Your test will be that the intersection is not empty.
Thanx The shortest answer and exactly what i needed.
Re: Polygon interact other polygon
by bart (Canon) on Oct 24, 2004 at 21:12 UTC
There's a nice simple algorithm to check if a point is inside a polygon: the principle is this: draw a line from it to any point outside the polygon (for example with an X that is larger than any X for all the polygon vertices), and count how many edges it crosses. Iff this number is even, the point is outside of the polygon.

There are problematic cases where your test line goes straight through a vertex. You could think up an elaborate advanced scheme, taking the direction of the crossing into account, and counting such a crossing as +0.5 or -0.5 depending on its direction; or you could simply try again with a different line.

Combine this with tests on whether any edges from the polygons actually cross, and the basis is set.

Re: Polygon interact other polygon
by zentara (Archbishop) on Oct 24, 2004 at 13:56 UTC
From just thinking about the problem, my first thought, is if it's polygons, then you should have the equations of the line segments which make up it's sides. All you have to do is see if any segment from polygon1 intersects any segment from polygon2. How to find the intersection of 2 lines has a well known solution, called "simultaneous equations".

I'm not really a human, but I play one on earth. flash japh
"How to find the intersection of 2 lines has a well known solution,"

Well, just to add a bit detail here: line is the extension of a line segment on both ends. Two line intersect each other, does not mean that the two original line segments intersect. Your solution is fine, other than that we have to add one more step: after you get the point of intersection, you have to go back and check whether the intersection point is on both original line segments.

You are right, but I didn't go into detail because I figured it would be obvious to do x-y bounds checking.

I'm not really a human, but I play one on earth. flash japh
That won't catch the case where one polygon is entirely inside the other.
Good point. That is a case where the line segments need to be extended to infinity, before checking for intersection. Then using that in conjunction with the known x and y maximum and minimums, one should be able to figure out if one is inside another. It's an interesting problem. I can see a set of nested loops. You can probably do some "pre-filtering" of the line segment extreme endpoints, to determine which type of tests to perform. If all extreme endpoints of one polygon fall between the extreme endpoints of the other, and there is no intersection detected between segments.....??? Sounds like homework now.

I'm not really a human, but I play one on earth. flash japh
Re: Polygon interact other polygon
by FoxtrotUniform (Prior) on Oct 24, 2004 at 21:35 UTC

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://402017]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-22 23:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found