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



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?


Yeah I’d also love to know!


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?


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


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



myScalePoly(myPoly, 2.0f);

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


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


@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


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


Some test code:

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

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


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.