New Reply

Algorithm for testing intersection of point and compound polygon

 
Share Thread Thread Tools
Nov16-12, 05:52 PM   #1
 

Algorithm for testing intersection of point and compound polygon


I'm trying to find a reasonably fast method for testing whether or not a point (x,y euclidean coordinate system) lies inside a (preferably convex, concave or complex - though different methods for each would be OK) compound polygon with edges consisting of line segments, arcs and/or elliptical arcs (elliptical arcs may be rotated in addition to having a start and end angle). I've already written the algorithms for edge and volumetric intersections of Points, Lines, Rays, LineSegments, Arcs, EllipticalArcs, Circles, Ellipses, Rectangle, Polygons and every combination thereof (also edge-based intersections of CompoundPolygons) - so have plenty of algorithms to call from, however I would prefer something a bit cleaner than testing the number of hits along rays cast from the test point to all surrounding points if that can be avoided. Does anyone know of a better solution to this problem?
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Bird's playlist could signal mental strengths and weaknesses
>> Minus environment, patterns still emerge: Computational study tracks E. coli cells' regulatory mechanisms
>> Bacterium uses natural 'thermometer' to trigger diarrheal disease, scientists find
Nov20-12, 07:24 AM   #2
 
A way to do it would be to calculate the areas of all the triangles defined by that point and each two successive vertices. You can do this in an algorithm by using the cross product. If the point is inside the polygon, this sum will equal the area of the polygon, otherwise it will not. Since your polygon is convex, I'm pretty sure that this is guaranteed.
Nov20-12, 07:00 PM   #3
 
Recognitions:
Gold Membership Gold Member
Science Advisor Science Advisor
Assuming that the nodes of the polygon are given and each of the lines/arcs is defined by a function you can determine:

A decent algorithm is to compare the sides of the polygon to the Y (vertical) coordinate of the test point. Retain the x coordinate values.

If the number of intersections is 2, then test the x coordinates of the intersections against the x coordinate of the point. This assumes that you have a function to generate the coordinate from the y coord using the nodes of the polygon. This is O(n) for the case line segments only, where n=nodes in the polygon. I've not tried it for what you are attempting.

This is based on work by D R Finley. see: http://alienryderflex.com/polygon/
Nov24-12, 04:11 AM   #4
 

Algorithm for testing intersection of point and compound polygon


Quote by meldraft View Post
A way to do it would be to calculate the areas of all the triangles defined by that point and each two successive vertices. You can do this in an algorithm by using the cross product. If the point is inside the polygon, this sum will equal the area of the polygon, otherwise it will not. Since your polygon is convex, I'm pretty sure that this is guaranteed.
Quote by jim mcnamara View Post
Assuming that the nodes of the polygon are given and each of the lines/arcs is defined by a function you can determine:

A decent algorithm is to compare the sides of the polygon to the Y (vertical) coordinate of the test point. Retain the x coordinate values.

If the number of intersections is 2, then test the x coordinates of the intersections against the x coordinate of the point. This assumes that you have a function to generate the coordinate from the y coord using the nodes of the polygon. This is O(n) for the case line segments only, where n=nodes in the polygon. I've not tried it for what you are attempting.

This is based on work by D R Finley. see: http://alienryderflex.com/polygon/
Thanks for the suggestions, I ended up solving this in the case of convex, concave and complex compound-polygons as follows if anyone is interested:

Cast a ray starting at the point being tested to the mid-point of each edge (drawing through a virtual line-segment at the start and end points of each edge, not following the arc/elliptical-arc if one exists) and count the number of intersections after merging any intersecting points and line-segments of the detected intersections into a single line-segment and thereby counting it as a single intersection. If all intersection counts are odd and non-zero the point is inside the compound-polygon, otherwise it is outside.

It may be slower than ideal, but I wanted to get the algorithm correct and move on to the next one before optimizing them all (working on the joys of compound-polygon and [rectangle,polygon,compound-polygon] intersections now) - the first two should be relatively simple (already have polygon-[arc,elliptical-arc,circle,ellipse-rectangle] intersection code written in a similarly mutable and accurate manner) but the compound-polygon/compound-polygon intersections might be a bit of a chore.

Again, thanks for the tips.
New Reply
Thread Tools


Similar Threads for: Algorithm for testing intersection of point and compound polygon
Thread Forum Replies
Algorithm for cutting a polygon into specific shapes Differential Geometry 5
Line intersection algorithm optimization Engineering, Comp Sci, & Technology Homework 0
Among the six measurements of the boiling point of a silsicon compound Set Theory, Logic, Probability, Statistics 2
Formula/Algorithm to apply force to an arbitrary point on a polygon Classical Physics 15
Testing a Lanczos (tridiagonalization) algorithm Linear & Abstract Algebra 8