Thanks for the suggestions, I was able to follow some of your suggestions. I will try to roll with this one, I can continue tests with this to see if is actually useful.
class Line:
def __init__(self):
self.name = ''
self.point1 = [0,0]
self.point2 = [0,0]
def set_points(self, x1,y1 , x2,y2):
self.point1 = [x1,y1]
self.point2 = [x2,y2]
def __repr__(self):
return '{0}={1}:{2}'.format(self.name, self.point1, self.point2)
def createLines():
'''
0,0 1,0
| |
0,1 1,1
'''
# simplifying the problem
# * assume coords are in xy rather than uv
# (just to look nice on this view)
# * no 3d space used but 2d space
# * no curves used but lines
# (I think I can exclude curve-related notions
# and think in terms of points or edges)
# * assume that points are snapped onto each other
# (no problem if they share the same coordinates
# but most important is to think in terms of edges)
i = [Line(), Line(), Line(), Line()]
i[0].name = 'top'
i[0].set_points(0,0 , 1,0) #lr
i[1].name = 'right'
i[1].set_points(1,0 , 1,1) #tb
i[2].name = 'bottom'
i[2].set_points(1,1 , 0,1) #rl
i[3].name = 'left'
i[3].set_points(0,1 , 0,0) #bt
return i
def tryShuffle(lines):
# shuffle line order
import random
random.shuffle(lines)
# shuffle point directions by chance
for l in lines:
if random.random() >= 0.5:
print('shuffling points for', l.name)
t = l.point1
l.point1 = l.point2
l.point2 = t
def trySort(first:str, lines):
import math
# somehow i found the first line to be this
# (later will find some better way to determine this)
first = [l for l in lines if l.name == first]
first = first[0] if len(first) == 1 else None
print('first line:', first.name)
dist = lambda p1, p2: math.sqrt((p2[0]-p1[0])**2 + (p2[1] - p1[1])**2)
sortedlines = []
current = first
for loop in range(4): # it will have to run only 4 times
print('current', current.name)
sortedlines.append(current)
for other in lines:
# skip same
if current == other: continue
# point2 of current - should always match point1 of other
# (valid sequence goes like in clockwise order: point2-point1-point2-point1-...)
if dist(current.point2, other.point1) < 0.0001:
print('matched point1 of', other.name)
# this is a valid match because it respects the order
# switch current and repeat outer loop
current = other
break
# point2 of current - matches point2 of other
# (it means that other direction is flipped - better to flip the points here)
if dist(current.point2, other.point2) < 0.0001:
print('matched point2 of', other.name, '(but in wrong direction)')
# I just do the flip here and let the sort to the next iteration
tmp = other.point1
other.point1 = other.point2
other.point2 = tmp
current = other
loop -= 1
break
return sortedlines
def test():
lines = createLines()
print('created lines')
print(lines)
print('')
tryShuffle(lines)
print('shuffled lines')
print(lines)
print('')
lines = trySort('top', lines)
print('****')
print('sorted lines are')
print(lines)
test()