Consider the following code which plots a triangle.
p = {{0, 0}, {.2, 0}, {0, .2}};
{Cyan, Polygon[Dynamic[p]]} // Graphics
Then adding (for example) {.1, -.1}
yields a non-simple polygon with intersecting lines.
AppendTo[p, {.1, -.1}]
Question: Given a list of 2D points that are plotted as a polygon by Graphics
. Is there a way to re-order the points such that Graphics
plots a simple polygon after a point which has been added that resulted in plotting a non-simple polygon?
Answer
The function you may be looking for is FindCurvePath
. Its task is to find reasonable curves through disorganized point sets. It is not guaranteed to find the solution that you find most pleasing but if often gets close. You may also end up with several disconnected lines instead of a single one.
(* some points randomly drawn on a sine shape *)
pts = {#, Sin[#]} & /@ RandomReal[{0, 2 \[Pi]}, 30];
Graphics@Line@pts
(* now with FindCurvePath *)
Graphics@GraphicsComplex[pts, Line@FindCurvePath@pts]
In the case of your point set it finds the following:
Graphics@GraphicsComplex[p, {EdgeForm[Black], Polygon@FindCurvePath@p}]
i.e., a polygon and a line.
Comments
Post a Comment