# Iterator for juce::Path

currently trying to find a point in a path where x == someValue
in order to do that it would be useful to have something like this:

for(const auto& p: path) if(p.x == someValue) doSomething();

Have you had a look at juce::Path::Iterator?

https://docs.juce.com/master/classPath_1_1Iterator.html

well noâ€¦ not yet. but if there already is an iterator, why did no one implement the nice for loop yet? or is it just a bad idea to loop through a path for some reason?

Paths arenâ€™t just a collection of points, and what points they do have arenâ€™t necessarily points the stroked version of the path will pass through.

Take a bezier curve for example, it has a start and end point and then 1 or 2 control points. If you iterated those points itâ€™d only give those 3/4 points, not a point for every x coordinate along the resultant curve.

That being said, there is also juce:PathFlatteningIterator which does convert a path containing curves into a series of straight lines and so each point given by that iterator will be a point along the curve.

Since thereâ€™s no way to know which set of points is desired by for (auto& p : path), such a feature isnâ€™t provided so a specific iterator must be chosen.

1 Like

that makes sense. wellâ€¦ but then it would still be nice to have some additional methods for the path-object at least. i already solved my problem in a different way now, but if i still used the path iâ€™d kinda like a method that works a bit like getPointAlongLine, but instead of getting a distance as argument, it would just get an x-value. that wouldnâ€™t make sense for paths that can have multiple x values per y value, but for stuff like drawing an envelope, or an oscilloscope, or a spectrum analyzer, where you know, that the path can only go into one direction on the x-achsis, it makes a lot of sense to have a function where you get a y value for only an x value as argument and nothing else.

edit: and yes, i tried making a vertical line from my x value to the height of the component and then using getClippedLine but that didnâ€™t do anything. i suppose it was because my path was not closed or something, but you know, sometimes one doesnâ€™t want to close a path because it just doesnâ€™t look good, sooo, having a getClippedLine-function that works with open paths would also solve the problem somehow

edit: orâ€¦ you know what would also be cool? to have a seperate object, which is not a path, but acts very similiar to a path, with the only difference being that itâ€™s restricted to graph-type data, where there canâ€™t be 2 x-values for one y-value. i know something like that could already be done with a simple vector of y values. but i imagine something that also has these path-type functions, like making it curved, smoothing the edges, drawing to juce::Graphics and so on

The issue is still even if the line is straight across the X-Axis, there can still be multiple points at an x position due to a â€śstepâ€ť

Itâ€™s a very special use case and I have the same issue â€“ PathFlatteningIterator is the closest functionality.