How do I scale a ofPolyline? or convert `ofPolyline` to `ofPath`?


#1

Hello,

I would like to scale up an ofPolyline that i’m getting from and ofxCv::contourFinder.
I see that ofPath has a scale method but ofPolyline does not.

What would be the best way to convert an ofPolyline to an ofPath and back, so that I can scale?


#2

Yeah I’d also love to know!


#3

Wouldn’t it be enough to do

    for(auto & vert : line) {
        vert *= 2.0f;
    }

to scale it 2x? And if the idea is just to draw it larger, ofScale(2.0f) would work too, right?


#4

Sure, it works. It’s just not very convenient to be doing this manually all over the place.


#5

If you create a function to scale polylines, it’s pretty much the same amount of code, or?

myPath.scale(2.0f);

vs

myScalePoly(myPoly, 2.0f);

Other options: convert it to ofPath and back, or change ofPolyline so it does have a scale method :slight_smile:


#6

How do you convert to ofPath, by the way? I think that was part of the original question…


#7

@anselan converting a polyline to path is pretty much as much code since it’s just creating a new path and passing all the points from the polyline into the path.

There’s a PR to directly create a Path from a Polyline but it’s not very well tested so it hasn’t been merged yet

if anyone wants to a PR to add scale, translate… to polyline i think that could be useful


#8

Cool, I can copy translate, rotate and scale from ofPath into ofPolyline.


#9

Some test code:

ofPolyline line;
line.addVertex(-50, -50);
line.addVertex( 50, -50);
line.addVertex( 50,  50);
line.addVertex(-50,  50);
line.close();

line.rotate(ofGetFrameNum(), glm::vec3(0.0, 0.0, 1.0));
line.translate(glm::vec2(200, 200));
line.draw();
line.scale(0.5f, 0.5f);
line.draw();
line.scale(0.5f, 0.5f);
line.draw();

polyline

Notice that transformations happen around (0, 0, 0). That’s why by scaling after the translation it moves towards the origin. The methods are based on the matching ones found in ofPath.