I've got a binary grid which is mostly zero but contains a bunch of 1-cell wide lines (e.g. obtained from a ridge extraction). You can obtain an example input from the following line:
grid = ArrayPlot[
Table[
Sin[x + Pi/2*Tanh[y/3]] > -0.99 // Boole,
{x, -10, 10, 0.2},
{y, -10, 10, 0.2}
],
Frame -> None,
ImageSize -> 101,
PlotRangePadding -> None
] // Binarize // Thinning // ImageData;
My actual data is a bit more complicated, but I've made sure that no white pixel has three white neighbours, so that the (Moore-)connected components really are just curves.
I've also got the data in the form of a list of coordinate pairs:
coords = Position[grid, 1];
Now I've got one of those points. It doesn't matter how I obtain it, but for testing you can just do:
startingPoint = RandomChoice[coords];
Now my goal is to walk along the line from that point in both directions either for a fixed number of steps or within a fixed (Euclidean or Chebyshev) distance (10 steps/cell widths, say) and collect all those points. That is, I want the (ordered) set of points which are on the same line as the startingPoint
and in its immediate vicinity. I could either work with two ordered lists, one for the line segment on each side of startingPoint
, or a single ordered list from one end of the line segment to the other.
I can easily solve this with some procedural code, where I just repeatedly look at the neighbours of the last point(s) I added and add their other neighbour to the list(s) as well. However, this doesn't seem like a very Mathematica-like solution.
What would be an efficient and idiomatic way to obtain this neighbourhood along the line?
FindCurvePath
will be useful. $\endgroup$FindCurvePath
seem to be a bit too magical to use it reliably. It's not really clear when it tries to close a loop and when not, and when it suddenly breaks up the line. $\endgroup$