I have a list of coordinates in tuples. Every two tuples correspond to the line, for example ((x1,y1),(x2,y2)) - is a line. The list is unordered and contains connected (chained) and non-connected segments. Under connected (chained) I mean the following list: [((x1,y1), (x2,y2)), ((x3,y3),(x4,y4)), ((x5,y5),(x2,y2)), ((x3,y3),(x5,y5))] so that if to connect them together the line appears. The problem is that in the list I have different lines that I want to segment. For now if to draw all the segments the figure looks like the following:
I want to smooth it so that the result is a single line for each branch. How to do it better?
for now, I tried to find connected components by the following function:
def get_connected_components(data):
result = [[]]
for connect in data:
# empty, add all
if not result[-1]:
result[-1].extend(connect)
continue
# does not continue current streak
if result[-1][-1] != connect[0]:
result.append([c for c in connect])
# does continue current streak
else:
# to collect all intermediate steps as well
result[-1].append(connect[1])
# if you do not need to build the whole thing, use this instead:
# result[-1][-1] = connect[1]
# simplify result
r = [(r[0], r[-1]) for r in result]
return r
but the problem is that tuples can be flipped. Moreover, the connectivity can be not only by 2 lines in 1 point but in 3 or even more. But the filtering is available only for a single line, not for a connection of them.