I want to obtain a shape formed by the area enclosed by or in between Line[] objects. The lines have been extracted from various types of plots. There can be a GeometricTransformation acting on the Line that need to be maintained.

A simplified example would be to go from

{Line[{{0, 0}, {0, 1}, {1, 2}}], 
   Line[{{0, 0}, {1, 0}}], {{{1, -1}, {0, -1}}, {0, 0}}]]}

to a shape like

Polygon[{{0, 0}, {0, 1}, {1, 2}, {1, 0}}]

but of course in an automatic way dealing with lines consisting of many points.

It seems to me that the Filling functions unfortunately are expecting functions or data directly instead of the Line objects produced by plots.

P.S.: It might be easier to work within a plot directly so that I can use the standard Filling techniques but in that case I need at the very least a clever way to deal with rotating interpolated functions. If I want f[x] but rotated by an angle theta that is easy to do by acting with the rotation on points or on a Line but not easy to work on the interpolation function directly. I guess I can convert the lines to points and create ListLinePlot's from that but is there a better way to work directly with the Line objects? (P.P.S.: The enclosed region is not necessarily convex.)


In general, generating nonconvex polygons is nontrivial unless you have an algorithm for determining which point to go to next. There's a pretty nice NonConvexHullMesh function in the repository that we can make use of.

linepts = {{0, 0}, {0, 1}, {1, 2}};
transformpts = {{0, 0}, {1, 0}};
geotrans = {{1, -1}, {0, -1}}.# + {0, 0} & /@ transformpts;
polypts = Union[linepts, geotrans];
mesh = ResourceFunction["NonConvexHullMesh"][polypts, 10]

Final calculated region.

I'm using the raw points here, but if your points are already in the form of lines, you can always extract the points using something like Cases[myline, {_,_}] to get the points back. GeometricTransformation states that GeometricTransformation[object, {m, v}] yields m.r + v for each point r in object, so I'm using that definition since the transformation doesn't seem to actually apply until you put it into a Graphics form.

In the ResourceFunction, the second argument (I have 10) is the sensitivity. Depending on the number of points and how closely spaced they are, you may have to adjust this sensitivity. As I mentioned before, non-convex hulls are difficult. If you know enough about the points beforehand, you may be able to come up with an efficient method of selecting the points directly.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.