We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › avoid complex polygons
Page Index Toggle Pages: 1
avoid complex polygons (Read 516 times)
avoid complex polygons
Jun 19th, 2008, 11:25pm
 
i have a program that essentially connects an arbitrary number of points into a polygon.  points can be added and removed at runtime.  how can i ensure that the polygon drawn doesn't cross itself (i.e. that it's not a complex polygon)?

i thought, for some reason, that the java Polygon class would take care of that for me, but no so.

i'm sure this is a common problem that has been solved many times over, but i can't find it on the Google.  any thoughts?

thanks...
Re: avoid complex polygons
Reply #1 - Jun 20th, 2008, 12:41am
 
you can make some tests to see if each of your polygon's edges doesn't cross another one. java.awt.geom.Line2D has some nice intersection methods between 2 lines.
Re: avoid complex polygons
Reply #2 - Jun 20th, 2008, 6:58am
 
Do you try to make always a convex polygon, eg. by making a bounding polygon (like an elastic put around a set of pegs)?
Or just avoid crossing of lines?
Because in the first case, there is certainly known algorithms, but in the second one, there are several possible polygons as soon as the number of points increases.
Eg. with four points as a square and one in the middle, there is at least 4 possible polygons.
Re: avoid complex polygons
Reply #3 - Jun 20th, 2008, 9:33pm
 
the first.  the analogy of elastic around pegs is perfect.  actually, more perfect is the analogy of shrink-wrapping around pegs -- concave polys are ok, just no boundary crossing.

i managed to get the following algorithm to work for the most part:

Code:

find the highest (lowest y coord) vertex
calc the angle from highest vertex to all other vertices
sort vertices numerically by that angle
connect vertices in order


and that avoids overlaps nicely.  however, sometimes it generates strange shapes.  here's an example of the algorithm at work, with a strange shape:
http://transmote.com/lab/simplePoly/

with this arrangement of vertices, it seems more 'natural' to draw the perimeter as 0-2-1-3-0, but that violates the "sort by angle to highest vertex" algorithm.

for a solution, i'm thinking about sorting by that algorithm, then looping through all vertices again and comparing the angle created by reversing the order of the second two vertices in each triplet, and going with the larger of the two.  (i.e. with A012, also measure A021, and compare.)  this could result in a complex polygon again, so if this loop does reverse the order in which it connects vertices, it would have to check the second segment in the triplet against all other segments in the polygon for an intersection.

since i'll rarely have more than, say, 10 vertices at a time, it shouldn't be too much of an issue for performance.  of course, i don't even know yet if that will work, but i'll come back here to post my results when i get that far...which, due to scheduling, won't be for a while, unfortunately Smiley
Page Index Toggle Pages: 1