r/gamedev • u/simswerdev • Jun 25 '14
Question about 2D polygon angle simplification
I've been looking up some ways to change the red polygon into the blue polygon in this diagram. You'll notice that the new blues angles are all multiples of 15°. I don't mind degeneration of the original polygon and the "corrections" can be iterative or simultaneous. I need it for level generation if that helps. The final polygon can be concave or convex, no problems. I think the Ramer-Douglas-Peucker algorithm (a polygon simplification algorithm) could be useful somehow, but maybe there's something else I don't know about. Can anyone push me in the right direction? What do we call this problem? Thanks a lot!
3
u/thesneezingpanda Jun 25 '14 edited Jun 25 '14
Maybe start with one line that you want to stay the same, that iterate around the shape rounding lines to nearest to the nearest 15°, i.e. so that line0 stays the same, then line1 is rounded 15° to line0, line2 is rounded 15° to line1 etc. Then close the shape with an extra line at 15° between line0 and lineN?
For angles between vectors see this
3
u/[deleted] Jun 26 '14
Try caching the endpoints of each line then using an IK algorithm to adjust the angles. So after you change one, see what change needs to happen to the next segment to get it as close as possible to its original position. If you change a segment that's 500 units long by 5 degrees then the endpoint will move substantially more than a 100 unit long one. This is why you get screwed up results.
2
u/simswerdev Jun 29 '14
I've solved the problem (finally), considering all the suggestions given by everyone. Thanks very much!
Here's an album of the result. I haven't finished the "pinching-off" part, but I don't need it yet; just wanted to simplify every other angle first.
I've changed my approach somewhat to stave of frustration and headaches and unnecessary forays into advanced (to me) calculation. Now I just add a vertex. Don't know why I didn't think of that before.
Ultimately the function does the following (pseudocode).
(1) Iterate over every vertex.
(2) Find midpoint between i (v1) and i+1 (v2).
(3) Find angle between v1+rayOnXAxis, v1 and midpoint.
(4) Round (3) to _angleMultiple.
(5) Calculate error between (3) and (4).
(6) If error exists:
(6.1) Rotate midpoint (vNew) around v1 to negate error and insert as new vertex.
(6.2) Increment totalCorrections.
(6.3) If totalCorrections>_noOfCorrections.
(6.3.1) Shoot a ray from v1->vNew. Whichever is closest, v2's x axis or v2's y axis, insert that intersection as the next vertex.
(6.3.2) Set totalCorrection to 0 and increment i (to exit the correction cycle).
If you don't limit the total corrections, a polygon can have hundreds of new vertices to compensate. That's just unnecessary to me, unless you need that kind of thing, in which case it can do it.
Finally, I also have a partially working function that can do this. PM me if you're interested, cause I'm out of brain juice to just do this for fun anymore.
Thanks again and I hope it's helpful.
2
u/RandyGaul @randypgaul Jun 26 '14 edited Jun 26 '14
You'll want to have some heuristic for defining an error given some angle between two edges. In this case the error might be defined as something like: how close to a 15 degree interval we are, and how long the sum of the edges contributing to the angle are. Longer edge lengths would contribute more error, and farther from a 15 degree interval would contribute more error.
Once a maximum error is found, the vertex between the two edges can be moved such that a 15 degree angle is formed. I'm sure you can figure this part out.
This can be applied iteratively until the maximum error is below a given threshold. Perhaps a better error metric can be defined than the one I wrote down here.
2
u/dragbone @dragbone Jun 26 '14
If you want to keep the number of corners constant then I recommend an iterative approach:
You can do 1. by remembering that the total internal angle of a polygon with n corners is (n-2)*180° = (n-2)*12*15°. For a estimate on the quality of an approximation I'd use summed square distance. I can not say much about the quality of the result but it should not be too bad. This procedure has the advantage of guaranteeing that your solution follows the constraint.