Exemple #1
0
def dijkstra(start, is_allowed, seen, dimensions):
    dist = dimensions[0] * dimensions[1] * np.ones((dimensions[0], dimensions[1]), dtype=float)
    dist[start] = 0
    dseen = np.zeros((dimensions[0], dimensions[1]), dtype=bool)
    heap = PriorityQueue()
    heap.update(start, 0)
    while heap:
        _, p = heap.pop()
        for d in dirs:
            dis = dist[p]
            newp = add_dir(p, d, dimensions)
            npts = []
            k = 0
            while newp and not dseen[newp] and is_allowed(newp):
                k += 1
                npts.append((newp, dis + 0.001 * k))
                newp = add_dir(newp, d, dimensions)
            for (p_, d_) in npts:
                dist[p_] = min(dist[p_], d_)
                heap.update(p_, dist[p_])
        dseen[p] = True
        if not seen[p]:
            res = [p]
            c = 0
            while p != start:
                c += 1
                x, ps = min([(dist[_p], _p) for _p in [add_dir(p, d, dimensions) for d in dirs]], key=lambda x: x[0])
                p = ps
                res.append(ps)
                seen[p] = True
            res.reverse()
            return res
    return None
Exemple #2
0
def find_center(bin_im):
    px = bin_im.load()
    width, height = bin_im.size
    center = (width /2, height /2 + 9)
    c = center
    try:
        while px[c] != 255:
            c = add_dir(c, (0, 1), bin_im.size)
    except:
        c = center
        while px[c] != 255:
            c = add_dir(c, (0, -1), bin_im.size)
    return c
Exemple #3
0
def discover(p, seen, dim):
    res = []
    seen[p] = True
    dir = 0
    last_dir = 3
    while dir != last_dir:
        p_p = add_dir(p, dir, dim)
        p_s = add_dir(p, (dir + 1), dim)
        while (p_p and not seen[p_p]) or (p_s and not seen[p_s]):
            last_dir = dir
            p = p_p if (p_p and not seen[p_p]) else p_s
            res.append(p)
            seen[p] = True
            p_p = add_dir(p, dir, dim)
            p_s = add_dir(p, (dir + 1), dim)
        dir = (dir + 1) % 4
    return res