AutoPolygon for multiple island shapes

AutoPolygon for multiple island shapes
0

Hi!

I have been using this AutoPolygon for my project to trace the vertices for user drawn shapes on my render texture.
However what I can achieve by far is getting only 1 enclosed shape within the image, while i will need ALL the shapes that are drawn by the user.

I am trying this approach of looping through several segments for my image and performing this function
std::vector trace(const cocos2d::Rect& rect, float threshold = 0.0f)
on each of them, and then hopefully to combine them with Clipper library.

However, the points returned from the execution are all relative to the small size of the rect segment that i passed in. For eg, i passed in Rect = {origin={x=204.000000 y=272.000000 } size={width=71.0000000 height=68.0000000 } }
But the points returned are all within the range of 0~60 for x & y.

Can anyone advise what i’ve been doing wrongly? Or is there a better way to achieve the goal?

Thanks!

A picture is worth a thousand words, so perhaps you should post up some screenshots of what you’re trying to do.

A few key points from that:

1 - You pass in a rectangle segment.
2 - The point values returned are relative to the rectangle you passed in.

You created the initial rectangle segment, so the values are known. When you receive the point that’s based off that rectangle segment, why don’t you simply add the x and y values from the rectangle segment to those point values? That will give you the point values relative to the texture rectangle, which I’m assuming is what you’re after.

Hi @R101, thank you for responding to my post!

yes you are right on the points that i am trying to describe. And here i have uploaded a picture for a better presentation of what i am doing here.

And yes, after posting the question, I did exactly what you suggested and I eventually got the result that I wanted :slight_smile:
However, I needed to alter some of the codes in AutoPolygon in order for it to work correctly. Under the marchSquare function, there is this portion that performed some flipping to the y values which i removed from there.

if(stepx == prevx && stepy == prevy)
{
    _points.back().x = (float)(curx-rect.origin.x) / _scaleFactor;
    //_points.back().y = (float)(rect.size.height - cury + rect.origin.y) / _scaleFactor;
    _points.back().y = (float)(cury - rect.origin.y) / _scaleFactor;
}
else
{
    //_points.push_back(Vec2((float)(curx - rect.origin.x) / _scaleFactor, (float)(rect.size.height - cury + rect.origin.y) / _scaleFactor));
    _points.push_back(Vec2((float)(curx - rect.origin.x) / _scaleFactor, (float)(cury - rect.origin.y) / _scaleFactor));
}

But then added it back only after the points are being returned to me after the trace, reduce & expand functions. (that were called in generateTriangles)

// some refactoring of the points
for (auto& pt : points)
{
	pt += rect.origin / CC_CONTENT_SCALE_FACTOR();
	pt.y = pixelSize.height / CC_CONTENT_SCALE_FACTOR() - pt.y;
}

This will allow me to have all the points to be plotted at the correct positions according to the image.

I suppose this is a bug in the AutoPolygon class? Which didn’t just work by itself and needed these modifications?
Anyway, posting this here in case anyone would like to use a similar implementation :slightly_smiling_face:

If you’re confident that it’s a bug, then it’s best to log an issue in the cocos2d-x github repository. Also, if it is a bug and any fix is going to have an impact on the current usage, it may not be ideal to change it.

Regardless of whether it is or isn’t a bug, you should be able to sub-class it and change the implementation to suit your needs so you don’t have to change the engine code, if that works out better for you.