# 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, new pointC is  and PointB is .)

``````void fillPolyline(ofPolyline &line, ofVec3f a, ofVec3f b) {
if (a.distance(b) < 25) {
return;
} 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.begin();
testLine.end();
``````

Calling the function once works:

``````fillPolyline(testLine, testLine.getVertices(), testLine.getVertices());
``````

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

``````fillPolyline(testLine, testLine.getVertices(), testLine.getVertices());
fillPolyline(testLine, testLine.getVertices(), testLine.getVertices());
``````

And a for loop crashes ``````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 Solved!

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

I’ll just use that instead 