views:

51

answers:

3

I'm making a vector drawing application. I use this algorithm to generate outlines.

This algorthm works well, except it does not close the outline as seen here: alt text

I'm not sure what I should do to ensure that it always closes the outline. I tried inserting the last vertex at position[0] in the std::vector but this did not help.

DOUBLEPOINT looks like:

struct DOUBLEPOINT {
double point[2];
};

How can I make it so it always properly closes the shape even on sharp corners?

Thanks

+1  A: 

How about:

for( size_t i = 0; i < input.size(); ++i )
{
    POINTFLOAT cur;
    POINTFLOAT nxt;

    if( i == input.size() - 1 )
    {
       cur.x = input[i].point[0];
       cur.y = input[i].point[1];

       nxt.x = input[0].point[0];
       nxt.y = input[0].point[1];
    }
    else
    {
       cur.x = input[i].point[0];
       cur.y = input[i].point[1];

       nxt.x = input[i+1].point[0];
       nxt.y = input[i+1].point[1];
    }
Gianni
This almost works, but it does not round the end as seen here: http://img36.imageshack.us/img36/1385/asjhh.png
Milo
Well, you could use the same idea to add another point, and repeat: `cur = input[0]` and `nxt = input[1]`.
Gianni
+1  A: 

I usually just use modulus:

nxt = input[(i+1) % input.size()];
genpfault
This is harder to understand whats going on and why, and also is less efficient.
Gianni
Actually, I find it clearer and much shorter (and hence less error-prone).
Alok
I tried modifying my oroginal wth this and it has the same issue as above http://img36.imageshack.us/img36/1385/asjhh.png
Milo
@Gianni: Do you have your profiling results available?
GMan
+1  A: 

Try appending a copy of the first and second points to the end of the vector before plotting it. Your first and last line segment will overlap, but it should ensure that all the points are connected and all corners are rounded similarly.

bta