I'm currently working on a problem where I need to properly order (using something like a right-hand rule) the nodes that make up a planar polygon in 3D space. My idea thus far is to construct a transformation matrix to convert the nodes to an x-y plane, then use a Graham scan. I need to ensure that the user enters only convex polygons, so if I find any "in开发者_如何转开发ternal" nodes, I know the polygon is concave and can throw an error. In addition to checking for convexity, the sorting procedure of the Graham scan will order the nodes for me.
I am not terribly familiar with optimized geometry algorithms. Does this seem like a appropriate/efficient process? Or is there a better way to:
1) Order the nodes of the polygon by some rule (e.g. RH rule) and 2) Ensure that the planar polygon (which may not be in the x-y plane) is convex?
Yes, that is a very good solution. Here's how to implement it to ignore numerical inaccuracy.
- take any 3 points; these determine the plane, rotate appropriately
- check that abs(z)<THRESHOLD for all z-coordinates, now you can ignore them
- perform Graham scan which is O(n log(n)) time
- return order, else throw error if results.size < #points (non-convex)
You might wish to choose 3 points sufficiently far apart, or a combination of many points (still has its issues), and make the THRESHOLD some fraction of the max distance between.
With a few assumptions, this is provably as good as you can do asymptotically, because otherwise, you could use this to perform a comparison sort in less than O(n log(n))
time, which we know is impossible without extra knowledge (the problem mapping would be to place each element X of an unsorted array at position [X,X^2] in the plane plus a point at [0,max^2]).
精彩评论