Convex Partitioning

Here's the code if anyone's interested. There are cases when it's sub-optimal, where it uses more vertices than it needs to, and it got into an infinite loop once, but I haven't been able to reproduce it... maybe I inadvertently fixed it, I don't know.

http://www.sfu.ca/~mnb2/conv-part.zip
Another update for you guys.... I coded up an OPTIMAL solution. It's so damn inefficient it can't handle more than 13 vertices. Professor said my other algo wasn't good enough, so he made me code up this POS. The other one can handle over 500 vertices in the blink of an eye and is optimal 99.9% of the time... (I can't find a case where it's not, but maybe I'm just stupid).

Anyway, if you're intrigued, the optimal sol'n works like this (it's O(n^4)... at least)

Code:
EdgeList Polygon::decomp() {
    Edge diag;
    EdgeList min, tmp1, tmp2;
    int nDiags = std::numeric_limits<int>::max();
    bool found = false;

    for (int i = 0; i < v.size(); ++i) {
        if (isReflex(i)) {
            for (int j = 0; j < v.size(); ++j) {
                if (canSee(i, j)) {
                    tmp1 = copy(i, j).decomp();
                    tmp2 = copy(j, i).decomp();
                    tmp1.insert(tmp1.end(), tmp2.begin(), tmp2.end());
                    if (tmp1.size() < nDiags) {
                        min = tmp1;
                        nDiags = tmp1.size();
                        diag = Edge(at(i), at(j));
                        found = true;
                    }
                }
            }
        }
    }
   
    if (found) min.push_back(diag);
    return min;
}
Thanks for this - I'm just starting to implement non-square shapes into my game's level editor, and this has saved me a lot of work!

Where would you suggest would be the best place to put a point limit on created polys (I want to stick to Box2D's default limit of 8 points on a poly)

Btw, I've ported your algo to C# (as that's what my editor's written in), so shout if you'd like a copy!

Original thread: https://box2d.org/forum/viewtopic.php?t=2057