使用笛卡尔坐标输出问题绘制通过二维列表的路线

问题描述 投票:0回答:1

我正在尝试根据 txt 文件中的“NSEW”方向绘制通过网格的“无人机”路线,并输出无人机位置的笛卡尔坐标。运行代码时,要么网格正确而坐标错误,要么坐标错误。我不得不使用特殊的修复方法,例如跟踪 2 个

drone_pos
变量,这感觉是不必要的。我怀疑这与索引有关:

def file2list(file):
    """
    Helper function that converts a file to a list
    """
    with open(file) as f:
        lines = [word for line in f for word in line.split()]
    
    return lines 

def cart2index(x, y, height):
    idxX = x - 1
    idxY = height - y

    return [idxY, idxX]

def cart2indexreverse(x, y, height):
    idxX = x - 1
    idxY = height - y

    return [idxX, idxY]


def index2cart(x, y, height):
    Xcoord = x + 1
    Ycoord = height - y

    return [Xcoord, Ycoord]

def drone_route(route_list):
    """
    Route plotter: generates a grid and tracks current position of drone on the grid
    """
    route = route_list[2:].copy()
    start_coords = route_list[:2].copy()
    start_pos = list(map(int, start_coords))
    start_pos = cart2index(start_pos[0], start_pos[1], HEIGHT)
    start_pos2 = list(map(int, start_coords.copy()))
    start_pos2 = cart2index(start_pos2[0], start_pos2[1], HEIGHT)
    print(start_pos)
    grid = [[0 for _cols in range(WIDTH)] for _rows in range(HEIGHT)]
    
    visited = False
    directions_index = {'N' : [0, -1],
                  'S' : [0, 1],
                  'E' : [1, 0],
                  'W' : [-1, 0]}
    
    directions_coords = {'N' : [-1, 0],
                  'S' : [1, 0],
                  'E' : [0, 1],
                  'W' : [0, -1]}



    if start_pos[0] > HEIGHT - 1 or start_pos[1] > WIDTH - 1:
        raise Exception("Drone starting position outside of grid, input valid start position") 
    
    print('Coordinates:\n')
    print(str(index2cart(start_pos[0], start_pos[1], HEIGHT)))

    grid[start_pos[0]][start_pos[1]] = 's'    # s marks the drones start position
    drone_pos = [start_pos[0], start_pos[1]]  # init drone position as the start of the position in file
    drone_pos2 = [start_pos2[0], start_pos[1]]
    for dir in route:
        if dir in directions_index:
            if drone_pos[0] > HEIGHT-1 or drone_pos[0] < 0 or  drone_pos[1] >  WIDTH-1 or drone_pos[1] < 0: 
                raise Exception("Drone has moved outside of the grid, input valid route")
            else:
                drone_pos2 += np.array(directions_coords[dir])
                drone_pos += np.array(directions_coords[dir])
                print(str(index2cart(drone_pos2[0], drone_pos2[1], HEIGHT)))
                grid[drone_pos[0]][drone_pos[1]] = '+'  # tracks the drone's position through the grid
                
        else:
            raise Exception("Invalid direction: Use 'NSEW'")

    grid[drone_pos[0]][drone_pos[1]] = 'x' # marks drone's final position
    

    for row in grid:
        print(str(row))

*driver function*

输出:

[1, 10]
[2, 10]
[3, 10]
[3, 11]
[4, 11]
[5, 11]
[6, 11]
[6, 10]
[6, 9]
[6, 8]
[7, 8]
[8, 8]
[8, 9]
[8, 10]
[9, 10]
[9, 9]
[9, 8]
[9, 7]
[9, 6]
[8, 6]
[7, 6]
[6, 6]
[5, 6]
[5, 7]
[4, 7]
[3, 7]
[3, 6]
[3, 5]
[4, 5]
[4, 4]
[5, 4]
[5, 3]
[6, 3]
[7, 3]
[7, 4]
[8, 4]
[9, 4]
[10, 4]
[11, 4]
[12, 4]
[12, 3]
[11, 3]
[11, 2]
[11, 1]
[0, 0, 's', 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, '+', 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, '+', '+', 0, 0, '+', '+', '+', 0, 0, 0, 0]
[0, '+', 0, 0, 0, '+', 0, '+', '+', 0, 0, 0]
[0, '+', 0, 0, 0, '+', '+', 0, '+', '+', 0, 0]
[0, '+', '+', '+', '+', 0, '+', 0, 0, '+', 0, 0]
[0, 0, 0, 0, '+', 0, '+', 0, '+', '+', 0, 0]
[0, 0, '+', '+', '+', 0, '+', 0, '+', 0, 0, 0]
[0, 0, '+', '+', '+', '+', '+', 0, '+', 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, '+', 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, '+', '+', '+', 'x']
[0, 0, 0, 0, 0, 0, 0, 0, '+', '+', 0, 0]

网格是正确的,但坐标应该是:

[3, 12]
[3, 11]
[3, 10]
[2, 10]
[2, 9]
[2, 8]
[2, 7]
[3, 7]
[4, 7]
[5, 7]
[5, 6]
[5, 5]
[4, 5]
[3, 5]
[3, 4]
[4, 4]
[5, 4]
[6, 4]
[7, 4]
[7, 5]
[7, 6]
[7, 7]
[7, 8]
[6, 8]
[6, 9]
[6, 10]
[7, 10]
[8, 10]
[8, 9]
[9, 9]
[9, 8]
[10, 8]
[10, 7]
[10, 6]
[9, 6]
[9, 5]
[9, 4]
[9, 3]
[9, 2]
[9, 1]
[10, 1]
[10, 2]
[11, 2]
[12, 2]

python arrays list
1个回答
0
投票

问题在这里

def index2cart(x, y, height):
    Xcoord = x + 1
    Ycoord = height - y

    return [Xcoord, Ycoord]

我认为这应该是

的逆
def cart2index(x, y, height):
    idxX = x - 1
    idxY = height - y

    return [idxY, idxX]

应该是

def index2cart(y, x, height):
    Xcoord = x + 1
    Ycoord = height - y

    return [Xcoord, Ycoord]

请注意,我交换了参数中的 x,y 坐标

© www.soinside.com 2019 - 2024. All rights reserved.