Hello again,
Here is a more representative data sample I use to build the polygonal chains I need:
sampledata={{{171.797, 143.727}, {165.422, 143.727}}, {{165.422,
143.727}, {165.422, 140.844}}, {{165.422, 140.844}, {165.223,
140.844}}, {{165.223, 140.794}, {165.422, 140.794}}, {{165.422,
140.794}, {165.422, 137.91}}, {{165.422, 137.91}, {171.797,
137.91}}, {{171.797, 137.91}, {171.797, 143.727}}, {{178.197,
143.827}, {165.223, 143.827}}, {{165.223, 143.777}, {171.847,
143.777}}, {{171.847, 143.777}, {171.847, 137.91}}, {{171.847,
137.91}, {178.197, 137.91}}, {{178.197, 137.91}, {178.197,
143.827}}, {{184.597, 143.977}, {165.223, 143.977}}, {{165.223,
143.877}, {178.247, 143.877}}, {{178.247, 143.877}, {178.247,
137.91}}, {{178.247, 137.91}, {184.597, 137.91}}, {{184.597,
137.91}, {184.597, 143.977}}, {{190.997, 144.177}, {165.223,
144.177}}, {{165.223, 144.027}, {184.647, 144.027}}, {{184.647,
144.027}, {184.647, 137.91}}, {{184.647, 137.91}, {190.997,
137.91}}, {{190.997, 137.91}, {190.997, 144.177}}, {{197.397,
144.427}, {165.223, 144.427}}, {{165.223, 144.227}, {191.047,
144.227}}, {{191.047, 144.227}, {191.047, 137.91}}, {{191.047,
137.91}, {197.397, 137.91}}, {{197.397, 137.91}, {197.397,
144.427}}}
Using a manipulate function, it can be seen that all individual segments build a set of 5 open polygons that I want to detect but all segment are not ordered by polygon ...
Manipulate[Take[sampledata, {1, i}] // Line // Graphics, {i, 1,Length[sampledata], 1}]
EndSegs2D is a list supposed to content all pairs of points each constituing a 2D segment , in the form {{p1, p2}, ...} where p1 = {x1, y1}, p2 = {x2, y2}, etc ...
EndSegs2D = sampledata
polygons = {};
TreatedSegs2D = {};
i = 1;
While[Length[EndSegs2D] != 0,
i0 = 1;
endpoint = EndSegs2D[[i0, 1]];
startpoint = EndSegs2D[[i0, 2]];
origin = endpoint;
epsilon = 0.001;
vertex = {origin};
point = {};
test = 1;
While[point != origin,
point1 =
Select[EndSegs2D, (EuclideanDistance[#[[1]], startpoint] <=
epsilon && EuclideanDistance[#[[2]], endpoint] > epsilon ) &];
point2 =
Select[EndSegs2D, (EuclideanDistance[#[[2]], startpoint] <=
epsilon && EuclideanDistance[#[[1]], endpoint] > epsilon ) &];
point = {};
If[point1 != {}, point = point1[[1, 2]];
pos = Position[EndSegs2D, point1[[1]]][[1, 1]];
AppendTo[TreatedSegs2D, EndSegs2D[[pos]]];
EndSegs2D = Delete[EndSegs2D, pos]];
If[point2 != {}, point = point2[[1, 1]];
pos = Position[EndSegs2D, point2[[1]]][[1, 1]];
AppendTo[TreatedSegs2D, EndSegs2D[[pos]]];
EndSegs2D = Delete[EndSegs2D, pos]];
If[point == {}, EndSegs2D = Delete[EndSegs2D, 1];
Print["Break at ", EndSegs2D // Length, " ", i]; test = 0; Break[]];
If[point == origin, Print["Ok ", i];
AppendTo[TreatedSegs2D, EndSegs2D[[1]]];
EndSegs2D = Delete[EndSegs2D, 1]];
endpoint = startpoint;
AppendTo[vertex, startpoint];
startpoint = point];
If[Length[vertex] > 3, AppendTo[vertex, origin];
AppendTo[polygons, vertex]; i++];]
Here a graphics check of the result. It can be seen that my code is not perfect because each independant polygon chain is not detected (it seems that the detection is faulty because each polygon is essentially scanned in one direction)
{{Green, Line[polygons]}, {Blue, vertex // Line}, {Black,Line[sampledata]}} // Graphics
Hope this is more clear.
Thanks in advance for your comments
Christian