Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Polygon interact other polygon

by zentara (Archbishop)
on Oct 24, 2004 at 13:56 UTC ( #402033=note: print w/replies, xml ) Need Help??

in reply to Polygon interact other polygon

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

Replies are listed 'Best First'.
Re^2: Polygon interact other polygon
by pg (Canon) on Oct 24, 2004 at 17:15 UTC
    "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
Re^2: Polygon interact other polygon
by tilly (Archbishop) on Oct 25, 2004 at 17:37 UTC
    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
        Not so easy. Imagine a polygon that tracks out a big "C, then turns around and makes that into a strip. The other is a small circle in the middle of the C. Looking at extreme endpoints it would look like the first contains the other but it doesn't. I'd suspect that nailing down all possible cases could be very hard.

        Without knowing the standard algorithm, my natural approach would be to try to find intersections. If there are none then take the polygon with the point with the largest x-value, take any point of the other polygon, pull out my complex analysis and calculate a winding number by numerical integration. (In that calculation you're viewing the point (x,y) is treated as the complex number x + yi.) If that comes out non-zero (within the tolerance of rounding errors), then the one is inside the other. Otherwise not.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://402033]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2023-09-21 11:44 GMT
Find Nodes?
    Voting Booth?

    No recent polls found