def findpiece(a, b, q): found = None for (a1, b1) in pieces: (l, _) = pieces[(a1, b1)] l = [a1] + l + [b1] if a in l and b in l: ia = l.index(a) ib = l.index(b) d = 0 if ia < ib: lastn = l[0] for n in l[1:]: if lastn == a: d += q * distances[(lastn, n)] break d += distances[(lastn, n)] lastn = n found = ((a1, b1), d) else: l = rev(l) lastn = l[0] for n in l[1:]: if lastn == a: d += q * distances[(lastn, n)] break d += distances[(lastn, n)] lastn = n found = ((b1, a1), d) break return found
def extendpath_p(p, goaln, d0, n2, nz, acc): nlast1 = p[-1] if nlast1 == goaln: if nz == None or p[-2] == nz: #print("%f %s" % (d0, str(p))) return acc + [(d0, p)] for (n, d) in neighbours_p(nlast1): # Here, we should not look for only p, but for the sequence # [p[-1], n] if n in p: continue if len(p) == 1 and n2 != None and n != n2: continue sharpturns = [[6, 23, 5], [34, 23, 35], [5, 6, 23], [34, 35, 23], [35, 34, 23], [6, 5, 23], [3, 4, 34]] newp = p + [n] if len(newp) >= 3: newp3 = newp[-3:] if newp3 in sharpturns or rev(newp3) in sharpturns: continue acc = extendpath_p(p + [n], goaln, d0 + d, n2, nz, acc) return acc
def extendpath_p(p, goaln, d0, n2, nz, acc): nlast1 = p[-1] if nlast1 == goaln: if nz == None or p[-2] == nz: #print("%f %s" % (d0, str(p))) return acc + [(d0, p)] for (n, d) in neighbours_p(nlast1): # Here, we should not look for only p, but for the sequence # [p[-1], n] if n in p: continue if len(p) == 1 and n2 != None and n != n2: continue sharpturns = [[6, 23, 5], [34, 23, 35], [5, 6, 23], [34, 35, 23], [35, 34, 23], [6, 5, 23], [3, 4, 34]] newp = p + [n] if len(newp) >= 3: newp3 = newp[-3:] if newp3 in sharpturns or rev(newp3) in sharpturns: continue acc = extendpath_p(p + [n], goaln, d0 + d, n2, nz, acc) return acc
def findpiece(a, b, q): found = None for (a1, b1) in eight.pieces: (l, _) = eight.pieces[(a1, b1)] l = [a1] + l + [b1] if a in l and b in l: ia = l.index(a) ib = l.index(b) d = 0 if ia < ib: lastn = l[0] for n in l[1:]: if lastn == a: d += q * eight.distances[(lastn,n)] break d += eight.distances[(lastn,n)] lastn = n found = ((a1, b1), d) else: l = rev(l) lastn = l[0] for n in l[1:]: if lastn == a: d += q * eight.distances[(lastn,n)] break d += eight.distances[(lastn,n)] lastn = n found = ((b1, a1), d) break return found
def eightinit(): global distances, neighbours, pieces eightpath(19.2, 15.4, 12.5) neighbours = dict() piecelist = [ [6] + piece1 + [35], [5] + rev(piece4) + [34], piece2a + [23], [23] + piece2b, piece3a + [23], [23] + piece3b, [35, 34], # piece5 [5, 6], # piece6 [3, 4] ] # piece7 distances = dict() pieces = dict() for piece in piecelist: piece2pathpoints(piece, 0.35) dtot = 0 lastn = None for n in piece: if lastn != None: if not n in neighbours: neighbours[n] = [] neighbours[n] = neighbours[n] + [lastn] if not lastn in neighbours: neighbours[lastn] = [] neighbours[lastn] = neighbours[lastn] + [n] d = dist(nodes[n][0], nodes[n][1], nodes[lastn][0], nodes[lastn][1]) distances[(n, lastn)] = d distances[(lastn, n)] = d dtot += d lastn = n pieces[(piece[0], piece[-1])] = (piece[1:-1], dtot) pieces[(piece[-1], piece[0])] = (rev(piece[1:-1]), dtot)
def eightinit(): global distances, neighbours, pieces eightpath(19.2,15.4,12.5) neighbours = dict() piecelist = [[6] + piece1 + [35], [5] + rev(piece4) + [34], piece2a + [23], [23] + piece2b, piece3a + [23], [23] + piece3b, [35, 34], # piece5 [5, 6], # piece6 [3, 4]] # piece7 distances = dict() pieces = dict() for piece in piecelist: piece2pathpoints(piece, 0.35) dtot = 0 lastn = None for n in piece: if lastn != None: if not n in neighbours: neighbours[n] = [] neighbours[n] = neighbours[n] + [lastn] if not lastn in neighbours: neighbours[lastn] = [] neighbours[lastn] = neighbours[lastn] + [n] d = dist(nodes[n][0], nodes[n][1], nodes[lastn][0], nodes[lastn][1]) distances[(n, lastn)] = d distances[(lastn, n)] = d dtot += d lastn = n pieces[(piece[0],piece[-1])] = (piece[1:-1], dtot) pieces[(piece[-1],piece[0])] = (rev(piece[1:-1]), dtot)