I am looking for a way to sort and combine objects / segments into one connected segment. I have a list of objects and I have to arrange them so that the end of one matches the beginning of the other (some of them can be reversed), is there some way to do it, thank you in advance.
class Line:
def __init__(self, name, start, end):
self.name = name
self.startPoint = start
self.endPoint = end
def __repr__(self):
return f"<{self.name}{self.startPoint}-{self.endPoint}>"
a = Line("d", (4, 4), (3, 3))
b = Line("b", (2, 2), (3, 3))
c = Line("c", (1, 1), (2, 2))
d = Line("a", (0, 0), (1, 1))
l = [a, b, c, d]
#result
# a.reverse
sorted_l = [d, c, b, a]