Recursively add vertices to polyline?


Hi everyone!

I’m working on a function that fills out a poly line with points. It compares PointA and PointB in a polyline, and if the distance between the two points is greater than X, adds a new PointC at the halfway point. Then, it recursively checks the distance of PointA and PointC, and PointC and PointB and so forth. My function “half-works” in a small test case. Code is below.

(Also, I’m trying to maintain index order in polyline so points are walkable, so PointA is index[0], new pointC is [1] and PointB is [2].)

void fillPolyline(ofPolyline &line, ofVec3f a, ofVec3f b) {
	if (a.distance(b) < 25) {
	} else {
		//std::cout << "Distance is "<< a.distance(b) << std::endl;
		ofVec3f middlePoint = a.getMiddle(b);
		int index = line.getIndexAtLength(a.distance(b));
		line.insertVertex(middlePoint, index);
		fillPolyline(line, a, middlePoint);
		fillPolyline(line, middlePoint, b);

Test case – let’s make a test line with three points:

ofPolyline testLine;
testLine.addVertex(ofVec3f(100, 100));
testLine.addVertex(ofVec3f(400, 100));
testLine.addVertex(ofVec3f(400, 400));

Calling the function once works:

fillPolyline(testLine, testLine.getVertices()[0], testLine.getVertices()[1]);

Calling the function twice in a row results in the first two points being filled:

fillPolyline(testLine, testLine.getVertices()[0], testLine.getVertices()[1]);
fillPolyline(testLine, testLine.getVertices()[1], testLine.getVertices()[2]);

And a for loop crashes :frowning:

for (int i = 0; i < testLine.size()-1; i++) {
	fillPolyline(testLine, testLine.getVertices()[i], testLine.getVertices()[i+1]);

Ugh, my noob recursion brain.

Maybe I’m approaching this wrong or making this unnecessarily tough … is there a function/library that can fill an existing polyline with evenly-ish spaced points?

Thanks for your help :heart:



It turns out I totally missed the ofPolyline method getResampledByCount()

I’ll just use that instead :grinning: